WordPress Cannot Connect to the Database After Changing the MySQL Password

WordPress Cannot Connect to the Database After Changing the MySQL Password

After changing the MySQL password, you also need to update wp-config.php in the WordPress root directory. During installation, WordPress writes the database connection details into this file. If the password for the MySQL account has changed but wp-config.php still contains the old password, WordPress will naturally be unable to connect to the database.

The common error is usually:

Error establishing a database connection

Handle it as follows:

  1. Go to the WordPress root directory and back up the configuration file first:
cp wp-config.php wp-config.php.bak
  1. Open wp-config.php and find the database configuration:
define('DB_NAME', 'database name');
define('DB_USER', 'database username');
define('DB_PASSWORD', 'database password');
define('DB_HOST', 'localhost');
  1. Change DB_PASSWORD to the current MySQL user's new password.
  1. If it still cannot connect, check whether DB_USER, DB_NAME, and DB_HOST are correct. Many hosts use localhost, but some servers require a specific database host address.
  1. If you can use the command line, test the MySQL login with the same username and password:
mysql -u database_username -p database_name

If you can log in, the MySQL account itself is usable. If login fails, check the MySQL user's password, permissions, or whether that user is allowed to connect from the current host.

WordPress has no way to automatically know that you changed the MySQL database password, so after changing the database password, be sure to update wp-config.php as well.

Leave a Reply