Did you just try to access your WordPress site only to be met by the dreaded Error Establishing A Database Connection message?
You’re certainly not alone! Anyone who’s worked with WordPress long enough has probably run into this issue at one point or another.
And don’t start worrying too much…I’ll show you how to fix the Error Establishing A Database Connection message in WordPress so that you’ll be back to a fully functioning site in no time.
In this post, I’ll take you through:
- What the Error Establishing A Database Connection message means.
- What is likely causing the Error Establishing A Database Connection message to appear.
- How to fix the Error Establishing A Database Connection in WordPress.
What Does the Error Establishing A Database Connection Message Mean?
Ok, so you just tried to head to your WordPress site and ran into this:
So what does that ominous message actually mean? Let’s start with the basics. This is a bit oversimplified, but WordPress is composed of two basic parts:
- Code – this is what determines how your content displays and functions.
- Database – this is where your actual content is stored.
For example, when someone visits one of your posts, the code of your site builds the layout and decides how the content should look. But to get the actual content to display (e.g. the text of your post), WordPress needs to query the database.
Without the database, there’s no content to display.
So if WordPress is having issues connecting to your database, it’s not able to get access to the stored content it needs to function. And that’s why it gives you an error!
How to Fix the Error Establishing A Database Connection Message in WordPress
Now you know what the error actually means. But how can you fix it?
While there’s always a chance your site is encountering some unique, site-specific problem…most of the time, one of these troubleshooting steps should be able to fix the database connection error.
Check If Your Database is Corrupted and Repair It
There’s an easy way to check if your database connection is truly broken or just corrupted. All you need to do is try to navigate to your WordPress dashboard. That is, go to yoursite.com/wp-admin.
If you see the exact same Error Establishing A Database Connection message when you go to your dashboard, you’re done with this section. That is, you’ve verified that it’s not a simple corruption problem.
If, on the other hand, you get a different error (normally something like One or more database tables are unavailable…), your database is corrupted, rather than completely unavailable.
Thankfully, fixing a corrupted database is normally pretty simple. All you need to do is add one line to your wp-config.php file and then run the automated repair process.
To do this, find your wp-config.php. If you’re using FTP, you can download the file to your computer. But for things like this, I prefer using cPanel’s built-in File Manager tool:
Once you launch File Manager, make sure to select the domain in question:
Then, find wp-config.php, click on it, and select the Edit button at the top:
Now, you should be able to edit your wp-config.php file in your browser:
Add the following code lines to the bottom of your wp-config.php file:
/* Enables the database repair function. */
define ( ‘WP_ALLOW_REPAIR’ , true);
Make sure to save your wp-config.php file by clicking the Save button in the top right.
Then, navigate to www.yoursite.com/wp-admin/maint/repair.php and click the Repair Database button:
Once you’ve repaired your database, the error should hopefully be gone. Just remember to go back to your wp-config.php file and delete the code you added. You only want to enable the repair functionality when you actually need it.
Check If Your Database Details in wp-config.php Are Correct
If your database isn’t corrupted, the next step is to verify that the database details in your wp-config.php file are correct. Basically, if the access credentials here don’t match up with the access details in your hosting account, WordPress won’t be able to connect to your database.
Start by opening your wp-config.php file and finding the section below (you can open your wp-config.php file by following the same steps I outlined above):
You should see all of your credentials laid out in plain text. That is, your database name, username, and password.
While there are ways to test if the existing credentials are correct, they can be a little confusing for non-developers. Personally, I think the easiest way is to just enter new access credentials. Why? Because it knocks out two birds with one stone. That is, it ensures that:
- Your database connection details are correct
- Your database user has the proper permissions
To get started, open up a new tab with cPanel and find the MySQL Databases option:
First, make sure that you actually see the database name listed in your wp-config.php file in the list of Current Databases in cPanel:
If it doesn’t already exist, you’ll need to create it. But hopefully, it already exists so you don’t lose your content.
Now, go ahead and create a new user by scrolling to MySQL Users and entering the details:
Creating a new user allows you to absolutely ensure that the access privileges are correct.
Once you have your new user, scroll down to Add a User to a Database and add your new user to your existing database (the same one listed in your wp-config.php file):
On the next screen, make sure to check the box to give your user All Privileges:
Now, head back to the editable wp-config.php file you opened in File Manager and update the username and password areas. Just replace the text between the apostrophes:
Once you do that, you can be certain that your database credentials are valid. And most of the time, that should be enough to make the error go away.
Contact Your Host
Sometimes the problem has nothing to do with you or your WordPress site. That is, the root of the problem might actually be an issue with your host.
There are a couple of reasons this might happen:
- Most likely…You’re using shared hosting with a memory limit that is too low. Ask your host’s support if this is the problem and how you can increase your memory limit.
- Less likely…Your shared host uses separate servers for databases. While not every host works this way, if it does…there’s a chance the database server is down while the server housing the code side of things is still humming way.
So if none of the other troubleshooting tips have helped, reach out to your host. If your database credentials are valid and your database is not corrupted, then the next most likely cause is that pesky memory limit, which requires help from your host to fix (in most cases).
Wrapping Things Up
Look, I know how scary it is to see that big Error Establishing A Database Connection message when you try to visit your precious site. But most of the time, it’s probably just a simple problem.
It’s easy to make a typo when inputting database credentials, or sometimes wayward plugins corrupt your database in a way that requires fixing.
If neither of those are the problem, the next most likely scenario is a problem with your host.
And if none of those issues fix the Error Establishing A Database Connection message on your site, leave a comment and let’s try to figure out the issue together.