WordPress says “Error establishing a database connection”
WordPress shows Error establishing a database connection on every page. MariaDB is down or the database user or password in wp-config.php differs from what MariaDB has, and two commands show which.
Symptoms
Every page of the site shows Error establishing a database connection.
Cause
WordPress could not connect to MariaDB using DB_NAME, DB_USER, DB_PASSWORD and DB_HOST from wp-config.php.
MariaDB may be down. The database user or password in wp-config.php may also differ from what MariaDB has, as can happen after you copy a site from another server.
Resolution
Try connecting with the values from
wp-config.php.mariadb -u example_wp -p example_wpIf the database user or password is wrong, the command prints this error.
Output ERROR 1045 (28000): Access denied for user 'example_wp'@'localhost' (using password: YES)If MariaDB is down, the command prints this error on AlmaLinux.
Output ERROR 2002 (HY000): Can't connect to local server through socket '/var/lib/mysql/mysql.sock' (2)If MariaDB is down, check its state and start it.
urbestack-service-list --name mariadb urbestack-service-restart --name mariadbIf the password is wrong, list the customer's database users and their grants.
urbestack-database-user-list --owner USERSet a new password for the database user. The command reads the password from standard input.
urbestack-database-user-set --name example_wp --password-stdinPut the same password in
DB_PASSWORDinwp-config.php. The value ofDB_HOSTislocalhost.Run the connection command again with the values from
wp-config.phpto check that it connects. If it still prints one of the errors above, follow the steps for that error.