How to Fix Error Establishing a Database Connection Error
As a WordPress site developer or webmaster, a terrifying situation is when your site is down. You not only lose incoming traffic but your site’s ranking and reputation are also negatively affected.
One of the errors that cause your site to temporarily be inaccessible is the database connection error. You might have run into this error when you find yourself entering the URL of your site and nothing appears other than this error message: “Error establishing a database connection.”
If you’ve ever encountered this error or are in the midst of finding a solution for it then don’t fret! In this tutorial, we will show you how to fix this error very easily – the right way. By following the steps carefully, you’ll be able to retrieve your site and get back all of your traffic and subscribers in no time.
Sound good? Let’s get started!
What Is the Error Establishing a Database Connection Error?
Before we jump into the tutorial let’s take a brief moment to understand what this error actually means. The error establishing a database connection error basically means that for some reason or another the PHP code was unable to connect to your MySQL database to retrieve the information it needs to fully build that page. That’s why the error is always shown on a blank page because there is no information about your site as it is not connected to your database.
Why Does the Error Establishing a Database Connection Error Occur?
Now that we have understood the meaning of the error, we need to find out what is causing the error when we try to open our WordPress site. It could be because of many reasons because errors in database connectivity may have several different underlying causes.
01. Incorrect Login Credentials
For instance, it can be because of the wrong login username and password being used to access the admin panel. If the login credentials have changed recently, you will not be able to access your database with the old ones. So your first order of business should be to confirm that the login credentials you’re entering are accurate.
02. Corrupt Database
Another possibility is that the database might be corrupted. The WordPress database can be corrupted by a number of things like installing a faulty or incompatible plugin. Another likely reason might be that the server that hosts your database may temporarily be down.
These are some of the possible reasons that may have caused the error. Now that you know what the error is and how it was probably caused, let’s try fixing that error and getting your site back up online.
How to Fix Error Establishing a DB Connection Error
Before fixing the error, we need to determine exactly what is causing the error.
Method 1: Repair the Database
First we need to check if our database is okay. If you can see your site normally and get an error when trying to access the wp-admin page or get a different error saying “One or more database tables are unavailable. The database may need to be repaired.” when you try to access your site, these are clear signs that your database is corrupt. Thankfully, your database can easily be repaired using a built-in WordPress feature.
By default the feature is disabled, so you need to enable it yourself in order to repair your database by going into the wp-admin.php file and adding this code:
define( 'WP_ALLOW_REPAIR', true );
This simple code of line allows you to repair and optimize your database. After adding this line, simply navigate to www.yoursite.com/wp-admin/maint/repair.php. A page with two similar options will appear – the first option would be the Repair option which will repair your database and the second would be the Repair and Optimize option which will repair and optimize your database for future errors and keep it stable.
The Repair and Optimize error takes more time than the simple Repair option so if you’re short on time then simply choose the first option.
Another important thing to keep in mind is that the repair page you just opened after putting the line of code into the wp-admin.php file is not secure; as anyone can access that URL without having to login. So make sure you remove that little line of code from the wp-admin.php file you put earlier after you have successfully repaired your site. Doing this guarantees that no one will mess with your database options in the future.
Method 2: Change Login Credentials
Still no luck on opening your website successfully even after repairing the database? You might want to take a look at the database login settings in the wp-config file. Your database login credentials may have stopped working because you might have changed your hosting company or some useful information about your database which wasn’t manually updated in the wp-config file.
Open the wp-config file again just like before and find the database login information; it will probably be near the top of the file.
It shows the name of the database as “DB_NAME”, the login username as “DB_USER”, the login password as “DB_PASSWORD”, and the database host as “DB_HOST”. After going through all of this information, do make sure everything is correct as WordPress won’t be able to connect to the database if any of these values are incorrect.
You can check your database outside of WordPress by using PHPMyAdmin which is basically a tool for accessing and editing MySQL databases. But be careful when using this tool as it deals directly with your site’s database – there’s a great chance of messing things up.
- After logging in from your PHPMyAdmin account, click on your database from a list of databases on your server. Make sure to click on the one which matches the name in the wp-config file which you just checked.
- After clicking on the database name, you’ll be seeing a lot of names of the tables in your database. To make sure you’ve got the database name right, find the table named wp_options and click on the Browse button next to it.
- This will take you to a page where you will be able to see the name, URL, and other general settings of your WordPress website. Make sure it’s the same as it was in the wp-config file you saw earlier. If, however, it isn’t, go fix it in the wp-config file before proceeding.
Now that’s it for the database name. Moving on to the password and the username, there are a few ways to check if both of those are correct. One way is to check the already existing login username and password are correct is by creating a simple .php file which will test to see whether or not you are able to connect to the database using the login credentials from the wp-config file or the existing login username and password. To test, just simply create a .php file, name it anything you like and add this code to it:
<?php $test Connection = mysql_connect('localhost', 'root', 'password'); if (!$testConnection) { die('Error: ' . mysql_error()); } echo 'Congratulations! The database connection works!'; mysql_close($testConnection); ?>
This code allows you to test the connection by going to the URL of the file into your browser. For example, if I set the name of the .php file to sample-file.php then I’ll use the URL www.yoursite.com/sample-file.php. You’ll either get a successful connection or in the worst case, another error with more details, which means your previous login credentials are not working and you need new ones.
To create a new database user and password we require using a different tool available in cPanel called MySQL Databases. Simply go click on that and under the heading of MySQL Users click Add New User. This will take you to a new page which asks you for the new login credentials.
Once you’ve chosen a username and a password, make note of them in the wp-config file. Click Create User to continue. On the following screen, click on Add User to Database, choose your new username and your WordPress database and click Add.
Once you’re done adding the new user to the database, simply update your wp-config file with the new username and password. Doing this ensures your database name, password, and username is 100% correct.
Method 3: Fix Corrupted Files
If completely troubleshooting your database doesn’t even work, the last thing you can try is to fix any corrupted files in WordPress. Many users who failed to identify the underlying problem completely wiped all of their files and the error went away. So that’s exactly what we are going to do now as the error could be because of corrupted WordPress files.
That said, since we are talking about the main system files, you need to be very careful. The tiniest of mistakes can wipe away your entire site. Before we proceed, it’s best to backup all of your data either manually or by using a plugin. The next thing we need is new, fresh WordPress files to replace the old corrupted files that are causing problems for us.
- Download a fresh copy of the WordPress CMS from WordPress.org.
- Unzip it in your drive and delete the whole wp-contents folder and the wp-config file so there’s no chance of overwriting your current wp-config file. Plus all the hard work you’ve put into setting up themes and plugins up till now won’t be lost.
- After deleting the infected or corrupt files, paste the rest of the files into your WordPress root folder using the File Manager or an FTP client.
This will replace the corrupted files or the files which are causing the error. By following these simple steps you’ll be able to easily fix the Error Establishing a Database Connection error. Reload the page once you’re done and you are all set to go.
If you’ve followed the entire tutorial in order (and correctly) and the problem still persists, the next logical step would be to contact your web hosting service’s customer support representatives because it’s entirely possible that the error occurs at the server end. Remember to tell the customer support representative about the measures you took to resolve the problem.
Wrapping It Up
This Error Establishing a Database Connection error is one of the most frustrating errors you can encounter with your WordPress website. There are several different possible root causes for this error making it difficult to identify and resolve it.
We covered some of the most probable reasons behind this error and walked you through three easy-to-follow methods to fix it. If you follow the steps outlined in this tutorial extra carefully you’ll have your site up and running in no time.
Were you able to identify and fix the Error Establishing a Database Connection error on your WordPress website? Which method worked for you? We’d love to hear about your experience so let us know in the comments section below!
Hi,
I was changing page status from published to draft and after that I started to get this database error? So am I getting this error because I edited the page or it is just the coincidence that the server decided to go down at the same time??
Hi Vini,
I don’t think changing the status of the post has anything to do with the error. But it could be because of corrupt files or database-related issue.
Did you try repairing your database? If that doesn’t work, you should contact your hosting provider to see if they can find the culprit.
Hi my friend’s website [link removed] seems to have this issue
Hey Milo,
Just checked your friend’s website, looks like you got the issue sorted out.
I get this error whenever there are more than 250 visitors on my site at once… my hosting provider says that my site is trying to make too many connections, but they offer little advice on how to resolve the issue! Any ideas would be gratefully received!
Hey Tania,
I think you need to upgrade / change your hosting provider, here are some great options to choose from.
Another issue, could be a heavy WordPress theme or plugin. Which theme and hosting are you using?
Hi Rafay
Thanks for the article, it helped me allot. Basically I have a website on a raspberry pi that has Raspian and LAMP installed with WordPress. The website is hosted by myself using a Dynamic DNS. I keep getting this “error establishing a db” after a while of the site being up. I believe it’s because I’m getting allot of traffic, as this is really a site for me to try and get a job so probably allot of prospective employers looking at my site and clogging up the traffic. If this is the case 1. How do I make my server allow more traffic so this doesn’t happen again? 2. if it’s not because of traffic, why do I get this message now and again?
Thanks for reading dude and keep up the awesome work.
P.S. The way the get the message to disappear before is simply by restarting my server and hey presto it’s back working normally again until a week or two later when I get the message again.
My website showed “Error establishing a database connection.” just once for about a few seconds. Can I edit that page for in future it rather displays what i want.? And why did it happen for a few seconds and auto-corrected.?
Hi, my website also encounter the same prob. May I have ur email for further advise ?
You can reach out to us using our contact us page – http://www.designbombs.com/contact
Thank you for this post… You saved my work!
lol my website is showing up, but I cannot login. So… now what?
Hey Kristi,
Can you please provide a bit more info? If you forget the password, you can reset it from WordPress using PHPmyadmin or by uploading password reset script.
https://codex.wordpress.org/Resetting_Your_Password
I AM NOT TOO COMPUTER SAVVY. THIS WAS TOTALLY OUT OF MY LEAGUE. I DID NOT UNDERSTAND ANY OF IT. I WENT TO GMAIL’S WEB SITE AND FOLLOWED THE INSTRUCTIONS. I DID GET ACCESS TO MY GMAIL AFTER THAT. THE NEXT DAY IT HAPPENED AGAIN. IS THERE ANYWAY YOU CAN GIVE SUGGESTIONS TO US WHO HAVE MINIMAL KNOWLEDGE IN COMPUTER SKILLS?
THANK YOU!
Hey!
Actually I was trying to move my wp site from subfolder to root folder so I just copied wp files from subfolder to root folder. so when I tried to login wp admin using the domain name of my root, it’s showing this error. While when I open wp admin using domain of sub folder everything runs fine. So I think the problem is establishing database connection with my new root address but I am unable to sort it out. Can you help me in this?
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…
For the repair parameter, you say that it is in wp-admin.php, but it is in wp-config.php
Our website is inconsistent. The front page loads normally, but takes a longer time. However, when we try to access the back end (wp-login.php), it either takes a long time to get to the login page or gives us the “Error in Establishing Database Connection” message.
Is this a likely database problem or a server problem? Thanks!
Hi, thank you for all your help and guidance provided!
I realized that the database error occurred just after I installed and activated a new plugin. So I deleted the plugin from my c-panel, and also followed the repair and config steps explained. I added the code to wp-admin.php, but it seems like it was rather/also needed in the wp-config.php file.
All fixed, so thanks!
How do i open the get to the file for the code.