Ansible为什么不更改默认的MariaDB密码?

huangapple go评论80阅读模式
英文:

Why doesn't Ansible change the default MariaDB password?

问题

我有一个Ubuntu虚拟机。但是,当我尝试通过Ansible更改MariaDB的默认密码(空密码)时,它说我没有权限这样做。我通常可以使用 sudo mysql -u root -p 登录MariaDB。

我尝试设置 become: yes,但没有成功。

你能帮我吗?

playbook.yml

- hosts: vm-bd-01
  vars:   
     mysql_root_password: password  
  tasks:
  - name: 安装MariaDB服务器
    apt:
      pkg:
      - mariadb-server
      - python3-pymysql
      update_cache: yes
    become: yes
  - name: 更改默认MariaDB密码
    become: yes
    mysql_user:  
      login_host: 'localhost'
      login_user: 'root'  
      login_password: ''
      name: 'root'
      password: '{{ mysql_root_password }}'
      state: present

错误

TASK [Change default MariaDB password] ************************************************************************************************************************************************************************************************************
fatal: [IP]: FAILED! => {"changed": false, "msg": "无法连接到数据库,请检查 login_user 和 login_password 是否正确,或者 /root/.my.cnf 文件中是否包含凭据。异常消息:(1698, 'Access denied for user 'root'@'localhost'')"}
英文:

I have an Ubuntu VM. However, when I try to change the default password (Empty Password) for MariaDB via Ansible, it says that I don't have permission to do so. I can normally login to MariaDB using sudo mysql -u root -p.

I tried setting become: yes, but it didn't work.

Can you help me?

playbook.yml:

- hosts: vm-bd-01
  vars:   
     mysql_root_password: password  
  tasks:
  - name: Install MariaDB Server
    apt:
      pkg:
      - mariadb-server
      - python3-pymysql
      update_cache: yes
    become: yes
  - name: Change default MariaDB password
    become: yes
    mysql_user:  
      login_host: 'localhost'
      login_user: 'root'  
      login_password: ''
      name: 'root'
      password: '{{ mysql_root_password }}'
      state: present

Error:

TASK [Change default MariaDB password] ************************************************************************************************************************************************************************************************************
fatal: [IP]: FAILED! => {"changed": false, "msg": "unable to connect to database, check login_user and login_******** are correct or /root/.my.cnf has the credentials. Exception message: (1698, \"Access denied for user 'root'@'localhost'\")"}

答案1

得分: 2

当连接时不需要密码时,您需要通过mysql unix套接字(/run/mysqld/mysqld.sock)而不是通过TCP连接进行连接。比较以下行为:

root@ubuntu:~# mysql
欢迎使用MariaDB监视器。命令以;或\g结尾。
您的MariaDB连接ID为31
服务器版本:10.6.12-MariaDB-0ubuntu0.22.04.1 Ubuntu 22.04

版权所有(c)2000、2018年、甲骨文、MariaDB公司Ab及其他公司。

键入'help'或'\h'获取帮助。键入'c'以清除当前输入语句。

MariaDB [(none)]> ^D再见

与此:

root@ubuntu:~# mysql -h 127.0.0.1
错误1698 (28000):拒绝用户'root'@'localhost'

重新编写您的playbook以通过unix套接字连接:

- hosts: vm-bd-01
  vars:
     mysql_root_password: password
  tasks:
  - name: 安装MariaDB服务器
    apt:
      pkg:
      - mariadb-server
      - python3-pymysql
      update_cache: yes
    become: yes
  - name: 更改默认MariaDB密码
    become: yes
    mysql_user:
      login_unix_socket: /run/mysqld/mysqld.sock
      login_user: 'root'
      login_password: ''
      name: 'root'
      password: '{{ mysql_root_password }}'
      state: present

通过这个更改,您的playbook会成功运行:

PLAY [localhost] **************************************************************************************************************************************************************************************************

TASK [安装MariaDB服务器] *****************************************************************************************************************************************************************************************
changed: [localhost]

TASK [更改默认MariaDB密码] ****************************************************************************************************************************************************************************************
changed: [localhost]

PLAY RECAP ********************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

现在需要密码:

root@ubuntu:~# mysql
错误1045 (28000):拒绝用户'root'@'localhost'(未使用密码)
root@ubuntu:~# mysql -p
输入密码:
欢迎使用MariaDB监视器。命令以;或\g结尾。
您的MariaDB连接ID为35
服务器版本:10.6.12-MariaDB-0ubuntu0.22.04.1 Ubuntu 22.04

版权所有(c)2000、2018年、甲骨文、MariaDB公司Ab及其他公司。

键入'help'或'\h'获取帮助。键入'c'以清除当前输入语句。

MariaDB [(none)]>
英文:

When connecting with no password, you need to connect over the mysql unix socket (/run/mysqld/mysqld.sock) instead of over a TCP connection. Compare the behavior of:

root@ubuntu:~# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 31
Server version: 10.6.12-MariaDB-0ubuntu0.22.04.1 Ubuntu 22.04

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> ^DBye

With:

root@ubuntu:~# mysql -h 127.0.0.1
ERROR 1698 (28000): Access denied for user 'root'@'localhost'

Rewrite your playbook to connect over the unix socket:

- hosts: vm-bd-01
  vars:
     mysql_root_password: password
  tasks:
  - name: Install MariaDB Server
    apt:
      pkg:
      - mariadb-server
      - python3-pymysql
      update_cache: yes
    become: yes
  - name: Change default MariaDB password
    become: yes
    mysql_user:
      login_unix_socket: /run/mysqld/mysqld.sock
      login_user: 'root'
      login_password: ''
      name: 'root'
      password: '{{ mysql_root_password }}'
      state: present

With this change, your playbook runs successfully for me:

PLAY [localhost] **************************************************************************************************************************************************************************************************

TASK [Install MariaDB Server] *************************************************************************************************************************************************************************************
changed: [localhost]

TASK [Change default MariaDB password] ****************************************************************************************************************************************************************************
changed: [localhost]

PLAY RECAP ********************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

And now a password is required:

root@ubuntu:~# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
root@ubuntu:~# mysql -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 35
Server version: 10.6.12-MariaDB-0ubuntu0.22.04.1 Ubuntu 22.04

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

huangapple
  • 本文由 发表于 2023年7月12日 23:25:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76672191.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定