How to Fix the Error Establishing a Database Connection in WordPress

Database Connection Error

Written by - Masood

August 7, 2017

This error message is clear that your website cannot connect to the database. However, solving this error can be tricky for beginners. Usually, this occurs when a user has entered or made their database credentials (database host, database username, and database password) invalid. Sometimes your database server could be unresponsive, or your database may have corrupted. However, mostly it is incorrect database login credentials.

Does the problem occur for /wp-admin/ as well?

The first thing you should do is ensure you are getting the same error on both the front end of the website and the back end of the website (WordPress Dashboard). If the error message is the same on both pages “Error establishing a database connection, ” follow the instructions below. If you are getting a different error on the wp-admin, for instance, something like “One or more database tables are unavailable. The database may need to be repaired”, then you need to repair your database.

You can do this by adding the following line in your wp-config.php file.

define('WP_ALLOW_REPAIR', true);


Once you have done that, you can see the settings by visiting this page:

Repair Database

Remember, the user doesn’t need to be logged in to access this functionality when this definition is set. This is because its main intent is to repair a corrupted database; Users can often not log in when the database is corrupt. So once you are done repairing and optimizing your database, make sure to remove this from your wp-config.php.
If this did not fix the issue, or you are having trouble running the repair then you should continue reading this article as you might find another solution that will work.

Checking the WP-Config file

WP-Config.php is probably the single most important file in your entire WordPress installation. This is where you specify the details for WordPress to connect to your database. If you changed your root password or the database user password, then you need to change this file as well. The first thing you should always check is if everything in your wp-config.php file is the same.

define('DB_NAME', 'database-name');
define('DB_USER', 'database-username');
define('DB_PASSWORD', 'database-password');
define('DB_HOST', 'localhost');

Remember your DB_Host value might not always be localhost. Depending on the host, it will be different. For popular hosts like HostGator, BlueHost, and Site5, it is localhost. Some experts suggested that they fixed their problem by replacing localhost with the IP. It is common to see this kind of issue when running WordPress on a local server environment. For example on MAMP, the DB_Host value when changed to the IP may seem to work.

define('DB_HOST', '');

IPs will vary for online web hosting services.

If everything in this file is correct (make sure you check for typos), then it’s good to say that there is something wrong on the server end.

Check your Web Host (MySQL Server)

Often you will notice an Error in establishing a database connection when your site gets swarmed with a lot of traffic. Your host server just cannot handle the load (especially when you are on shared hosting). Your site will get too slow and for some users even output the error. So the best thing you should do is get on the phone or live chat with your hosting provider and ask them if your MySQL server is responsive.

Those who want to test if the MySQL server is running by themselves can do a few things. Test other sites on the same server to see if they are having the issue. If they are also getting the same error, then definitely there is something wrong with your MySQL server.

If you do not have any other site on this same hosting account simply go to your cPanel and try to access phpMyAdmin and connect the database. If you can connect, then we need to verify if your database user has sufficient permission. Create a new file called testconnection.php and paste the following code into it:

Make sure to replace the username and password. If the connected successfully, then it means that your user has sufficient permission, and there is something else that is wrong. Go back to your wp-config file to make sure that everything there is correct (re-scan for typos).

If you cannot connect to the database by going to phpMyAdmin, then you know it is something with your server. It does not necessarily mean that your MySQL server is down. It could mean that your user does not have sufficient permission.

Running MySQL Server

In our case, our MySQL server was running. All other sites on the servers were working fine except for WPBeginner. When we tried going to our phpMyAdmin, we ended up getting the error:
#1045 – Access denied for user ‘foo’@’%’ (using password: YES)

We got on the phone with HostGator and their support quickly found the problem. Somehow our user’s permissions were reset. Not sure how that happened, but that was the reason. They went back in and restored the permissions and we were able to get the site back live.
So if you get the access denied error in either connecting to your phpMyAdmin or through testconnection.php results, then you should contact your host right away to get them to fix it.

You May Also Like…