Ansible Couldn’t Connect to the Database

Trellis, as of sometime in 2016, simplified the way users it generates the MySQL database and user:

  1. USER will be named example_com
  2. DATABASE will be named example_com_environment

If you, as I had done, end up changing site name in one place or another you may get a message that Ansible couldn’t connect to the database, check that MySQL is running and that wp-config.php exists.

One possible solution would be to create a user and database with granted permissions that match the above conventions, but probably you just want to check the Trellis group_vars/environment/ files and insure that the configurations are correct.

Login to the server and as root, connect to MySQL using the Vault.yml vault_mysql_root_password password.

MariaDB [(none)]> SELECT User FROM mysql.user;
+--------------------+
| User               |
+--------------------+
| root               |
| root               |
| root               |
| debian-sys-maint   |
| root               |
| example_com        |
+--------------------+

Notice there are four (4) root users. This is because Trellis (or maybe Ansible by default) creates root accounts at four different hosts: localhost, the ip address, 127.0.0.1 and something like 1::, which I’m not sure what that means.



MariaDB [(none)]> show databases; +----------------------------+ | Database | +----------------------------+ | information_schema | | mysql | | performance_schema | | example_com_staging | +----------------------------+ MariaDB [(none)]> SHOW GRANTS FOR 'example_com'@'localhost';; +---------------------------------------------------------------------------------------------------------------------------+ | Grants for staging_example_com@localhost | +---------------------------------------------------------------------------------------------------------------------------+ | GRANT USAGE ON *.* TO 'example_com'@'localhost' IDENTIFIED BY PASSWORD '*CB...' | | GRANT ALL PRIVILEGES ON `example_com_staging`.* TO 'example_com'@'localhost' | +---------------------------------------------------------------------------------------------------------------------------+ 2 rows in set (0.00 sec)