How to Fix the Error Establishing a Database Connection in WordPress

Fix the Error Establishing a Database Connection

One of the worst things that any WordPress website owner wants to see is this message, “Error establishing a database connection in WordPress”.

Here’s how the issue crops up, WordPress uses both PHP and MySQL database, and rather it uses php commands to communicate with MySQL database to connect with the same and to retrieve the required information.

Often, when WordPress fails to retrieve the required information, which does happen from time to time, it displays the error message shown above.

There are at least five reasons that this could have happened and one of the predominant reasons is that your WordPress files have become corrupted which is why the error message is showing up at this juncture. Let’s take a look at some of the reasons and see how this issue can be resolved at the earliest.

But keep in mind that if the issue is not resolved, even after you have carried out all the steps listed below, that means that the issue is likely to be more serious.

In which case, you may need to replace your WordPress files altogether. Just check out the various reasons that this could have happened and what you need to do next.

How to Fix the Error Establishing a Database Connection in WordPress

Here are some of the common reasons as to why this issue may have cropped up –

Incorrect login credentials:

incorrect login credentials
One of the reasons that your WordPress is displaying the error could be on account of incorrect login credentials. If you do not enter the correct login credentials then WordPress cannot retrieve any information and as a result, it may display the error shown above.

Corrupted WordPress files:

corrupted WordPress file
this can happen; your core WordPress files can get corrupted on account of a failed update as a result of which they may fail to work properly.

This can include failed updates for core files, plugins, widgets, and pretty much everything else. When this happens, the theme in question will no longer be able to function effectively and WordPress would not be able to retrieve any information.

Corrupted database:

corrupted database
This can happen as a result of malicious attacks launched by hackers or due to some rogue plugin. In either case, the database becomes corrupted and WordPress would end up displaying the same error message all over again.

Database server is down:

database server is down
This happens more often than you would think; for some reason or the other, your database server files could be stored on a different machine than your web server.

And the database server could be down for a variety of reasons which is why you are seeing the error message listed above.

Too much traffic:

too much web traffic
This is one of the common causes that lead to the “error establishing connection with database”. It often occurs when your website receives vast amount of traffic, so much so that it is no longer able to cope up with the same. It sounds similar to a DDOS attack and pretty much can lead to your server crashing altogether.

Now, that you have viewed some of the common reasons as to why this error cropped up, let’s see how to go about resolving the same at the earliest.

Related Post:How to Fix the 500 Internal Server Error on Your WordPress Website

1. Check with your web host:
If your server was fine a minute back and it is displaying the “error connecting with database”, then the first thing that you need to do is to contact your web host and alert them regarding the problem.

Most web hosts often offer excellent customer services and they would look into the issue, even do an analysis and let you know if the issue is at the web host end or at your own.

You can count on getting a definite response with a quick turnaround; but instead of just shooting them an email, see if it is possible to connect with them via chat so that they can walk you around as to what you need to do next.

You can inform them that your database server is up and running and that there has been no uptick in traffic to your website recently which is why you are contacting them regarding the same. They should help fix the issue and if there is no resolution, then it is time to go to the next step.

2. Check and see if your plugin or theme files have been corrupted:
There are any number of ways by which your plugin or theme file could become corrupted. You may have used an external plugin that has not been vetted or for that matter, have hooked up some external service to your database.

This can lead to your files being corrupted but you need to double check and make sure that this issue is the one causing the problem.

Just connect to your host via FTP, use Filezilla. Now, you need to login with the credentials that you got from your web host; once you do, connect to the server and to the root folder.

It is usually called ‘public.html’ or ‘www’. You should see files such as wp-admin, wp- content, head over to the wp-content file and rename the plugins folder and see if that takes care of the issue that you are facing at the moment.

If it does, that means that some of the plugins you are using is causing this error to creep up. Now, all that’s left is to identify the rogue plugin, so head into the plugin file and change the name of each plugin by adding ‘-‘ and reloading your site to see if the issue clears up.

If it does, then you have your answer and you can go ahead and delete the plugin in question.

3. Check and see if your database has been corrupted:
WordPress comes with a massive database with lots of tiny cogs working in unison. And when something goes wrong, the error listed above crops up which is reason enough as to why you should have to check your database often to ensure that it has not become corrupted.

First head over to your WordPress dashboard; if you see the same error as listed above, then you may want to skip this message. However, if you spot a different message, then you need to connect with your database, head over to your root and download the wp-config file.

Once you have download the same to your system, open it in your text editor and add this code right at the end, just before “that’s enough, let’s stop editing”.
define(‘WP_ALLOW_REPAIR’, true);

Once you have included this code, upload the file back to your root via FTP and what this does is that it would allow WordPress to carry out repairs on your site in an effort to put your site back to normal.

Now head over to Go to YOURSITE.com/wp-admin/maint/repair.php, it should provide you for options on automatic repairs, choose yes and once the repairs are done, just head back to your main site and see if it is up and running. If it does not, then carry on to the next step.

4. Check your database credentials:
The database credentials do not change but it can malfunction from time to time. The fact is that WordPress needs specific credentials to be able to retrieve any information.

Just head over to your root, and check out the wp-config file. The codes below should be displayed just the way they are, in your wp-config file.

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

These hold all the required information that WordPress needs to connect with its database and if any of this is wrong, then no connection will be formed. Here’s what you need to do, copy the four lines of code from your wp-config file, contact your web host and ask them to confirm if the same are correct.

They will respond soon and let you know if the information is accurate or not and if not, then you can make out the required changes at your end, and upload the file back, using FTP.

5. Restore the default files:
If none of the steps listed here have worked, then what you need to do is to contact an expert or just restore the default files. That should take care of the problem and you can then customize and tweak it as per your preference.

All you have to do is to head over to WordPress.org and download a clean version of the various files.

All that’s left is for you to copy the same over your old files and this should provide you with an effective reset by which your site should be able to connect with your database and be able to successfully retrieve the required information.

Thus using all of the above error establishing a database connection can be resolved easily. Using the above steps one can check the various problems and get rid of the issues of the same.

About Sonl Sinha

Sonl SinhaSonl Sinha is a passionate writer as well as WordPress and WooCommerce rockstar who loves to share insights on various topics through his engaging blog posts. He runs a successful website design and digital marketing company. With 15+ years of experience in WordPress theme development, he strives to inform and inspire readers with his thought-provoking content. He helps thousands of small and medium businesses and startups create a unique online presence. Follow Sonl Sinha for your regular dose of knowledge and inspiration.

Do check out our free WordPress themes and WordPress themes bundle