geerlingguy / ansible-for-devops

Ansible for DevOps examples.
https://www.ansiblefordevops.com
MIT License
8.51k stars 3.48k forks source link

on Chapter3, Page 36: unable to connect to database, check login_user and login_password #578

Open kayg04 opened 8 months ago

kayg04 commented 8 months ago

On page 36 of the book, the command fails with the following error message. I am thinking mysql needs some more configuration before access is allowed?

$ ansible db -b -m mysql_user -a "name=django host=% password=12345 priv=*:*:ALL host=localhost state=present"
[WARNING]: Found both group and host with same name: db
db | FAILED! => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "msg": "unable to connect to database, check login_user and login_password are correct or /root/.my.cnf has the credentials. Exception message: (1698, \"Access denied for user 'root'@'localhost'\")"
}
kayg04 commented 8 months ago

I had to specify login_user and login_password to get it working. I do understand I had the wrong command with priv=*:*:ALL instead of priv=*.*:ALL but even with that corrected, it did not / does not work.

AndrewO commented 4 months ago

@kayg04 What do you use for login_user and login_password? I'm getting the same thing when I do ansible db -b -m mysql_user -a 'name=django host=% password=12345 priv=*.*:ALL state=present login_user=root login_password=""'

But if I SSH into the machine as root and run mysql I can get this:

MariaDB [(none)]> status
--------------
mysql  Ver 15.1 Distrib 10.5.25-MariaDB, for Linux (aarch64) using  EditLine wrapper

Connection id:          5
Current database:
Current user:           root@localhost
SSL:                    Not in use
Current pager:          stdout
Using outfile:          ''
Using delimiter:        ;
Server:                 MariaDB
Server version:         10.5.25-MariaDB MariaDB Server
Protocol version:       10
Connection:             Localhost via UNIX socket
Server characterset:    latin1
Db     characterset:    latin1
Client characterset:    utf8
Conn.  characterset:    utf8
UNIX socket:            /var/lib/mysql/mysql.sock
Uptime:                 10 min 0 sec

So it would appear root@localhost can login that way.

AndrewO commented 4 months ago

Just noticed in my example that when I logged in locally and connected it was using the Unix socket. So I did:

$ ansible db -b -m mysql_user -a "name=django host=% password=12345 \
   priv=*.*:ALL state=present login_unix_socket=/var/lib/mysql/mysql.sock

...and it worked. Thinking I might have messed up the firewall configuration since I can't seem to login to the db from my host machine.

badr-elmazaz commented 1 month ago

This command worked for me:

ansible db -b -m mysql_user -a "name=django host=% password=django \
   priv=*.*:ALL state=present login_unix_socket=/var/run/mysqld/mysqld.sock"

Credits to this stackoverflow thread.