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: http://www.yoursite.com/wp-admin/maint/repair.php

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.



We had our wordpress installed on AWS EC2 and we got the “Error establishing database connection” for all our sites when the database was installed on the same server as the web server. When the database was moved to Amazon RDS for each of our sites, that error did not recur.
Hi!
This helped me a lot with a WP installation going bad. Thanks!
Greetings.
I have recently experienced the same issue when moving my multi-site from a subdirectory to root. Even after going through all the steps outlined in various tutorials, I encountered this dreaded message and nothing I did seemed to work.
What did work for me was this: In cPanel, go to your softaculous script installer, select the edit icon for your WordPress installation, change the paths that point to your install.
Once I did this and saved, then navigated to my site URL, it was back up and running smoothly.
Just wanted to leave this here in case it helps someone else along the way.
Thanks man, it work on ngnix server due to error establishing.
I have an issue with database connection. I wanted to create a new database for a new website #2 connected in multisite in OVH, and when wordpress asked me to enter login, password etc I entered those of website #1 as I had to do BUT without changing “wp_” for “wp_2″… And now both of these two sites have the message “Error establishing a database connection”. I deleted all FTP WP for website #2 and deconnected from multisite, but impossible to get back and to enter in website #1!! Do you know how to resolve this problem?
I’m having an issue with my website, my WordPress database just seams to disappear. logged into MySQL and nothing exist there anymore. has anyone ever experienced this? anyone with a solution?
The cause of this issue when I had it was so many people running hacking programs against my login page that they were impacting the server. It happened every Friday evening through Sunday evening.
We eliminated the issue by protecting that page with a pop-up so they couldn’t run their automated hacking programs against the login page.
Thank you for posting this–it saved a bit of a panic when the company’s WP blog disappeared behind this error. After verifying that the database was still there and undamaged, I called our host.
Turns out they’ve been migrating customers’ web sites to new and improved servers, and they somehow lost the connection info. Hopefully it will be repaired today.
But a very informative article, and a quick reminder (for me) of how to check in on mySQL to make sure everything is still good. Thanks!
i cant create pages, new post, or edit my themes or even upload pictures i constantly get the following error…
This page isn’t working.
didn’t send any data.
ERR_EMPTY_RESPONSE
the website setup is new but am faced with this problem
Thanks. Helped me debug a DB backend issue.
How can i find my wordpress database username and password?
By Mistake, i have deleted wp-config.php file and now i have uploaded new files.
I am Using Godaddy’s windows server…
I am getting below error:
This either means that the username and password information in your wp-config.php file is incorrect or we can’t contact the database server at . This could mean your host’s database server is down.
Are you sure you have the correct username and password? Are you sure that you have typed the correct hostname? Are you sure that the database server is running? If you’re unsure what these terms mean you should probably contact your host. If you still need help you can always visit the WordPress Support Forums.
Please help me as I have wasted by complete week…
Thank you..
Hey Jitesh,
You can login to your hosting control panel and locate the MySQL database section. Most likely, you will be able to find the database name, username, and password information there. Alternately, you can contact support to help you locate that information.
Thanks so much, all, for maintaining such a great website! You guys continue to bail me out with problems, issues & fixes for those of us who just enjoy writing & posting and not dealing with all of the WordPress programming stuff. Great work!
Cheers!!!!
Thanks for all the great articles. I am having such a problem tho just getting WP installed. I’ve followed your instructions regarding doing it with MAMP and keep getting the “can’t connect to localhost:8888” msg. After much searching I found a terminal code to test apache. It returned with httpd not running, trying to start. I did it again and it didn’t return anything so perhaps worked.
I tried connecting to local host again and this time I got Error connecting to database. At least this seems like progress! I tried some of your suggestions above and this time, it seemed to be trying to connect for quite a while, but finally gave me the Error connecting to database again.
I have yet to get the happy WP page. Does anyone at all have any ideas as to what is going on? I have spent hours searching, contacting, trying this and that and I don’t know what else to do at this point. I am an absolute WP beginner.
Thanks to anyone/everyone in advance.
Hey! Were you able to figure it out? I’ve tried all of their solutions and I’m still stomped.
Hi Ashley, No, unfortunately nothing is working for me either! I’ve yet to find someone who knows the answer & have been asking many places. For me, I think I’m going to use different software and leave WP for another time. Too strange, too much hassle.
This isn’t a WordPress problem, it’s a MAMP problem. If you have to run this on a local server, try a different XAMP package. Better yet, get a shared hosting account and set it up there. Local XAMP can be frustrating.
I missed that when you add the repair line to your WP config file you have to add it right at the end of the declarations before the first “if” statement when I first read it. Took me a while to realize what I was doing wrong. Still didn’t fix my problem but at least I was able to strike one possible issue off my list.
Thank you for a great helpful post.
Solved by resetting DB password but this process repeated several times. issue solved but page speed is too much slow now.
Also had the same problem and tried everything. TestConnection was failing. What I just had to do was manually edit the user password through MySQL Cpanel and it suddenly worked!
I am so confused. First time on wordpress. No tech experience whatsoever. I didn’t receive any error. message. My website is up. The problem I had is when I set the page to update some updates would take effect and others didn’t. I called WordPress and they told me I didn’t set up the database correctly. Is this the same issue as this article or something totally different? Thanks in advance.
Hi, i had this problem when I formatted my laptop and reinstalled XAMPP.
Problem was, I forgot to create a new Mysql user with the same data (host, name and password) from my BD user stored on wp-config.php, just like we do when we configure one new WP site for the first time. That solved it
So, maybe the problem is not that the user has not enough permissions… perhaps the problem is, user simply doesn´t exist yn mysql.user table! (as I said, it can be possible after formatting your computer).
thank you for the great help, i got mine up by replacing localhost with 127.0.0.1
I just had the same problem, which I managed to solve by simply changing the DB password and correcting that in the config.php file.
I have no idea why the password suddenly became an issue, though.
Changing the localhost in my wp-config.php to the ip on my host account in godaddy cpanel did the magic for me. Even godaddy support were talking rubbish. Imagine telling to upgrade with about $200, that my hosting plan is low on resources. I almost believed until I found this article. Thanks a lot.
I still have the problem. GoDaddy tried but unable to help. They have a service that, for $80.00, will have a WordPress expert investigate. However, there are no guarantees she / he can fix the problem but you still have to pay the $80.00.
This review was very helpful,
I had a similar issue with installation on windows server, I kept getting database error, and what I did was to replace localhost which was internal server to IP Address and Port which was external server and it worked perfectly.
Mine just happened like 20 minutes ago, I am hosting at Ipage and WHAT A SCARE!!! It happened all over sudden and I could not access both the admin site and the website itself with my laptop but I could access the site with my phone.
Then before I could complete reading this guide the site got back by itself lol. I want to know if there is a way to avoid this from happening in future.
The same happened with my site. Very strange!
Nice article. I want to know about the AMP errors and how to fix them ?
This was helpful, Thank you very much. I always rely on WP Beginner when struggling.
my website is showing database connection error .
can some one help me to solve the problem
my website is created in amazonweb service
for every 24 hours it showing database connection error
the php-myadmin ,wp-content every thing is fine
can plz some one help to solve this issue
I have no WordPress account, but have been having this happen to me a lot recently on several sites. How can I fix this? (Note: I don’t own these other sites and I don’t think I can access their databases directly)
Thanks much for the article. I ran into the same issue.
1. Wp-Config file was correct
2. phpMyadmin connection was success
3. testconnection.php approach too was success
4. I tried to update the site URL I couldn’t get it as naming conventions of the table names of the WP changed
THIS IS WHAT HELPED ME FINALLY.
I selected wp_Options” table in the phpMyAdmin window to browse the content, there was an informative Error message that, the table has to be repaired. (wp-config auto-repair didn’t work out)
I was able to repair using the options available in phpMyAdmin and got my site back.
Thanks all.
hi there.. can i please seek help.. i am working now on transferring my live WordPress site to local using XAMPP.. my database is doing good but i cant connect it during the WordPress installation. please help.. thanks in advance!
Hi there,
Thank you for this article. My website was hacked recently and after I got no support from the hosting provider, I was told I was going to need to reinstall WordPress and upload the backup. I didn’t use a backup plugins (I am all the way in South Africa) as it is to expensive for the paid plugins. So I did a cpanel backup through Softaculous and downloaded the Zip of 1.03Gig. Two weeks later my site was hacked (Twice in one week). With the second hack my wordpress installation was comprimised – and backup deleted from server. So I had to delete the original installation and reinstall. Now I followed all the steps to upload the zip to file manager and now I am stuck with this: Error Establishing a Database Connection. I have tried all the suggested solutions from this post, but still no progress. Please help me! I am quite new to wordpress and this is my biggest client’s website.
Kind regards,
Tanya
Hi Tanya,
The most common reason for this error is incorrect Database information in your wp-config.php file. Confirm that you have restored your WordPress database correctly, then enter the mysql host, database name, database username and password carefully. If you are unsure, ask your hosting provider and they will let you know your mysql host and other information.
Hope this helps
Some folks suggested that they fixed their problem by replacing localhost with the IP.
It worked for me….
Any logical explanation for this…?
Think about it. ‘Localhost’ is the name of a location. If changing that to the IP works, then ‘localhost’ was not pointing where you need it to be.
Thank you for your article, it was very informative and helpful in getting my issue resolved.
I have MAMP installation on my Macbook. i.e I am running MySQL also on my local machine. My problem was that in wp-config.php, the database server ‘localhost’ is not working. Once I changed it to 127.0.0.1, it started working.
What does it mean if testconnection.php is just a blank white page?
Repair database command worked for me.
Front end or site was fine but couldn’t access admin panel – Repair found a damaged database file (wp-options) – repair worked
What i did was, in the cPalen i went into MySQL databases and scrolled down to current user and changed the password. I used the very same password that was in my wp-config.php file but for some reason the password was corrupt. Everything works fine now.
I got this error on a client’s site. After a bum steer chatting with a Bluehost tech, I called back and the phone tech support person I got did this same @JP Hoey trick (changing the DB pass and updating it in wp-config.php). Took 5 minutes and worked like a charm.
An obscure one for me…
Don’t use #’s in your password.
I had two hash tags in my password that was throwing something off in the PHP–since hash tags create comments I guess. I removed the hash tags and BOOM, I’m in.
First thank you for a well-documented article on “We were able to connect to the database server (which means your username and password is okay) but not able to select the XXXXXX database.”
I tried almost everything outlined in the article but can’t fix my problem. Here is an outline and history.
Local machine installation:
I have installed MySQL 5.7.17 and MySQL WorkBench 6.3.8. And WordPress 4.7.2. The MySQL was installed after removing both an earlier version of MySQL and WorkBench. The previous version was displaying the same database error. The previous WordPress/MySQL worked about 2 years ago, but recently I contracted a project to develop a WordPress Web site, and I encountered this problem when I began setting up the new WordPress framework for this new site. The old WordPress site was deleted so I couldn’t test if that worked.
So, I reasoned, why not try a fresh start and also upgrade to the latest versions, etc.
But, this new version also had the same problem with not being able to connect to the database. I have narrowed down the problem to “permissions” and I haven’t been able to crack the code.
The MySQL Server logs verify this is this case:
Timestamp, Thread, Type, Details
2017-01-28T02:46:24, 134, Note, Access denied for user ‘root’@’localhost’ (using password: YES)
I have created a new WP directory and installed a fresh WP version etc.
I deleted and created new databases in WorkBench and properly assigned them users and passwords.
I have checked the Users and Privileges, Client Connections and can find nothing that would block access to the database.
I have checked my Windows 7 permissions for the MySQL Directory and the sub directories. I have unchecked the “Read Only” to the master MySQL Directory and all directories and files are read/write.
I have progressed through each attempt to connect to the database each time I change something.
I am exhausted and very disappointed there isn’t a good response by the WP Support Team to this issue of “not able to select the database.” It is a “popular” issue that many developers are experiencing with no resolution–many more experienced than me, are having.
You are my last hope!
Thank you!
Thanks it work.
Thank you, it worked for me while connecting database from phpmyadmin.
I’m trying to install wordpress on my local pc running window 10. I have installed Xampp, made all the changes to use port 8080. No problem starting Xampp. No problem starting Apache or Mysql. I can open phpmyadmin from the admin button without problem and was able to add a database. if I enter http://localhost:8080 it brings up the Xampp page. However when I enter http://localhost:8080/wp to install wordpress the web page times out, but does nothing. I extracted the wordpress files into the htdocs folder and made the changes to the wp-config.php file to use localhost:8080.
At this point I have no other idea what to do to get the install done and could really use some help.
Hi Tom,
You will need to copy paste WordPress files in /htdocs/wp/ folder instead of /htdocs/ folder.
Thank you! worked!
Unable to install word press on godaddy. Won’t even complete the installation. “Application has Errors”. – Need it urgently!! Please help
Hello WPBeginner,
I am currently running Viral Website. Many viral websites have some plugins. I am attaching link of the screenshot Here
In viral website niche, we need to see how many real time users are there on a particular story because, to see the viralness of new stories we need to see the real time users for a post. We use Google Analytics but Data Mining is too hard in google analytics to see if our story is going viral or not. By looking at the real time users for a particular story, we make conclusions that which of our story is going viral on social media.
Can you please provide me details? How can i show my Live story readers on the story/article page. I want my users to see how many live users are reading this particular story.
Is there any script code or any plugin for this type of feature?
Brilliant, this guided towards me getting this fixed!! Also had to change upload a new wp-settings.php file.
Thanks
To allow use of this page to automatically repair database problems, please add the following line to your wp-config.php file. Once this line is added to your config, reload this page.
define(‘WP_ALLOW_REPAIR’, true);
Massive thank you! 1st task fixed my site!
Hi Ben,
Glad you found it helpful
You may also want to subscribe to our YouTube Channel for WordPress video tutorials.
Thanks!!! Thanks to you my site work again!
Happy
Had this error trying to install WP at home for testing on Windows laptop. Tried everything and then noticed that my character set in the wp-config.php file where i defined the parameters for accessing the database contained characters that were not standard. The apostrophe character was not the standard windows font character. Once i replaced and made them all the same character WP installed without issue.
please someone connect my computer with teamwork and resolve my problem
Error establishing a database connection
I have the same problem.
Let me know if you have any suggestions.
Thanks.
Worked for me by replacing “localhost” withe the IP you provided. Thank you!