It doesn’t often happen, or rather, shouldn’t, but one of the WordPress Errors we sometimes encounter is the “Error Establishing a Database Connection”. Let’s take a look at this, and see how we can fix it.

7QjjuNNTTvitY8KTnOtE

No worries, it’s usually easy enough to fix.

Why does the Error Establishing a Database Connection happen?

Every WordPress website consists of two parts: one part is where all the files are contained, with your plugins, themes and uploads; the other is the database.

Your database is where your website’s contents and settings are saved.

In order for your WordPress website to work properly, its files need to constantly access and edit the database.

Each and every time a visitor visits your WordPress website, any page of it, your files will need to access the database (cache makes it an exception and you might not immediately see the error, but this is not about that).

If WordPress can not reach your database then, you end up getting the “Error establishing a database connection” screen.

It’s almost time to be investigators and understand why WordPress cannot reach the database.

As with most things that have to do with interacting with files or databases, it is highly recommended to have a backup available. 

As a common practice, we should always have backups available. For more on backups, read our How to Backup WordPress guide.

Where is WordPress Database Information Stored?

Before we investigate, we need to know where WordPress stores database information: it’s a file in the root folder, called wp-config.php.

You can connect via (s)FTP, SSH or your hosting panel File Manager. Check out our guide on how to connect to your WordPress files, to know more.

In the wp-config.php file, right at the beginning, we’ll see these lines: they contain your WordPress database name, username, password and server information.

inH9ayqRCCIjw8roOXjA

Now we are ready to investigate and fix the WordPress error.

A Few Common Reasons for the Error Establishing a Database Connection

We’ll look at them in detail, but often enough there are just a few common reasons and you can quickly fix the error and get up and running just by knowing off the top what could be.

  • Database credentials are incorrect
    (DB Name, DB User, DB Password or DB Host)
  • Database is corrupted
  • Files are corrupted
  • There are issues with your DB Server
    Any number of things can happen to your DB Server, from being overloaded to being offline

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

Here we are, time to investigate! Let’s go over those various possibilities in detail

Check WordPress Database Credentials

It is very common when there’s a database connection error in WordPress that your database information is incorrect. While that would not usually happen on its own, it is very common if you have recently moved your WordPress website to another hosting provider. 

By moving WordPress to another host, you would move both files and the database, so the connection details to your new database could be different.

In order to check this out, open your wp-config.php file, which is in the root WordPress folder, as we have seen.

KeR9neERpuAcRB4aYUdf

Once that’s done, just at the beginning you will find your Database Configuration settings:

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'database name' );

/** MySQL database username */
define( 'DB_USER', 'database username' );

/** MySQL database password */
define( 'DB_PASSWORD', 'database password' );

/** MySQL hostname */
define( 'DB_HOST', 'localhosts' );

That’s where you need to make sure the information is the same as your host. Depending on your hosting control panel, there are many ways to check this information. If you need specific details for a certain web host, please ask us in the comments or check your host documentation.

One more thing to note: Most often the MySQL hostname line will be localhost, but there will be exceptions: you should make sure by checking your hosting provider documentation.

If you find the mistake you can simply add the correct information, save the file and that should do it!

Check your Database Server

If your credentials seem correct or you have not changed anything recently nor moved your WordPress install to another hosting provider, something could be going on with your Database Server.

Any number of reasons can cause your Database Server to be down: a spike in traffic, or lack of resources (especially common with shared hosting providers). Often enough, your site will be really slow and the issue fixes itself overtime. But sometimes there’s just too much traffic or too many processes get stuck and your server will go down.

Having an optimized website with a good hosting provider and using the right size plan or VPS is usually enough to avoid issues of this type, as well as having autohealing features and processes in place on your host, but sometimes it just happens. As long as this does not happen too often, it is ok.

All we have to do is check whether our Database Server is down, and if it is, try to turn it on. If we can do that, then that most likely solves our issue. If we cannot, then we should open a ticket with our hosting provider.

In order to check whether the Database Server is up, we can check with our Hosting Provider Panel, or connect via PhpMyAdmin and see whether we get an error. This all depends on the tools at our disposal, which varies between hosting providers.

If this seems to work, we can then check if the database user has enough permission rights. To do so, create a file called testdb.php in your root WordPress folder, and paste this code:

<?php
$link = mysqli_connect('localhost', 'db username', 'db password');
if (!$link) {
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully';
mysqli_close($link);
?>

Make sure to change db username, db password with the correct database credentials, then save the file and browse to yourwebsite.ext/testdb.php.

If you get this message, you should probably check against Incorrect Database Credentials once again:

iFkv78gRmKqfFMNHKX7V

You can always change/reset your Database Credentials, so if you do not know or remember them, your hosting panel can surely point you to the right place, as well as your hosting provider.

Whereas, if you get this, your database can connect properly and you have the right permissions. You should be good to go! If you still have the same error, check the other possible causes.

LfgYrPTFi5SMatlnoqQA

Remember to delete the testdb.php file once you are done testing

Repair WordPress Database

If you go into wp-admin rather than the normal website, and you get a different error along the lines of “One or more database tables are unavailable. The database may need to be repaired.”, then there’s a different procedure, and we’ll look into repairing the WordPress database.

It’s not something we have to do manually, fortunately. WordPress has a database repair functionality and we only have to enable and activate it.

In order to do so, open up your wp-config.php file once again, and just at the end, before it says

/* That's all, stop editing! Happy blogging. */ 

add this line:

define('WP_ALLOW_REPAIR', true);

This will enable the WordPress Database Repair functionality, and we can get to it by browisng to https://yourwebsite.ext/wp-admin/maint/repair.php. 

If you go to the repair.php url and you get the same “Error Establishing a Database Connection”, then it most likely means this is not the problem and you should check the other solutions we write about.

However, if you do get this as with the screenshot, you can proceed with Repair Database

8VcYIvkER2a0ngVHZAU3

Note: The user does not need to be logged in to use this feature. It is VERY important that once you’re done with it, you remove the line you added to your wp-config.php file.

FIX Corrupt Files

It can be it’s not about the WordPress Database, but your files are corrupted. That could be for many different reasons, but we suggest using WAF and an antivirus.

The way we can fix Corrupt Files is to simply upload original WordPress files via (s)FTP and overwrite what’s there. No worries, all your settings, data and posts will not change: WordPress core files are never changed and should never be changed.

Let’s head to WordPress.org and grab a copy of WordPress to download.

Once we do that, we can unzip the file and delete the wp-content folder, as well as the wp-config.php file.

We can then upload it via (s)FTP and overwrite the existing files. This should ensure our files are not corrupt.

You Could Also Try…

As this pertains the Database Server and its connection with WordPress, there are several other things that could be done, depending on your host. One among the others is if you have a VPS, you could simply restart your VPS.

Restore Backup

If you still cannot find the culprit, your Database Server is up, you have the Correct Credentials in your wp-config.php and your permissions are correct, your database has been repaired and you have no corrupt files, you can always restore from a backup.

Some backups are done via WordPress plugins, so that is not helpful in this case. But depending on your hosting provider or panel, you might (and should!) have other forms of backups. If you do, restoring is a viable way to fix the issue, but only if you’ve checked the possible causes! Otherwise it might not change anything.

Conclusion

There are many ways to fix the “Error Establishing a Database Connection” in WordPress, and most are very quick and easy. 

As with most things, we highly recommend having multiple layers of backups available, as well as using a good hosting provider, in order to mitigate this issue and others.

If you find you have this issue repeatedly due to your Database Server crashing, you should open a ticket with your hosting provider, consider a different host or a bigger and beefier plan.

We hope this has been helpful, have you had this issue before? Were you able to fix it?

0 0 votes
Article Rating