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

t98cgbkg  于 2023-08-05  发布在  其他
关注(0)|答案(1)|浏览(119)

我有一个Ubuntu VM。但是,当我尝试通过Ansible更改MariaDB的默认密码(空密码)时,它说我没有这样做的权限。我通常可以使用sudo mysql -u root -p登录MariaDB。
我尝试设置**become:是的,但是没有用。
你能帮我吗?

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

字符串

错误

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'\")"}

smdnsysy

smdnsysy1#

当没有密码连接时,你需要通过mysql unix socket(/run/mysqld/mysqld.sock)而不是TCP连接。比较以下各项的行为:

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

字符串
与:

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


重写你的playbook以通过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


有了这个变化,你的剧本对我来说运行得很成功:

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


现在需要输入密码:

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)]>

相关问题