If you’ve recently attempted to reach your WordPress-based website and instead received a message stating “Error Establishing Database Connection“, the cause is most often one of the following:
- The database has crashed
- The database login credentials are incorrect in your WordPress configuration file
- The WordPress database tables have been damaged
We will walk through these issues one at a time to determine if they affect you, and how to fix them.
Step 1 — Try repairing the WordPress Database
Sometimes, due to a failed upgrade, a database crash, or a faulty plugin, your WordPress database can become corrupted. This problem can present itself as a database connection error. The first thing you can do is try repairing your database.
Additionally, WordPress provides a built-in functions to repair the database. It is disabled by default because it has no access controls and could be a security issue. Below, you will see how we will enable the feature, run the repairs, and then disable it to avoid this potential security threat.
Open the wp-config.php file. On any blank line, paste in the following:
This defines a variable that WordPress looks for when determining if it should enable the repair feature.
Save and close the file. Switch over to your browser and load the following address, being sure to substitute with your site’s domain.
A database repair page will load. Press the Repair Database button, and you’ll be taken to a results page where you can see the checks and repairs happening in real-time.
Once the process finishes, be sure to open up the wp-config.php file again and remove the line we just pasted in.
Did you notice any repairs being made? Try your site again, and check whether the error is gone.
Step 2 — Check Database Login Credentials
If you’ve just moved your WordPress site between servers or hosting providers, you might need to update your database connection details. These are stored on the server in a PHP file called wp-config.php.
Now use your favorite text editor to open the configuration file. Locate the database connection information:
/** The name of the database for WordPress */ define('DB_NAME', 'database_name'); /** MySQL database username */ define('DB_USER', 'database_username'); /** MySQL database password */ define('DB_PASSWORD', 'database_password');
Check that these three variables are correct based on your records. If they don’t look right, update as appropriate, save and exit.
If the steps above didn’t solve your issue, you might need to restore the database from a backup if you have one available.
If you’re a ChemiCloud customer, you can rest easy! All our WordPress hosting plans come with 30 day daily offsite backups allowing to be quickly restored.