Email This List Email This List Print This List Print This List

How to Fix the Error Estab­lish­ing a Data­base Con­nec­tion in Word­Press

How to Fix the Error Establishing a Database Connection in WordPress

If you have been surf­ing the web for a while, you have at least seen this error a few times. Error Estab­lish­ing a Data­base Con­nec­tion is one of those curses that could be caused by many reas­ons. As a Word­Press begin­ner, this could be awfully frus­trat­ing spe­cially when it happened on its own without you chan­ging any­thing. We ran into this issue yes­ter­day on our own site. It took a little over 20 minutes to detect and fix the prob­lem. While doing the research to find pos­sible causes, we real­ized that there was no good art­icle that covered everything. In this art­icle, we will show you how to fix the error estab­lish­ing a data­base con­nec­tion in Word­Press by com­pil­ing a list of solu­tions all in one place.

Note: Before you make any data­base changes, make sure you have suf­fi­cient backups.

Why do you get this error?

Well in short, you are get­ting this error because Word­Press is unable to estab­lish a data­base con­nec­tion. Now the reas­on why Word­Press is unable to estab­lish a data­base con­nec­tion can vary. It could be that your data­base login cre­den­tials are wrong or have been changed. It could be that your data­base serv­er is unre­spons­ive. It could be that your data­base has been cor­rup­ted. In our exper­i­ence, major­ity of the times this error hap­pens because of some sort of serv­er error how­ever there could be oth­er factors as well. Lets take a look at how to go about troubleshoot­ing this prob­lem.

Does the prob­lem occur for /wp-admin/ as well?

First thing you should do is to make sure that you are get­ting the same error on both the front-end of the site, and the back-end of the site (wp-admin). If the error mes­sage is the same on both pages “Error estab­lish­ing a data­base con­nec­tion”, then pro­ceed onto the next step. If you are get­ting a dif­fer­ent error on the wp-admin for instance some­thing like “One or more data­base tables are unavail­able. The data­base may need to be repaired”, then you need to repair your data­base.

You can do this by adding the fol­low­ing line in your wp-config.php file:

1 define('WP_ALLOW_REPAIR', true);

Once you have done that, you can see the set­tings by vis­it­ing this page:http://​www​.yoursite​.com/​w​p​-​a​d​m​i​n​/​m​a​i​n​t​/​r​e​p​a​i​r​.​php

WordPress Database Repair

Remem­ber, the user does not need to be logged in to access this func­tion­al­ity when this define is set. This is because its main intent is to repair a cor­rup­ted data­base, Users can often not login when the data­base is cor­rupt. So once you are done repair­ing and optim­iz­ing your data­base, make sure to remove this from your wp-config.php.

If this repair did not fix the prob­lem, or you are hav­ing trouble run­ning the repair then con­tin­ue read­ing this art­icle as you might find anoth­er solu­tion to work.

Check­ing the WP-Con­fig file

WP-Config.php is prob­ably the single most import­ant file in your entire Word­Press install­a­tion. This is where you spe­cify the details for Word­Press to con­nect your data­base. If you changed your root pass­word, or the data­base user pass­word, then you will need to change this file as well. First thing you should always check is if everything in your wp-config.php file is the same.

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

Remem­ber your DB_​Host value might not always be loc­al­host. Depend­ing on the host, it will be dif­fer­ent. For pop­u­lar hosts like Host­G­at­or, Blue­Host, Site5, it is loc­al­host. You can find oth­er host val­ues here.

Some folks sug­ges­ted that they fixed their prob­lem by repla­cing loc­al­host with the IP. It is com­mon to see this sort of issue when run­ning Word­Press on a loc­al serv­er envir­on­ment. For example on MAMP, the DB_​Host value when changed to the IP may seem to work.

1 define('DB_HOST', '127.0.0.1:8889');

IP’s will vary for online web host­ing ser­vices.

If everything in this file is cor­rect (make sure you check for typos), then it is fair to say that there is some­thing wrong on the serv­er end.

Check your Web Host (MySQL Serv­er)

Often you will notice this Error estab­lish­ing data­base con­nec­tion when your site gets swarmed with a lot of traffic. Basic­ally, your host serv­er just can­not handle the load (spe­cially when you are on shared host­ing). Your site will get really slow and for some users even out­put the error. So the best thing you should do is get on the phone or livechat with your host­ing pro­vider and ask them if your MySQL serv­er is respons­ive.

For those users who want to test if MySQL serv­er is run­ning your­self, you can do a few things. Test oth­er sites on the same serv­er to see if they are hav­ing the issue. If they are also get­ting the same error, then most def­in­itely there is some­thing wrong with your MySQL serv­er. If you do not have any oth­er site on this same host­ing account simply go to your cPan­el and try to access phpMy­Ad­min and con­nect the data­base. If you can con­nect, then we need to veri­fy if your data­base user has suf­fi­cient per­mis­sion. Cre­ate a new file called testconnection.php and paste the fol­low­ing code in it:

1 <?php
2 $link = mysql_connect('localhost', 'root', 'password');
3 if (!$link) {
4 die('Could not connect: ' . mysql_error());
5 }
6 echo 'Connected successfully';
7 mysql_close($link);
8 ?>

Make sure to replace the user­name and pass­word. If the con­nec­ted suc­cess­fully, then it means that your user has suf­fi­cient per­mis­sion, and there is some­thing else that is wrong. Go back to your wp-con­fig file to make sure that everything there is cor­rect (re-scan for typos).

If you can­not con­nect to the data­base by going to phpMy­Ad­min, then you know it is some­thing with your serv­er. It does not neces­sar­ily means that your MySQL serv­er is down. It could mean that your user does not have suf­fi­cient per­mis­sion.

In our case, our MySQL serv­er was run­ning. All oth­er sites on the serv­ers were work­ing fine except for WPBe­gin­ner. When we tried going to our phpMy­Ad­min, we ended up get­ting the error:

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

We got on the phone with Host­G­at­or and their sup­port quickly found the prob­lem. Some­how our user’s per­mis­sions were reset. Not sure how that happened, but appar­ently that was the reas­on. They went back in and restore the per­mis­sions and we were able to get the site back live.

So if you get the access denied error in either con­nect­ing to your phpMy­Ad­min or through testconnection.php res­ults, then you should con­tact your host right away to get them to fix it.

Solu­tions that Worked for Oth­ers

It is import­ant to note, that these may not work for you. Use at your own risk and make sure that you have suf­fi­cient backups if any­thing goes wrong.

Deepak Mit­tal said that his cli­ent was get­ting the error that data­base needs to be repaired. Even after repair­ing the data­base, the error did not go away. He tried vari­ous things and at the end, the issue was the site url. Appar­ently that was changed which caused the error to per­sist. He ran the SQL query by going to phpMy­Ad­min:

1 UPDATE wp_options SET option_value='YOUR_SITE_URL' WHERE option_name='siteurl'

Make sure to replace YOUR_​SITE_​URL with the actu­al url example: http://​www​.wpbe​gin​ner​.com. The wp_​options will be dif­fer­ent if you havechanged the default Word­Press data­base pre­fix.

This seemed to fix the issue for him and few oth­ers that com­men­ted on his post as well.

Sachin­um sug­ges­ted that he was able to con­nect the data­base with testconnection.php, so he changed the wp-config.php user to the root user. Word­Press star­ted to work per­fectly fine. Then he rever­ted the set­tings back to the data­base-user, and it con­tin­ued to work. He could not fig­ure out what was wrong, but con­cluded that it was a typo.

Cute­won­ders sug­ges­ted that they removed the con­tent of active_​plugins in wp_​options table and edited the con­tents of recently_​edited. Basic­ally that seemed to fix the prob­lem. Please their full response here.

We read on numer­ous sources that users simply uploaded a fresh copy of Word­Press and it fixed the error.

This is a really frus­trat­ing error. What have you tried that seemed to work for you? We would be happy to expand on this resource, so oth­ers do not have to waste as much time find­ing a solu­tion.

Related Post

admin has written 133 articles