“Error establishing a database connection.”

Nobody likes seeing this message. It’s scary, frustrating, and practically the definition of a bad day for a WordPress website owner.

You can’t understand what went wrong. Your internet connection is good, you’ve logged out and logged in into your account again, you’ve sent up a prayer. But still… nothing.

No need to panic!

Fixing an error when establishing a database connection in WordPress isn’t that difficult. All you need is a little guidance, which is exactly why I’ve created this guide.

What to Expect When Fixing Error Establishing a Database Connection in WordPress?

An error establishing a database connection is a very common issue. It’s also self-explanatory—it’s an error that occurs when the database connection fails. To solve it, just follow the steps below:

  • Step 1 – Contact Your Web Hosting Service Provider
  • Step 2 – Check Whether Your WordPress Theme or Plugin Files Are Corrupted
  • Step 3 – Check Whether Your WordPress Base Is Corrupted
  • Step 4 – Check Whether Your Database Connection Credentials are Correct
  • Step 5 – Restore the Default WordPress Files

The Good

The error message can pop up due to all kinds of reasons.

Luckily, you can solve this problem without any extra hassle by simply contacting your web host provider.

There might be a server down that’s causing the error. Assuming your chosen web host provider has a fast-reacting support team, they’ll tell you the exact cause and even fix the issue for you. Nothing extra is required from you.

This is why you should always invest in quality WordPress-specific hosting tailored to run the platform as efficiently and quickly as possible.

Moreover, solving the error is a pretty straightforward process. If your provider doesn’t want to help you, you can still follow the (correct) steps carefully.

The Bad

This error occurs when WordPress cannot access the information kept in the database via PHP (PHP is the programming language used to write the WordPress core files) broadly speaking. But to solve it, you have to get granular, which is where things get tricky.

There are plenty of reasons why a database connection error occurs—some common, some uncommon—making it difficult (and time-consuming) to identify the cause. At times, you may have to talk to your web host provider to figure things out.

A corrupt database is also a common reason for this error.

Here, you’ll have to repair your database. You need to have a basic understanding of PHP to do this. So before you proceed, familiarize yourself with the basics of the language.

Installing a reliable WordPress backup plugin is a must to save yourself from disasters. Well, error establishing a database connection definitely has the potential to become that disaster.

Although rare, it’s possible you may not be able to restore your site due to whatever reason. If you don’t have a WordPress backup in place, you’ll find yourself regretting not listening to the experts and starting from scratch.

Translation: Get a WordPress backup plugin ASAP.

Step 1 – Contact Your Web Hosting Service Provider

Like they say, why complicate simple things?

It’s possible that the error establishing a database connection in WordPress is your web host’s fault. Think about it: Your site was working just fine yesterday or even a minute ago, so it’s possible that a database server might be down, among other reasons on your web host’s end.

So, why should you waste your time trying to solve the issue when only your hosting provider can fix the problem for you? It’ll also be quicker and easier.

Contact Your Hosting Provider’s Live Chat Support

Nearly all quality hosts offer fast-reacting customer support, and live chat is the most common form.

Visit your web host’s website to contact their live chat. Talk to them, and tell them you’re experiencing an “error establishing a database connection.” Use the exact phrase.

Follow this up by asking the representative whether the database server is down or facing problems or if there’s a sudden spike in traffic or activity.

The reason behind asking these questions is to know if your database is right and whether there’s any suspicious activity on the server. Furthermore, in most cases, your hosting provider will fix the issue for you, too.

If you find the problem is at your end, or if you fail to get help from your hosting provider, you’ll have to dig deeper and do the fixes on your own manually.

Don’t be intimidated! There are plenty of solutions.

Step 2 – Check Whether Your WordPress Theme or Plugin Files Are Corrupted

Misfortune strikes without warning, and your WordPress files might just be one of its victims.

Your WordPress files can get corrupted. You may have hooked the site to an external service, hand-edited some of the files incorrectly, or maybe you updated a plugin or a theme that just collapsed.

Find yourself nodding your head to any of the above? You have your culprit.

Connect to FTP

You have to first connect your host via FTP. I recommend signing up with FileZilla, a tool that’s free and very good.

To connect, you’ll need the account credentials to your FTP account.

Go to your mailbox to find your account confirmation email from your web host when you had initially signed up to get the login and password. You can also ask the live chat support representative to help me out if you’ve deleted your old emails.

Connect to the server and go to the WordPress root folder—typically called “public_html” or “www”—once you have the credentials. It’s also possible that you might already be in it after you establish the FTP connection.

How do you know whether you’re in the WordPress root?

Check the displayed subfolder names. If you see folders like “wp-content” and “wp-admin,” you’re in the WordPress root.

Open “wp-content”

Click on the wp-content folder.

Look for the Plugins folder here; rename it. You can choose any name you like as long as it isn’t “plugins.”

See Whether the Issue Is Solved

Go back to your WordPress site, and check if you’re still seeing the error establishing a database connection message.

If not, kudos, you found the source of the problem. It was a plugin causing this issue.

You have to go back to the FTP and rename the folder back to “plugins.” Open this folder, and change the folder names of the individual plugins one by one.

We know it’s a long process, but it’s necessary.

Quick Tip: Add an “_” (underscore) at the end of the name. Simple.

While you’re doing this, keep coming back to your website to find out which plugin is triggering the problem (the error message should vanish after you change the file name of that specific plugin).

Once you find the culprit, you can either delete the folder or go online to see if there’s a plausible solution.

Repeat Process With Themes If Error is Unresolved

If you see the error even after renaming the plugins, follow the same process with your Themes folder.

If even that doesn’t help, go back and rename both folders back to “plugins” and “themes”—you don’t have any corrupt plugin or theme WordPress files. The issue is something else.

Move on to Step 3.

Step 3 – Check Whether Your WordPress Base Is Corrupted

The WordPress database has several small cogs that have to function properly to avoid trouble. It’s when one of these cogs stops working properly that you see the error message.

Your job is to put this cog back in place.

Identify the Source of the Problem

Go to your main WordPress dashboard. You can directly type yoursitename.com/wp-admin as well.

If you see the error establishing a database connection message, skip this step and move on to Step 4. If not, carry on.

If you see something else pop-up on your screen, you must understand what WordPress is asking you to do. The latest few versions of WordPress have a more expanded form of problem-solving. You’ll find some great advice here, so it’s always better to follow WordPress’s suggestions.

If you see a message that goes like “One or more database tables are unavailable…,” it means you’ll have to work on “repairing” the database.

Again, don’t panic. It’s actually relatively easy.

Repair Your Database

Connect your WordPress website via FTP.

Go to the WordPress root (just like you did before in Step 2) and download the “wp-config.php” file to your computer.

Edit the “wp-config.php” file in Notepad by adding one extra line toward the end of the code. Here’s what you need to type: define(‘WP_ALLOW_REPAIR’, true);

Once you’re done, upload the file back to your site—through FTP, of course—so that it overwrites the old version.

Check Whether the Problem Is Solved

Go to your WordPress dashboard again. This is what you should see:

You can click on either of the buttons. Go back to your WordPress website once the automatic repair finishes.

If you see nothing, congratulations! You solved the problem. However, you still have to revert your “wp-config.php“ file to its previous state. Remove the line you just added from the code, and re-upload the file.

If you’re still facing the database connection error, move on to Step 4.

Step 4 – Check Whether Your Database Connection Credentials are Correct

Before you can connect to the WordPress database, you need a specific username and password, which we discussed earlier.

The catch here is while these access credentials don’t change on their own, it’s possible that they may after you make changes to your website files, hosting environment, and so on.

To fix bad access credentials, you have to open the “wp-config.php” file first.

Fix Your Access Credentials

There are four lines in your “wp-config.php” file code that are relevant in this case. They are:

  • define(‘DB_NAME’, ‘some_db’);
  • define(‘DB_USER’, ‘some_username’);
  • define(‘DB_PASSWORD’, ‘some_pass’);
  • define(‘DB_HOST’, ‘localhost’);

If you go through the above code line, you’ll find that they hold all the information WordPress needs to connect to the database. There won’t be any connection if any of this goes wrong.

Copy the four lines from the “wp-config.php” file, and paste it on the Notepad app.

Contact Your Hosting Provider’s Support Team

Get in touch with your web host support through live chat, and paste the four lines in the chat window from Notepad. Ask them whether the details are correct to connect to your database.

The representative will cross-check the details and let you know the correct credentials in case anything has changed. Make a note of your new account credentials, if applicable.

Update Your Database Connection Credentials

Replace your old account credentials with the information you just received from your web host provider representative in your “wp-config.php” file.

Here’s what it should look like:

define(‘DB_NAME’, ‘yournew_db’); /* this is the name of your WordPress database */

define(‘DB_USER’, ‘yournew_username’);

define(‘DB_PASSWORD’, ‘yournew_pass’);

define(‘DB_HOST’, ‘localhost’); /* this is the web address of your database server */

Once you’re done, re-upload the file through FTP.

Go back to your site to see whether the error has been fixed.

Are you still facing problems? Ugh, frustrating. But I have another trick up my sleeve.

Step 5 – Restore the Default WordPress Files

There are tons of reasons why your core WordPress files get corrupted—modifying files accidentally, being a victim of a malicious hacker, rogue themes, plugins, etc. The list is never-ending.

The good news is whatever the cause, you can always restore the default WordPress files, a.k.a. the native files. Here’s how you go about this:

Restore the Native WordPress Files

Type in www.wordpress.org in your web browser.

Download the newest version of WordPress to your desktop, and then unpack it.

Once that’s done, go through the unpacked archive and delete the “wp-config” folder. Take a note I’m asking you to delete “wp-config” and not “wp-config.php.”

Upload the New WordPress Files

Connect your WordPress website via FTP, and go to the main WP dashboard. Upload the recently downloaded WordPress files here so that it overwrites the previous files.

Once done, return to your site to see whether your WordPress site is (finally!) working properly.

If you’re still facing a problem, unfortunately, this was the last resort. You have no other choice other than to contact your hosting provider support team, explain everything you’ve done, and ask for help.