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

While working with wordpress blog , we sometimes face with a common problem and that is not related wordpress design , not related to wordpress theme , even that is not related to any third-party Plugin, That issue exists inside the database itself .

Common error we face is nothing but the “Error establishing a database connection”

Sometimes I think WHY this errror occurs even after installing the wordpress and custom themes and plugins , All these i can install or configure because database is working perfectly.

So where is the issue persist, issue persist within, within database itself and sometimes we injected un-intentionaly.

So let take a ride how these issue persisit and it occurs sometimes but not always.

WordPress uses two main pieces of technology to provide you with all its awesome abilities: PHP and MySQL.

PHP is a programming language. WordPress – the core WordPress files – have been written in PHP. Boring, sorry.

MySQL is a database technology. WordPress uses a MySQL database to store all of your website’s content. This includes your posts, pages, but also the smaller elements, such as the title of your site, the layout of your widgets, the color settings, and etc. Basically, the MySQL database is where every, even the smallest piece of information about your website, is kept.

This is where the error comes into play. The error occurs when, for whatever reason, WordPress cannot access the information in the database via PHP commands. When that happens, WordPress basically doesn’t know what to do next, so the only thing it can say is, “Error establishing a database connection.”

With that being said, there can be a handful of reasons why the error occurs. Some of the more common ones:

  1. Incorrect login credentials. WordPress needs a specific login and password to access the database. If those have changed for any reason then WordPress will no longer be able to fetch anything from the database.
  2. Corrupted WordPress files. The core WordPress files can get corrupted as a result of a failed update, among others. This can include things like failed updates of the plugins, themes, and even the main WordPress software updates.
  3. Corrupted database. This might be a result of a rogue plugin messing up the database internally, a hacker’s attack, a theme failing, and many more.
  4. The database server is down. In some cases, the web server – the server that stores your website files – is run on a different machine than the database. And that separate database server can simply be down for whatever reason.
  5. Too much traffic. Even though this is a good problem to have, it’s still a problem. Specifically, your database might be unresponsive due to a high spike in traffic. For example, maybe one of your posts went viral and now everyone and their dog tries to access your site? Hence the server is not able to handle part of those visits.

How To Fix ?

Get In Touch With Your Web Host Provider

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.

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

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:

First: connect to your host via FTP. You can do this with a free tool called FileZilla.
Second: Go into “wp-content” and rename the “plugins” folder to something else (whichever other names, as long as it’s not “plugins”):
Third: Go back to your site and see if the error is still there.

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.

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.

Secondly, 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:

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”):

Now upload the file back to your site (via FTP) overwriting the old version.

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.

Check Your Database Connection Credentials

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. We’re talking about these lines specifically:

Restore The Default WordPress Files

As we 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.

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.