Establishing a Database Connection in WordPress, How to Fix this Error

Establishing a Database Connection in WordPress, How to Fix this Error

Error establishing a database connection is one of the most dreaded errors in WordPress nobody ever wishes to experience.

If you’ve never experienced it, good for you. But if you have, you will agree that it is mind-numbing and scary.

Database connection error, as the name suggests, happens when there is a disconnect between your WordPress website and the database holding it.

When this happens, this is what your visitors will see on their screen:

Establishing a Database Connection in WordPress, How to Fix this Error

It is as good as your website going down, because everything will stop working.

Thankfully, database connection errors in WordPress can be fixed, and we are going to show you how to go about it in this post.

But before getting to that, we will dig in a little to fully understand what this error is all about, and what causes it.

What is Error Establishing a Database Connection?

So, earlier we briefly explained what database connection error in WordPress is. While this might be enough, a deeper definition of it will help you have a better grasp of it.

MySQL database, along with PHP, is one of the core technologies that powers WordPress. As you would expect, that is where most of your website’s data are stored:

  • Post
  • Pages
  • Page title
  • Page settings
  • Login credentials
  • Etc

When a user loads your website, what PHP does is to query your database, find the data the users needs, and serve it on the frontend.

But if PHP is unable to connect to your website’s database for whatever reason, the result is Error establishing a database connection.

Not only will your visitors see a blank screen with an error message, you will as well lose access to your WordPress website.

What Causes Database Connection Error in WordPress?

There is no singular cause of connection error in WordPress. As such, whenever you experience this error, it might be that:

  1. Your login credentials are incorrect: This, oftentimes, is the major cause of database connection error. It usually happens when you move your WordPress website to another host. More details shortly.
  2. Something has corrupted your WordPress database: If there is a poorly coded theme or plugin on your website, it can corrupt your database. Also, failed WordPress updates can also do the same.
  3. The sudden surge in traffic: If your website’s traffic suddenly spikes, the webserver where your website lives will likely take a hit, thus leading to database connection problems.
  4. Your database server has gone down: In this case, the fault is coming from your hosting provider, and not your end. Typically common on shared hosting.

With all that said, let’s get to the main focus of this post: how to fix database connection problems in WordPress

How to Fix Error Establishing a Database Connection

If you are currently experiencing this error, or whenever you do, the very first thing we recommended doing before attempting to fix it is backing up your website.

There are a couple of ways to go about that:

  • Using a plugin
  • Manually by creating a copy of the website on your local machine
  • Backing up on the server-side

With that out of the way, here are hacks for fixing database connection error in WordPress

Solution 1: Ensure Your Login Credentials are Correct

An obvious place to start troubleshooting, isn’t it?

If the login credentials WordPress uses to access your database changes for whatever reason, you will experience this error.

Typical reasons for this include a failed update, change of web host, etc.

To correct this, you will need to access the wp-config.php file of your website. This is where the database login credentials are stored.

But how can you access this file since your website is down in the first place?

Simple. Via an FTP connection.

Once you have established an FTP connection, navigate to your website’s main folder. There you will find the wp-config.php file.

How to Fix Error Establishing a Database Connection

Open it.

Once opened, locate the following:

  • The name of the database for WordPress
  • MySQL database username
  • MySQL database password
  • MySQL hostname
MySQL database

This is where you will be inputting your database login credentials.

But what if you don’t know your login credentials? Well, you can still find them.

So, with the config file still open, follow the steps below to retrieve your login information.

Step one: Locate your MySQL database page. You can do so via the cPanel.

MySQL database page

Next, click to open it.

Step two: with the page opened, check your database username under Current database

WordPress database

Note it down somewhere.

Do the same for the username by checking under Privileged users. Also note that down.

If you are not sure of your password, click on the username. This will take you to your cPanel login page, from where you can reset your password.

Then head back to the wp-config file that is still opened. Check the information it contains against the ones you noted down.

If there’s a mismatch, adjust accordingly.

Save the config file simply by hitting the Ctrl + S keys on the PC, and Command + S on the Mac.

This will automatically update the config file.

Head back to your website now, reload it and see if the error has cleared.

If it hasn’t, then two possible reasons: your login credentials are still incorrect or something else is causing the error.

At this point, it’s best you reach out to your hosting provider and ask for help retrieving your login credentials.

If the problem still persists after doing so, move to the next solution.

Solution 2: Disable Your Plugins

A poorly coded plugin, as we earlier mentioned, can corrupt your database, and hence bring down your website.

It then follows that disabling all the plugins on your website can help fix database connection error in WordPress.

You will still need an FTP connection since you no longer have access to your WordPress dashboard.

Once plugged in, look for the wp-content folder, for it is where your plugins are stored. You will find it in the public_html folder.

Disable Your Plugins

Double tap to open it, and then locate the plugin folder. Rename the folder to something else. Say “plugin_testing”.

When you do this, WordPress will assume that all your plugins have been deleted, and so disable all plugins.

Now, head back to your website, reload and check if the error has cleared. If it has, apparently, a problematic plugin has been the culprit all the while.

But how would you know the plugin, exactly?

Open the plugins folder, rename all the child folders to something else by adding, say, an underscore.

For example, change “askimet” (if you have it as a plugin) folder to “askimet_”.

Do this one at a time for each plugin, and each time you do head back to your website and refresh it.

The problematic plugin is the one that will cause the error to resurface again after you’ve renamed it.

You sure know what to do from here, don’t you?

If after disabling all plugins nothing changes, it’s about time you tried another solution.

Solution 3: Repair Your WordPress Database

So, if disabling your plugins didn’t help to fix the database connection error you are looking to get rid of, making direct repairs to your database can help.

This will particularly work if WordPress keeps throwing the following error when you are attempting to log into your dashboard.

“One or more tables are unavailable. The database may need to be repaired”

To repair your database, open your wp-config file, like you did in aforetimes. Then copy and paste the following line of code somewhere at the bottom of the file.

define(‘WP_ALLOW_REPAIR’, true);

Save the file, but don’t close it yet.

The next step is to run the repair command. To do so, copy and paste the following link in your browser:

Of course, you need to change the “mydomain” here to your actual domain.

Doing this will bring up the following page.

Repair Your WordPress Database

Click the “Repair Database” button.

WordPress will automatically repair your database, if it was corrupted. Once the repair process has completed, check to see if the Error establishing a database connection message has cleared.

If it has, head back to the config.php file and delete the code you just pasted. Once you do, you are all set.

Still having the same problem? Try the next solution

Solution 4: Contact Your Hosting Provider

The database connection problem ought to have cleared by now. But if for some reasons it hasn’t, your best line of action is to get in touch with your hosting provider.

They are in the best position to know what, exactly, that is causing your website to throw up database connection problems.

It might be that your server is down, which, of course, is none of your fault. It could also be that your website is experiencing a surge in traffic.

Whatever be the case, your hosting provider should be able to help. Or, at least, make helpful suggestions.


With all we’ve discussed in this post, you don’t have to panic should you experience error establishing a database connection in WordPress.

And as you might have noticed, fixing this error is no rocket science. It’s something anybody can do.

We hope you found this post helpful.

Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124