The “error establishing a database connection” message is an especially frustrating WordPress error where your entire site gets replaced by a message that looks like this:
If youโre reading this post, thereโs a good chance that youโre experiencing this error right now. If not, you are in luck.
And even if youโre not staring at this message, learning how to fix the error establishing a database connection problem is still something that every WordPress user should know.
In this post, youโll learn three things:
- What the “error establishing a database connection” message means
- Some of the most common reasons why this message appears
- How to fix this error in WordPress
What Does The “Error Establishing A Database Connection” Actually Mean?
If youโre not familiar with how WordPress works, there are two essential parts to a WordPress website:
- Your files – this includes the WordPress software, your themes, your plugins, your media uploads, etc.
- Your database – this is where the actual content of your posts and pages are stored (as well as lots of other important information).
Whenever a visitor lands on your WordPress site, the PHP files in WordPress reach out to your database to determine exactly what information to display.
Without a working connection to the database, the PHP-file part of your WordPress site canโt get the information it needs to render a working page. Thatโs why it displays the “error establishing a database connection” message instead.
What Causes The “Error Establishing A Database Connection” Message?
There are multiple potential reasons why youโre seeing this message.
Most of the time, these causes boil down to:
- A corrupt database
- Incorrect database configuration details in your wp-config.php file
- Corrupt WordPress core files
- Problems with your web host
Because you canโt be sure whatโs causing the message for your specific situation, the best way to fix the problem is to take a diagnostic approach.
That is, you need to systematically follow a series of tests to isolate (and then fix) the problem.
Hereโs what to do:
How To Fix “Error Establishing A Database Connection” in WordPress
Potential Fix 1: Check If You Can Still Access The WordPress Dashboard
When you see this message on the front-end of your site, the first thing to do is check whether or not you get the same error when you try to access your wp-admin dashboard.
If you still see the same exact error message when you try to access your wp-admin area, skip ahead to the next step. Youโve already eliminated this problem as a potential cause.
If you see a different message mentioning something like โThe database may need to be repairedโ, continue with this section to learn how to repair your WordPress database.
Step 1: Turn on the repair utility in the wp-config.php file.
To get started, you need to edit the wp-config.php file for your site. You can do this by either:
- Connecting to your site via FTP. (FileZilla is a good program for that.)
- Using cPanelโs built-in File Manager tool.
Your wp-config.php file is located in the root folder of your WordPress site. Thatโs the same folder that contains wp-admin and wp-content:
Once youโre able to edit your wp-config.php file, add this short code snippet to the bottom of the file:
define( 'WP_ALLOW_REPAIR', true );
Make sure to save your changes before continuing.
Step 2: Run the automatic database repair utility.
Once youโve added the code snippet, you can access the WordPress database repair tool by going to:
YOURDOMAIN.com/wp-admin/maint/repair.php
On that page, click Repair Database to run the tool:
Once the tool completes its repairs, your site should hopefully work again. If it does, all you need to do is return to your wp-config.php file and remove the same line of code that you added to enable the repair utility.
If your site still isnโt working, continue onto the next step.
Potential Fix 2: Check Your Database Login Credentials
If your database isnโt corrupt, the next thing you should check is whether your database login credentials are working.
Your database credentials are defined in your wp-config.php file:
If this information doesnโt match the database credentials that you created via your hostโs dashboard, WordPress wonโt be able to access your database.
To test whether your database credentials are working, you can use the mysql_connect() function.
Use a text editor like Notepad to create a file called databasecheck.php.
Then, add this code snippet to the file:
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
Make sure to replace โmysql_userโ and โmysql_passwordโ with the actual username and password from your wp-config.php file.
Then, upload that file to your website via the cPanel or by FTP and access it by going to:
YOURDOMAIN.com/databasecheck.php.
If you see a message that says โConnected successfullyโ, your database credentials are working and you can skip to the next step.
But if you see a message like the one below, you have a problem with your database credentials:
The easiest way to fix the problem is to create a new set of database credentials to use for your WordPress site.
Step 1: Go to MySQL Databases in cPanel.
Assuming your host uses cPanel, you can create a new database user by going to the MySQL Databases option in your cPanel dashboard:
Step 2: Create a new user.
Scroll down to the MySQL Users area and find the box to Add a New User.
Enter a new:
- Username
- Password
Make sure to remember these details as youโll need them in a second.
Step 3: Add a new user to the database.
After you create a new user, go to the Add a User to a Database option and add the new user to your WordPress siteโs database:
On the next screen, make sure to give the user ALL PRIVILEGES:
Step 4: Update the wp-config.php file with the new database user.
To finish, you need to go back to your siteโs wp-config.php file and update the DB_USER and DB_PASSWORD fields to the new user:
Once you save your changes, your site should hopefully be working again!
Potential Fix 3: Reupload Core WordPress Files
If neither of the above methods fixed the error, some of your core WordPress files may be corrupt.
To ensure this isnโt the issue, you can upload a fresh copy of WordPress to your site. Done right, this wonโt affect any of your existing content, plugins, or themes.
Go to WordPress.org and download the latest version of WordPress:
Then, extract the ZIP file and delete the wp-content folder and the wp-config-sample.php file:
Upload the remaining files to your WordPress site via FTP. When your FTP program asks you what to do about duplicate files, make sure to choose the option to Overwrite duplicate files.
This will ensure that none of your core WordPress files are corrupt, and it will hopefully fix the issue.
Potential Fix 4: Speak To Your Host
If none of the above solutions have worked, you may just be dealing with a hosting issue.
There are a couple of potential hosting problems:
- Your database server is down. Some hosts use a separate server for databases, which means your database might be down even if your regular server still works.
- Your database has met its quota. Some shared hosts put a cap on your database usage. If your site has met its quota, that might explain why your database is down.
To work through these problems, youโll need to talk to your hostโs support team.
Final Thoughts
By this point, youโve hopefully been able to fix the error establishing a database connection message and have gotten your site working again.
There are still some smaller issues that might also cause this error. These issues are often unique to your site and are therefore hard to diagnose in a post like this.
If you continue seeing the error message after going through these fixes and talking to your host, you might want to consider reaching out to a WordPress developer to further investigate the issue.
Or, you can always leave a comment and hopefully we can all fix the problem together!
Here are a few hand-picked article that you should read next:
- Kinsta Managed Hosting Review: Is It Worth The Money & Hype?
- Top WordPress Hosting Companies That Offer Free WordPress Host Migration Services
- Which is the Top WordPress Hosting for High Traffic Sites?
I’ve faced this issue one time and the first solution did the job for me.
It can be a confusing task for some non-technical users. The good part is you’ve added proper images of each step and explained it in detail.
Thanks Harsh!
Excellent tutorial.
I used to get this quite a bit, then I changed hosts and things cleared up. Since my host changed servers last month things seemed to speed up even more, which is awesome.
This error can be annoying and confusing. So happy to tweet it to my followers to give them clarity.
Thanks for sharing Harsh.
Well, thank you so much because this literally solved all my problems. I was having same “error establishing a database connection” problem. But this method solved it really. Thank you very much.
Thank you so much for sharing this step by step tutorial. Actually, I have installed WordPress first time, it shows me “Error Establishing A Database Connection” and I donโt know what to do. Then I have searched on Google and I found your this article and really it is
very helpful for me and also many people who have no idea about this things.
My site was down due to establish the database. I have contacted my provider, they solved my problem. Thank you for sharing this. Next time, I don’t need to contact my provider, I can solve this problem myself. Thank you for sharing the article.
Great informative article. Itโs a best guideline for who doesnโt know about database setup on WordPress platform. Even I am very thankful to this post for know about wordpress database connectivity. Thanks.
thank you so much, I was able to get my site back & saved $$$ to get my hosting company restore it, which I had already pay them to restore b4. Great videos, clear to listen. Keep up the great job. Also tks to people like you that post free tutorials.๏ปฟ
Thanks, sir,
This article is very informative. Itโs the best guide for beginners who don’t know about database setup on WordPress.
You discuss this step by stepโฆ Thatโs why I love your articles all time.
Keep writingโฆ
Hi Harsh,
Nice guide. For some of the managed hostings or Amazon AWS, we do not get a cPanel but your method of updating WP-Config and doing the repair online will definitely help.
I have faced this situation once or twice but most of the time it boils down to wrong credentials than anything else. Especially if we are using WP-Optimize plugin to manage the WordPress database.
โ Sanjeev
What a very helpful tip man!
This is an easy thing to do to fix that database problem โ you just have to know exactly how to do it. And you just showed us the way!
Thank yo very much!
when i started wordpress with free blogging , i got this problem always . but after transfering to premium hosting , i have never faced this problem . This post is informative . Thanx
Hi Harsh,
Excellent tutorial.
I used to get this quite a bit, then I changed hosts and things cleared up. Since my host changed servers last month things seemed to speed up even more, which is awesome.
This error can be annoying and confusing. So happy to tweet it to my followers to give them clarity.
Thanks for sharing Harsh.
Ryan
Thanks sir,
This article is very imformative. Itโs a best guide for beginners who doesn’t know about database setup on WordPress.
You discuss this step by stepโฆ Thatโs why I love your articles all time. โค
Keep writing…
Prevention is always better than cure. All though I mainly use blogger. But I have scheduled to migrate it to wordpress. Considering this, it is always good to know about the problems that I can facec and their possible solutions. Specially when these things come from your blog, one can always believe on in blindly and follow those steps.
Thanks a lot for being with us.
Hey Harsh!
What a very helpful tip man!
This is an easy thing to do to fix that database problem – you just have to know exactly how to do it. And you just showed us the way!
Thank yo very much!
Cheers! ๐
Hello, Harsh.
This article of yours is really very informative. Even someone with just a little to basic programming knowledge can understand and follow it. Plus, you accompanied the steps with pictures which makes it really easy.
Hello Sir,
This is a very helpful article for the begginner to start with debugging the error. Thank you so much Sir as it will definitely help me if I face this error in my website.
Howdy Harsh Sir,
Learnt a lot from this, I had paid a handsome money to a developer for fixing a database error on my blog! At that time, I Googled many website to get thing fixed…
Wonder why i don’t search on your site for a database solution!!
This will help many bloggers for sure… thanks for sharing!
– Rajinder
I have faced this problem many times. I didn’t know what to do. I removed the existing installation and reinstalled. This is disgusting. I hope your post helps me it happens again.
Thank you. Still If I have doubts can I ask you ?
Hi Harsh,
Nice guide. For some of the managed hostings or Amazon AWS, we do not get a cPanel but your method of updating WP-Config and doing the repair online will definitely help.
I have faced this situation once or twice but most of the time it boils down to wrong credentials than anything else. Especially if we are using WP-Optimize plugin to manage the WordPress database.
– Sanjeev
Yes.. This can be helpful if I get this issue. Thanks Harsh for sharing such a information..