How to Fix “Error Establishing a Database Connection” in WordPress?

 What Causes the Database Connection Error?

To give you an insider’s understanding of the “EEDC” error, we first need to break down WordPress into its two fundamental components.

WordPress is built using PHP (e.g. index.php, sidebar.php etc) and MySQL. PHP is in charge of all functions on your WordPress site (e.g. posting comments, retriving posts and pages etc) and MySQL is in charge of data storage.

For instance, when you type your URL in the address bar and hit enter, PHP kicks in, fetches and displays the contents of the homepage (index.php) from your MySQL database. Images, videos and such media are, however, stored in the wp-content folder, from where they’re retrieved by PHP.

Your MySQL database contains all the vital information PHP needs to create your site. Now, when PHP can’t access your MySQL database for one reason or another, you get the dismaying “error establishing database connection” message on your screen. Which is just PHP-speak for I couldn’t find the data I needed to put together you WordPress site. Here’s an error to show you I tried.

That said, the “EEDC” error can be caused by a number of reasons including but not limited to:

  1. Login Credentials

Your database requires two separate items authenticate any user, WordPress or otherwise. That’s your login name and password. If those credentials have been changed, your database can’t be accessed, and no info means no page.

  1. Corrupt Database

It does happen, for several different reasons, from malware or bad plugins, to problems at your host server (hopefully a reputable host company will have things running again ASAP).

  1. Corrupt Files

Sometimes a single php file can have internal errors, if for instance you’ve had some kind of malware or a bad plugin that doesn’t work quite like it’s supposed to. Sometimes it’s not the php but another file the php is asked to open as part of your page.

  1. Server not Available

Sometimes the server just can’t be reached. It may be down for maintenance, overwhelmed with traffic and unable to support new connections. Maybe there is a problem with your ISP and Internet connection, or browser.

Some of these things may be beyond your control, so how can you identify the issue and fix it?

 

How To Fix “error establishing a database connection”

Since I value your time, I’m going to first recommend a path that’s the most likely to fix the problem for you quickly. Then, I’ll dig deeper and show you how to do all the fixes on your own manually.

Step 1: Get In Touch With Your Web Host Provider

Contacting your web host support is always a good idea…

It’s safe to assume that if your site was working just a minute ago (or a day ago) and now it isn’t then the fault can be on your web host’s end.

All quality hosts these days offer some form of fast-reacting support, usually through live chat.

Start by contacting that live chat, say that you’re experiencing an “error establishing a database connection” (paste the phrase), and ask:

  • if the database server is up and running with no problems, and
  • if there isn’t any spike in activity or traffic on the server right now.

In most cases, if you tell them what sort of error you’re having, they will either let you know what the cause is exactly, or they will even fix it for you.

Most importantly, they will let you know if your database is alright and whether or not there’s any suspicious activity on the server.

If the fault is not on their end (the server is operational and there are no spikes in traffic), and they don’t want to help you, proceed to step 2:

Step 2: Check If Your Plugin or Theme Files Haven’t Been Corrupted

Your WordPress files might get corrupted as a result of a couple of misfortunate events.

For instance, maybe you’ve updated a plugin or a theme, and the whole thing collapsed? Maybe you hooked the site up to an external service? Maybe you hand-edited some of the files?

If any of the above ring a bell, you probably have your culprit. If you haven’t done any of that, skip this step.

Here’s what you can do:

  • 1. First, connect to your host via FTP. You can do this with a free tool called FileZilla.

In order to connect, you need a login and a password to your FTP account. If you don’t know what/where those are, the easiest way to get them is to search through the initial emails that you got from your web host when you first signed up. Alternatively, you can ask the live chat support representative to give you those details.

Having them, connect to the server and navigate to the WordPress root folder. It’s usually called “public_html” or “www” or you might be in it already once you establish the FTP connection.

The rule of thumb is that if you can see sub-folders like “wp-content” and “wp-admin” then you’re in the WordPress root. Example:

  • 2. Go into “wp-content” and rename the “plugins” folder to something else (whichever other names, as long as it’s not “plugins”):
  • 3. Go back to your site and see if the error is still there.

If not, you now know that it’s been caused by some plugin. Go back to FTP, rename the folder back to “plugins”. Go in it, and change the folder names of the individual plugins, one by one. The easiest way to do this is to add an underscore at the end: “_”

Keep coming back to your site to see when the error gets triggered or vanishes. Once you’ve narrowed it down to one specific plugin, you have found the troublemaker. Either delete it or go online to see if the solution has been documented.

  • If the error is still there even after renaming the “plugins” folder, go through the exact same process with the “themes” folder.
  • If that doesn’t help, rename both folders back to “plugins” and “themes”. The error is caused by something else.

Step 3: Check If Your Database Hasn’t Been Corrupted

The WordPress database is a fairly complex thing, and there are a lot of small cogs that need to stay working together perfectly to avoid trouble. At times, something goes out of place and that’s when you see the error establishing a database connection.

  • 1. The first thing to check is to go to your WordPress dashboard – YOURSITE.com/wp-admin

If you see the same main error: “error establishing a database connection”, skip this step.

  • 2. If you see something else, pay close attention to what WordPress is suggesting you should do. In its recent versions, WordPress has expanded its problem-solving solutions, so you might see some good advice there. One of the possibilities:

Another possibility is that you’ll see a message starting with “One or more database tables are unavailable […]” This means that it’s you who needs to “repair” the database.

This is actually quite simple. First, connect to your site via FTP once again. Go to WordPress root and download the “wp-config.php” file to your desktop.

Edit this file in notepad, add one extra line at the end (right before “That’s all, stop editing”):

define(‘WP_ALLOW_REPAIR’, true);

Now upload the file back to your site (via FTP) overwriting the old version.
Go to YOURSITE.com/wp-admin/maint/repair.php

This is what you should see:

Click either of the buttons. Once the automatic repair finishes, go back to your site and see if the error is still there.

If no error, great! But you still need to revert your “wp-config.php” file to its previous state. Remove that new line that you just added and re-upload the file.

If the error’s still there, next step is:

Step 4: Check Your Database Connection Credentials

As I mentioned, WordPress uses a specific username and password to connect to the database. Normally, those access credentials don’t change on their own. However, you can experience some weird scenarios after making any changes to your site files, the hosting environment, or anything else. In other words, even though it shouldn’t be happening, the access credentials sometimes do change. And with bad access credentials – “error establishing a database connection” happens!

The fix is done in the “wp-config.php” file. I’m talking about these lines specifically:

They hold all the details that WordPress needs to connect to the database. If any of these are wrong, no connection will be established.

Note that there is a manual way of fixing this issue, but it honestly requires multiple steps and can be really confusing based on the web host you use and the interface they employ. In other words, anything I’d say here would still only be applicable to a small range of users who just happen to be using the same host as I am, so I have a much more universal solution for you:

Take those four lines from the “wp-config.php” file, simply copy them to the clipboard, contact your web host support again, paste those lines in the chat window and ask if the details are correct for you to be able to connect to your database.

The representative will check everything, and come back to you with some feedback. Most often, they will provide you with the correct details in case anything has been changed.

All you need to do is take the new credentials and put them in your “wp-config.php” file where the old ones used to be.

Finally, re-upload the file via FTP.

At this stage, you can go back to your site and see if the error has been fixed.

If not, I have one final step for you:

Step 5: Restore The Default WordPress Files

As I mentioned before, the core WordPress files can get corrupted for a number of reasons. Maybe there’s been a hacking attempt on your site, maybe you modified some files by accident, maybe a rogue plugin did the modification.

Whatever it might be, you can still restore the default, native files of WordPress. The easiest way to do that is to just download a clean version of WordPress from WordPress.org and copy it over the one you have right now.

Go here, and download the newest package to your desktop. Unpack. Browse through the unpacked archive and delete the “wp-content” folder.

Next, connect to your site via FTP, go to the main WordPress root, upload your new WordPress files. Overwrite all the previous files.

When the upload is done, go back to your site and see the effect.

This is a last resort kind of fix. If this doesn’t help, you will have no other choice than coming back to your web host support line, tell them everything that you’ve done and ask for help. At this stage, you’ve basically exhausted everything you could do to fix the issue yourself and it’s the host’s turn to solve it. You’re paying your hosting bills after all, aren’t you?

Related posts