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:
- Go to the WordPress root directory and back up the configuration file first:
cp wp-config.php wp-config.php.bak
- Open
wp-config.phpand find the database configuration:
define('DB_NAME', 'database name');
define('DB_USER', 'database username');
define('DB_PASSWORD', 'database password');
define('DB_HOST', 'localhost');
- Change
DB_PASSWORDto the current MySQL user's new password.
- If it still cannot connect, check whether
DB_USER,DB_NAME, andDB_HOSTare correct. Many hosts uselocalhost, but some servers require a specific database host address.
- 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.
