WordPress: How to Fix “Error in Establishing a Database Connection”

If you’ve been browsing the web for some time, at least you’ve seen this error a few times. Error Establishing a database connection is one of those curses that can be caused by several reasons. As a beginner in WordPress, this can be terribly frustrating, especially when it happened alone, without you changing anything. We came across this issue yesterday on our own website. It took a little over 20 minutes to detect and fix the problem. When doing the research to find possible causes, we realized that there was not a good article that covered everything. In this article, we’ll show you how to fix the error by establishing a database connection in WordPress by compiling a list of solutions in one place.

Note: Before making any changes to the database, make sure you have sufficient backups.

Why do you get this error?

Well, in short, you’re getting this error because WordPress cannot establish a connection to the database. Now, the reason WordPress cannot establish a connection to the database can vary. It may be that your database login credentials are incorrect or have been changed. It may be that your database server is not responding. It may be that your database is corrupted. In our experience, most of the time this error occurs due to some kind of server error, but there may also be other factors. Let’s take a look at how to solve this problem.

Does the problem occur for /wp-admin/ too?

The first thing you should do is make sure you are getting the same error on the front-end of the website and on the back-end of the site (wp-admin). If the error message is the same on both pages “Error establishing a database connection,” proceed to the next step. If you are getting a different error in wp-admin, for example, 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. Add before “All, stop editing! Happy blogging line wp-config.php.

define(‘WP_ALLOW_REPAIR’, true);

After you do this, you can see the settings by visiting this page:

WordPress: How to Fix "Error in Establishing a Database Connection" 1

Remember, the user does not have to be logged in to access this feature when this setting is set. This is because their main intention is to repair a corrupted database, users often can not log in when the database is corrupted. So when you’ve finished repairing and optimizing your database, be sure to remove it from your wp-config.php file.

If this repair does not fix the problem, or if you are having trouble performing the repair, continue reading this article as you may find another solution to work.

Checking the WP-Config File

wp-config.php is probably the most important file in your entire WordPress installation. This is where you specify the details of WordPress to connect to your database. If you have changed your root password or database user password, you will also need to change this file. 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 that your DB_Host value is not always localhost. Depending on the host, it will be different. For popular hosts like HostGator, BlueHost, Site5, it is localhost. You can find other host values here.

Some people have suggested that they have fixed their problem by replacing localhost with IP. It is common to see this type of problem when running WordPress in a local server environment. For example, in MAMP, the value DB_Host when changed to IP may seem to work.

define(‘DB_HOST’, ‘’);

IPs vary according to web hosting services online.

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

Check your web host (MySQL server)

Often, you’ll notice this error when establishing a connection to the database when your site is overrun by too much traffic. Basically, your host server simply can not handle the load (especially when you’re on shared hosting). Your site will be very slow, and for some users, it will generate the error. So the best thing you should do is to call or watch your hosting provider and ask if your MySQL server is responsive.

For those users who want to test if the MySQL server is running, you can do some things. Test other sites on the same server to see if they are having the problem. If they are also getting the same error then there is definitely something wrong with your MySQL server. If you do not have any other website in this same hosting account just go to your cPanel and try to access phpMyAdmin and connect to the database. If you can connect, then we need to verify that the user of your database has sufficient permission. Create a new file called testconnection.php and paste the following code into it:

$link = mysql_connect(‘localhost’, ‘root’, ‘password’);
if (!$link) {
die(‘Could not connect: ‘ . mysql_error());
echo ‘Connected successfully’;

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

If you can not connect to the database by accessing phpMyAdmin, you’ll know it’s something from your server. This does not necessarily mean that your MySQL server is down. This may mean that your user does not have sufficient permission.

In our case, our MySQL server was running. All other sites on the servers were working fine except for WPBeginner. When we try to go to our phpMyAdmin, we end up getting the error:

# 1045 – Access denied for user ‘foo’ @ ‘%’ (using password: YES)

We got on the phone with HostGator and their support quickly encountered the problem. Somehow, our users’ permissions have been reset. I’m not sure how that happened, but apparently that was the reason. They went back and restored the permissions and we were able to recover the site.

Therefore, if you get the access denied error when connecting to your phpMyAdmin or through testconnection.php results, you should contact your host immediately so that it corrects it.

Solutions that worked for others

It is important to note that they may not work for you. Use at your own risk and make sure that you have enough backups if something goes wrong.

Deepak Mittal said his client was getting the error that the database needs to be repaired. Even after repairing the database, the error has not disappeared. He tried several things and in end question was the URL of the site. Apparently, this was changed, which caused the persistence of the error. He ran the SQL query going to phpMyAdmin:

UPDATE wp_options SET option_value=’YOUR_SITE_URL’ WHERE option_name=’siteurl’

Replace YOUR_SITE_URL with the actual URL example:
The wp_options will be different if you have changed the default prefix of the WordPress database.

This seemed to solve the problem for him and a few others who also commented on his post.

Sachinum suggested that he was able to connect the database with testconnection.php, so he changed the user wp-config.php to the root user. WordPress started working perfectly well. Then it reverted the settings back to the database user and it continued to work. He could not figure out what was wrong, but concluded it was a typo.

The cutongonders suggested that they removed the contents of active_plugins in the wp_options table and edited the contents of recent_edited. Basically, this seemed to solve the problem.