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

How to Fix the Error Establishing a Database Connection in WordPress

Last updated on by
Follow WPBeginner on YouTube
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:

define('WP_ALLOW_REPAIR', true);

Once you have done that, you can see the settings by visiting this page: http://www.yoursite.com/wp-admin/maint/repair.php

WordPress Database Repair

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', '127.0.0.1:8889');

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:

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

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: http://www.wpbeginner.com. 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 »

Comments

  1. Scott says:

    Sort of silly looking back at this, but it might help someone else so I think it’s worth sharing. Recently had a site that was set up to use Cloudflare as it’s CDN. All the DNS “A” records were set except for the one for “mysql.domain.com”, so even using the testconnection.php trick didn’t work (on Dreamhost that doesn’t use localhost). Simply added the “A” record in Cloudflare and life was good again.

  2. Sharad Gupta says:

    An awesome solution for a very difficult and annoying problem .

  3. MaSilat says:

    Awesome. It helped me and solve DB problem.

  4. Mohamed Azazy says:

    You can also restart your MySQL service if you’re using VPS.
    – sudo service mysql restart

    Worked for me!

  5. Michael D says:

    Thanks for this article, it helped!

    We had the error occurring in both the root domain and wp-login. Changing ‘localhost’ to the IP in wp-config.php fixed it.

  6. Rav says:

    It appeared my ‘localhost’ was not working so I just added the direct address of my hosting provider (mysql.hostinger.co.uk) and everything started working again!

    Thanks for the help!

  7. Christopher says:

    Actually, I’m getting “Error establishing a database connection” in my home page only. All other pages are working fine. Please let me know what to do.

  8. Adrienne says:

    My old client has this issue now, and her site is about 3 years old and hasn’t been updated. I think the theme and all the changes has corrupted this site. It needs to be scrapped and redone…. now, I have to find a simple way to do it. lol

  9. Nicole says:

    I was moving a multisite system- I had to switch out the old website name in my database, and in the .htaccess and wp-config files

  10. m3d01 says:

    Hello guys,

    I found the problem that my hosting service made update and that lead to change in the DB_HOST number so i change it back by the new one by edit the config file and the site is back again to work finally :)

  11. John Bert says:

    Recently I have transferred from Arvixe to Phi9 web host.
    Actually I forgot to change the previous host “MySQL Connections Strings” in wp-config.php. Now it’s working.

    Thank you for your great help I really appreciate it! :)

  12. Micky says:

    I have seen this error. I was putting the wrong MySQL port in connection line. Your post helped me fix it. :)

  13. Carl says:

    This seems logical. But I go through each step and nothing changes. Why is this so complex?
    I liked the idea of the database repair but it appears that WordPress would need to be up and running to hit the Repair Database button. I’ve been at this for 2 days. Is there a place to go where I can find the problem by looking at some code? This all seems so vague.

  14. Leonard Rattini says:

    As I worked in computer application design for 30-years, and I’m also registered as a Certified Computer Professional, many people, to impress, use the “database” term incorrectly.

    Database is a different data retaining methodology than what was use before its time. Back in the earlier days, data was retained on liked related 80-column cards and collectively they were know as a file. For instant many invoice records, collectively were known as the Invoice File. Medical records collectively, were known as a Medical file, etc.

    In early big main frame computer days, before PC’s that came along, they processed these “80-column fixed-records.” With the advent of the Internet and distributed multiple keyboards, data then was recorded as “data packets” and they are stored in a “database,” which is a three-dimensional format structure stored in the computer. Then at the right time, to process, data items needed “linked’ data packets then allowed for this to happen which then looked like a record we are familiar with.

    An application must be designed as a database base application that includes a Database Administrator person, otherwise its application is not a database application.

    A database application served two main purposes versus the old file methods used in the olden days before the Internet and distributed keyboards. One was to capture data items at first point of (data) entry. And secondly, it is intended to eliminate “data-redundancy.” Leonard Rattini, CCP (to know of my CCP credentials, google ICCP)

  15. Toby Harding says:

    I’ve seen this error a few times now, with my WordPress that is hosted on an Amazon EC2 box in their AWS cloud. Simple way to fix the issue is to restart the instance from the AWS admin console.

  16. Jake B says:

    Thanks for the article. I was able to work through each step until I found my issue.

  17. Unknown says:

    Hey, i am getting this error while installing wordpress “Error establishing a database connection” what should i do to get rid of this error .I’ve tried everything like changed database name , deleted previous databases but still wordpress is not is not installed yet. Please help !

  18. David Clarke says:

    Solved my issue in minutes thanks to this helpful article, thanks a lot!

  19. Friday says:

    I can’t help it, since i created my site i haven’t touched that area can you help me to do it

  20. mikhail says:

    Thanks for the article, it helps.
    For me it’s because I change my database user password from CPanel but apparently the wpconfig.php file won’t change automatically when i changed my database user password .

    I have to change it manually in
    define(‘DB_PASSWORD’, ‘[insert your password here]’);

  21. Max Demian says:

    Really useful, thank you.

  22. James Kalmakoff says:

    The article was very useful and I resolved my Database connection problem. A further place to look for errors is in the MySQL database. Be sure that the ‘database_username’ and the ‘database_name’ are correctly assigned to each other and that there are no typos!

  23. Dawn says:

    I just got this error, I copy and Pasted a page to a word document, I saw that there was a paragragh added with ads and links to sites like viagra ect. I removed the ad/links paragragh and re-pasted the text back into my webpage. I was on the second page and posted the updates, when I received this error…any one else had this?

  24. Peter Gough says:

    Many thanks for this article. Very, very useful.

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.