Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
WordPress Plugins
View all Guides

How to Fix the Error Establishing a Database Connection in WordPress

Last updated on by
Special WordPress Hosting offer for WPBeginner Readers
How to Fix the Error Establishing a Database Connection in WordPress

If you have been surfing the web for a while, you have at least seen this error a few times. Error Establishing a Database Connection is one of those curses that could be caused by many reasons. As a WordPress beginner, this could be awfully frustrating specially when it happened on its own without you changing anything. We ran into this issue yesterday on our own site. It took a little over 20 minutes to detect and fix the problem. While doing the research to find possible causes, we realized that there was no good article that covered everything. In this article, we will show you how to fix the error establishing a database connection in WordPress by compiling a list of solutions all in one place.

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

Why do you get this error?

Well in short, you are getting this error because WordPress is unable to establish a database connection. Now the reason why WordPress is unable to establish a database connection can vary. It could be that your database login credentials are wrong or have been changed. It could be that your database server is unresponsive. It could be that your database has been corrupted. In our experience, majority of the times this error happens because of some sort of server error however there could be other factors as well. Lets take a look at how to go about troubleshooting this problem.

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

First thing you should do is to make sure that you are getting the same error on both the front-end of the site, and the back-end of the site (wp-admin). If the error message is the same on both pages “Error establishing a database connection”, then proceed onto the next step. 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. Add it just before ‘That’s all, stop editing! Happy blogging’ line wp-config.php.

define('WP_ALLOW_REPAIR', true);

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

Repair database in WordPress

Remember, the user does not need to be logged in to access this functionality when this define is set. This is because its main intent is to repair a corrupted database, Users can often not login 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 repair did not fix the problem, or you are having trouble running the repair then continue reading this article as you might find another solution to 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 your database. If you changed your root password, or the database user password, 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.

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, Site5, it is localhost. You can find other host values here.

Some folks suggested that they fixed their problem by replacing localhost with the IP. It is common to see this sort 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', '');

IP’s will vary for online web hosting services.

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

Check your Web Host (MySQL Server)

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

For those users who want to test if MySQL server is running yourself, you 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 most 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 in it:

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

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 means that your MySQL server is down. It could 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 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 apparently that was the reason. They went back in and restore 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.

Solutions that Worked for Others

It is important to note, that these may not work for you. Use at your own risk and make sure that you have sufficient backups if anything goes wrong.

Deepak Mittal said that his client was getting the error that database needs to be repaired. Even after repairing the database, the error did not go away. He tried various things and at the end, the issue was the site url. Apparently that was changed which caused the error to persist. He ran the SQL query by going to phpMyAdmin:

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

Make sure to replace YOUR_SITE_URL with the actual url example: The wp_options will be different if you have changed the default WordPress database prefix.

This seemed to fix the issue for him and few others that commented on his post as well.

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

Cutewonders suggested that they removed the content of active_plugins in wp_options table and edited the contents of recently_edited. Basically that seemed to fix the problem. Please their full response here.

We read on numerous sources that users simply uploaded a fresh copy of WordPress and it fixed the error.

This is a really frustrating error. What have you tried that seemed to work for you? We would be happy to expand on this resource, so others do not have to waste as much time finding a solution.

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »


  1. Bastien says:

    This error can also be triggered because of a DOS XMLRPC attack :

  2. Tejas Waghmode says:


    I’m confuse, how to solve the problem?

  3. Zoe says:

    Hello, I’m new to wordpress and need some more help.
    My wp-admin page shows the “One or more database tables are unavailable. The database may need to be repaired” message…. so I know I need to add that bit of code. But how and where do I add it? I can’t login… I do not know what address to type in to access this php file. Help please :)
    My website is

  4. Pavanswin says:

    I am a wp beginner, you guys are awesome, helped me building my careers through this kind of articles. I migrated my website to and struggled with the error for many days and finally found a solution in the article and is very helpful. thank you all

  5. Julio Sanchez says:

    The original name of the file wp-config is wp-config-sample, I just wrote the original name and vuala no “Error establishing a database connection” message.

    • WPBeginner Support says:

      wp-config-sample.php file is a sample file not the actual configuration file. The actual configuration file is wp-config.php.

  6. Sruly says:

    I have WordPress with Digital Ocean. I just restarted my server and that fixed the issue.

  7. Rudy says:

    Thanks, really thanks

  8. Nicholas says:

    If all fails, also try a swap file:

  9. Brendan says:

    “replacing localhost with the IP”

    This worked! Thank you so much for saving my ass

  10. Guy Pribyl says:

    Is there a way to have wordpress when posting this error message do a redirect to a non-wordpress page? I JUST WANT A BACK UP FOR WHEN THIS HAPPENS IN THE MIDDLE OF THE NIGHT.

    Thanks for your help

  11. Avioconsys says:

    Thanks Syed Balkhi and his team. It took me 5 minutes to solve the issue with this error. It was a bad update of the password for the general user.

    Fast and working. Very nice of you sharing this information.


  12. Suhel says:

    i am getting the different error but its something some like this,
    wpsa_options: Table ‘aamtechn_wp949.wpsa_options’ doesn’t exist
    wpsa_postmeta: Table ‘aamtechn_wp949.wpsa_postmeta’ doesn’t exist

    i am getting this message and i am not able to access the dashboard.please help me soon.

  13. Camaal Mustafa Sikander says:

    Thank you so much, this one fixed the Database error for me in the first step itself.

    Best wishes,

  14. Dudley Rees says:

    Thank you for your help. The username and password part of wp-config.php fixed it for me.

  15. Robert Varga says:

    I had this problem, and I spent 2 much hours on fixing it … Finally I solve that mistery. My problem was in htacces file, it only had Rewrite Engine line , I just c/p this “default” htaccess content in it and replace it with file on server, and tadaam it work now :D

    “default” htaccess content :

    # BEGIN WordPress

    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ – [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]

    # END WordPres

  16. Daniel M says:

    how do i solve depreciation errors for db connection using either PDO or MYSQLi? please help

  17. prince vashisht says:

    I changed the username (wp_users) from phpmyadmin and now I am getting the same issue.Reverting the changes is also not helping me out.Please help.

  18. Wayne says:

    When I first experienced this error I was so devastated and I wonder how will I resolve this issue. I did some research and noticed that my database name was the problem. So I went into MYSQL and change the database name. This article explains it all

  19. Kevin Wells says:

    For whatever reason, I ended up having to recreate my MySQL user in the database. After I did that, my website functioned normally again.

  20. Christine F says:

    Thanks so much for this!

    I encountered the same issue earlier. I changed the database password and then this error appeared. We have GoDaddy so what I did was just go to the File Manager, open the WP-Config.php and changed the (‘DB_PASSWORD’, ‘database-password’); to the one I recently used. Real simple but wouldn’t have done it without this article.

    Thanks again!

    • Matthew says:

      Thank you for this reply, Christine. I, too, changed the password and found that to fix the issue as well. Perhaps MySQL had reset it without notifying me.

  21. Alexander says:

    In my case I solved the problem just updating the password of the DB.
    It seems the password was to weak.


  22. sonu arora says:

    hey i m facing a weird issue with my website, i.e. when i try to check in pingdom then the screenshot generated there shows me data connection error, while when i am opening my website in normal tab or iphone, it gets open. could you please suggest me what to do? i am worried whether my website is showing to public or not. url is thanks in advance!

    • WPBeginner Support says:

      Try changing server locations in Pingdom test settings. Also try other tools like Google Page Speed tool.

  23. Angel says:

    You say to put this in your wp-config.php file….. define(‘WP_ALLOW_REPAIR’, true);
    you don’t say a single word as to where to put it. I dropped it in right at the top and got a HUGE WP error message. You had it on line 1 without anything to indicate context of where to put it. So most would assume it would go on line 1 at the top. If it doesn’t go there, why not give helpful and specific instructions?

    • WPBeginner Support says:

      You need to add this just before this line:

      /* That's all, stop editing! Happy blogging. */

      Thanks for bringing this to our attention. We have updated the article.

  24. Nica says:

    when i try this:

    I get an error:
    Could not connect: Lost connection to MySQL server at ‘reading initial communication packet’, system error: 95 “Operation not supported”

    When i test this to php:

    It shows:
    This site can’t be reached

    The connection was reset.

  25. samchief says:

    Problem resolved. DB_host was the problem on ipage, it’s quite different from others

  26. RINKU MEHAR says:

    how could i solve this errors:

    Warning: require_once(C:\xampp\xampp\htdocs\wordpress\wp-admin\admin.php): failed to open stream: No such file or directory in C:\xampp\xampp\htdocs\wordpress\wp-admin\index.php on line 10

    Fatal error: require_once(): Failed opening required ‘C:\xampp\xampp\htdocs\wordpress\wp-admin\admin.php’ (include_path=’.;C:\xampp\xampp\php\PEAR’) in C:\xampp\xampp\htdocs\wordpress\wp-admin\index.php on line 10

  27. FranciscoMary Aghogho says:

    Please I need an assistance here. I tried changing my site’s url from http to https and now cannot view my site. It say a coonection could not be established. Please what could I do in this instance. Thanks.

    • sourcebreak says:

      If you update from php 5.2 to 5.4 or 5.5 then you will get this error database connection error in wp.

      If your password is Pre mysql 4.0 then you will get this error

      Login to phpmyadmin >> user >> change password
      type your mysql password >> select >> MySQL 4.1+ compatible and go

  28. Kanif says:

    I was also facing this issue. I googled it and come to this post. It is really awesome, deep thinking on what all problems may come with this error.
    Mine problem is resolved by doing database repair and optimization.

    Thanks buddy,

  29. ScienceMan says:

    Thanks for the great post. My situation was interesting, I migrated my server to another account with the same provider. The result was the dreaded “Error Establishing a Database Connection”. Thanks to this article, I knew to have a look at the wp-config.php file.

    When I logged into my new server cPanel and checked the MySQL databases, I immediately spotted the problem. Due to the server migration, the name of the database and user had changed. The databases names at my provider all default to “accountname_databasename”. Also, the database username defaults to “accountname_username”. I suspect other servers are similar.

    Since my server account name changed with the server migration, so did the name of all my databases and database usernames.

    So to fix it, all I did was change these two lines in the wp-config,php file:

    /** The name of the database for WordPress */
    define(‘DB_NAME’, ‘XXXXXX_databasename’);

    /** MySQL database username */
    define(‘DB_USER’, ‘XXXXXX_username’);

    Basically, you want to check carefully to make sure the “XXXXXX” in the wp-config.php file matches what’s at your server.

    Thanks again!

  30. igwe simon says:

    I mistakenly changed my site and wordpress url to one of my domain so i had to go and change the dns and park it on the website, so i waited for some hours but the domain was still propagating then i read a tutorial ( and followed an option there by editing my functions.php and the next thing i saw was “One or more database tables are unavailable. The database may need to be repaired.”, so i followed the instructions here and still the problem is still active. I need an assistance please.

  31. hafsa munir says:

    I have following error in my xammp server,how can i resolve it
    he following error was encountered while trying to retrieve the URL: localhost/phpmyadmin/

    Unable to determine IP address from host name .localhost

    The DNS server returned:

    Name Error: The domain name does not exist.
    This means that the system was not able to resolve the hostname presented in the URL. Check if the address is correct.

  32. val girich says:

    I am seeking free videos on how to use W.P. once it is set up ready to edit, ad docs and so on.

  33. S says:

    i done everything as you mentioned in the post but when i opened wp-admin/ then again this error was coming “One or more database tables are unavailable. The database may need to be repaired.”
    So plz help me what to do

  34. Manny says:


    I just logged in through FileZilla and checked out the wp-config.php file and double checked everything and realized that I updated my database password but it still showed the previous default password on wp-config.php. So after changing it in the file and saving the changes I was able to access my site again. Your help allowed me to solve my problem, thank you!

  35. Jeremy Smith says:

    One very important that’s been left out in this post is the “turn it off and turn it back on again” approach.

    If you haven’t actually changed the wp-config file I don’t think you should be going in and fiddling around with it, at least not in the first instance.

    What you should do first is attempt to restart the mysql server.

    To do this (In Terminal)
    1. SSH into your website (you will need your key ‘.pem’ file)*
    2. To restart your mysql type “sudo service mysqld restart”

    This has worked every-time for me.

    *On a Mac
    1. Get your ‘.pem’ file. I got mine from Amazon Web services (hosting the site)
    2. Enable SSH on your Mac – Google it
    3. Open Terminal
    4. Change directory to where you have stored your ‘.pem’ file. -It’s probably easiest to save this on your desktop, then you can just type ‘cd Desktop’ and it should take you there.
    5. Type ‘chmod 400 yourpemfilename.pem (allows you to use this key to get in)
    6. Type ‘ssh -i “yourpemfile.pem” remote_username@remote_host – type the speech marks here
    7. Once logged in (You may have to type y a few times to get you in) type
    sudo service mysqld restart

  36. Malkesh says:

    Hi, my wordpress site gets this error very often,
    we are using AWS, so i need to run this command to restart mysql :

    sudo restart mysqld restart

    the same issue i have also on digital ocean too..

    so i am sure not this is not an server issue, it’s should be some thing in wordpress, that’s put down mysql server..


    • dino says:

      Same problem here. At least two times per day my site , which is in digital ocean , loose sql connection.
      I have try to optimize my database, bvut nothing works. Any idea?

  37. Glenn says:


    When WordPress dies it is scary for a WordPress newbie. I found your solution easy to understand, implement and worked a treat.

  38. Faiza says:


    This blog was very helpful but I still haven’t been able to figure out this problem. My WP-Config.php file shows me this localhost:

    I am using GoDaddy, I have tried replacing this with local host, my IP address, and (godaddy). Nothing has worked :( Can I get some help here?

  39. Claire says:

    would the problem be that my domains dns settings haven’t updated yet?

  40. Anupam says:

    Updating the WP-Config file works for me. Thanks a lot.

  41. Brandon says:

    WOW!!!! Came across this site because my wife’s business site went down and I was able to fix it in minutes! Thank you!

  42. Cidina says:

    Thank you soooo much. This post saved my 6-month large project in WordPress. Trying to switch directories I lost connection to the database. I’m not a .php programmer but the topic is so well explained here that it took me five minutes to fix it after finding this blog. Your are awesome!!!

  43. Nino F says:

    If you changed your password in phpmyadmin, you have to change it in your wp-config.php aswell, if you don’t do that the website doesn’t work. You have to link the two with eachother.

  44. Eddie O'Hagan says:

    I was getting this error I think because of the theme I had, I changed the theme and I haven’t gotten the error since, I noticed this because when I ran the repair, the comments tables were the ones that had the errors.

    • dino says:

      how often did you have this problem?
      I have the same , and i loose connection at least twice per day.
      I tried many things but nothin works. If it is the theme i will try it..

  45. samuel nmeje says:

    this was indeed helpful. thanks a lot.

  46. Ram says:

    if you use xampp for wordpress offline then the error is removed by following process:
    open in Xampp folder “\xampp\phpMyAdmin\” and then check username and password
    set the same password to wp-config then the error is not appear

  47. Micah says:

    This is very helpful. Thank you very much!

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.