There are only a few actually scary WordPress errors that could seriously impact your business. The first one is, without a doubt, the dreaded white screen of death. The second scariest error? That would be the WordPress error establishing a database connection.

Your entire WordPress website relies heavily on the database for the most vital functions:

The CMS stores all the content, posts, pages, and login credentials on the database. So, if your website fails to connect to the database, it’s rendered useless.

WordPress is one of the most resilient and flexible CMS platforms in the world – and that means there is no error you can’t fix. So, in this article, we will discuss what this database error is, what causes it, and how to fix it.

Let’s start with a short introduction to WP databases and go on from there.

What’s A WordPress Database?

Most people think of a database as a complex system of files. However, that couldn’t be further from the truth.

A database is just a table that stores data such as blog posts, titles, pages, colors – and much more.

WordPress can then access the data in the database and display it to your website’s users. The database is pretty snappy and robust – until it stops working, that is.

If you’re getting an “error establishing database connection” error, your WP website has trouble retrieving data from the database.

What Is an Error establishing database connection?

Most of your WordPress website’s data is stored on your database. That can include blog posts, pages, login credentials, colors, settings, and much more.

The only data not stored on the database is the media – such as images, videos, and particular files.

When someone opens your website, PHP runs a code that quires the database and displays the data to the visitor.

If there’s a break in the connection between the website and the database, you will get a blank page with a message “Error establishing a database connection.” The page is blank because the website failed to retrieve any data from the database. That not only messes up a website’s front-end but also your access to the admin dashboard.

Visitors might not spot this error right away. That is because your website’s still cached on the server. In most cases, the site gets recached every hour.

When caching expires, though, visitors will get the “500 Internal Server” error – the same error code you get when there’s any problem with the server.

What Causes The Error Establishing Database Connection in WordPress

As it’s the case with most WordPress errors, this error might be caused by multiple factors. Here are the four common causes of the database error issue:

Incorrect Database Login Credentials

Incorrect database login credentials are hands down, the most common cause of database connection issues. Simply put, WordPress doesn’t have the correct credentials for the database.

The incorrect information can be either:

  • Database name
  • Username
  • Password

Do note that database login credentials are not the same as your backend login credentials.

Corrupted Database

Your WordPress database contains tons of information. If any of this information gets deleted or corrupted, this could be the root cause of your issue.

Database corruption happens due to manual tinkering – and even due to regular use. Although MySQL databases are pretty robust, they’re not foolproof.

Another reason your database got corrupted could be bad server drives. However, that is rarely the case because most data centers have redundancies to prevent data corruption.

WordPress Core File Corruption

WordPress core files determine how your website functions. There are many reasons why these files got corrupted.

Anything from faulty plugins and themes to errors during FTP transfer could cause the core WP file corruption.

Server Issues

Sometimes it’s not your fault you’re stuck with this error. Several things could go wrong with the web hosts’ server, including traffic spikes, lack of resources, and even routine maintenance.

If you receive a WP error message like the one below when trying to open the dashboard, you’re probably dealing with incorrect login credentials.

Error Establishing a Database Connection

If you’re getting a different message, the cause lies somewhere else. The error is probably caused by a spike in traffic or a corrupted database.

Fixing WordPress database connection errors takes a couple of steps. The following section will show you a couple of different ways you can fix this error.

How Do I Fix Error Establishing A Database Connection In WordPress?

Even though WordPress database connection error is pretty serious, you can easily fix it. Here’s how to resolve this issue.

#1 Ensure Your Domain Is Pointing To Your Hosting Server

When you switch WordPress hosting providers, the provider will automatically migrate the files to their server. However, if the domain is not pointing to the right host where your database is stored, it can cause a database error.

To see if this is the case, you need to check the nameservers via your hosting control panel and WHOIS lookup tools. Login to your WordPress hosting backend and go to Account Details.

Now, see if both nameservers match each other. If that’s the case, everything is working as it should. However, if the names don’t match, you must change the nameservers at your domain registrar.

Log in to your hosting dashboard, then retrieve the list of nameservers. Once you get the nameserver list, you need to put in the nameservers into your domain registrar control panel.

Another way to point your domain to the right hosting server is via the A record. It’s a type of DNS record that assigns domains to their correct IP addresses. Please note that the A record can only point to a static IP, though.

To do that, you first have to get your website’s current IP or the A record value. Once you get the information, you need to replace it with the one from your new host.

You can do that by either using your local PC or an online DNS checker.

#2 Check Your Database Login Credentials

If everything is fine with your domain, you’re likely having trouble with incorrect login credentials. That usually happens when you switch to a new hosting provider.

All connection details are located in the wp-config file. This file is located in the root directory of your WordPress website. It contains important information that needs to be correct so that there are no errors.

Here is the information you need to check and see if it’s correct.

Database Name

Database Name

MySQL Database Username

MySQL Database Username

Database Password

MySQL Database Password

MySQL Hostname

/** MySQL hostname */

define(‘DB_HOST’, ‘example hostname’);

You can access the wp-config file by using the hosting provider’s file manager or via the FTP.

Here’s what the file looks like:

MySQL Hostname check to fix wordpress error establishing a database connection

You should check if the database username, password, and hostname are the same as those on your database server.

Here’s how you can do this in cPanel.

Checking Database Credentials in cPanel

First, login into your host cPanel. Once in, check the database name:

Open phpMyAdmin, and you’ll see the database name on the lefthand side of the screen. Now you have to compare the DB_NAME with the value in the wp-config file.

If the names match, you’re in the clear. But if they don’t, you need to edit the wp-config file with the correct value.

While you’re checking the name of the database in phpMyAdmin, you could also check if you’re looking at the correct database. Do that by clicking on the database then clicking on the wp_options table. You’ll see the website URL and name at the very top of the table.

Do they match your website’s name and URL? Then you can be sure you’re in the right place.

If the database name was correct and you’re still getting the establishing a database connection error, you must check the database username and password next.

To do that, you need to create a new PHP file in the root folder of your WP website and paste the code below. You can name it whatever you want; just change the values of db_user and db_password with those you found in your wp-config file.

Database Credentials

Once you have uploaded the PHP file into the root directory of your WordPress site, it’s time to run the code. To do that, visit https://yourwebsite.com/nameofthePHPfile.php.

If you get a message saying “MySQL Error: Access denied,” it essentially means your username or password is incorrect – and you need to reset it.

And if you’re getting the “Database connection is working properly!” message, it means that the password and username are correct.

Resetting Your Database Username And Password

The next thing to do is reset your database username and password. To do that, login into your cPanel and open the MySQL Databases. Next, scroll down and find the New MySQL user option. We recommend you pick a unique username and password so that hackers have a harder time guessing it.

Once you’ve entered the desired username and password, click on the Create User button.

Now, it’s time to add the new MySQL user to your WordPress database. And to do that, scroll down to the Add User To Database section, select the newly created user, pick the correct database, and click on the Add button.

The page will ask you which privileges you want to assign to the user. Select the All Privileges option.

You’re not done yet. You still have to update your wp-config file with the new information.

To do that, open the wp-config file and change the values of DB_USER and DB_PASSWORD with the username and password you’ve created previously. That should get rid of the “error establishing database connection” message.

If you’re still getting the error, you could try a couple of other things.

#3 Repair The Corrupt WordPress Database to Fix Error Establishing a Database Connection

If changing your database username and password didn’t resolve the issue, you might have a corrupt database. That is pretty rare – but it can happen.

You can easily find out if you’re dealing with a corrupt database by logging in to your WordPress dashboard.

If you’re getting the “One or more database tables are unavailable. The database may need to be repaired” message when trying to open the dashboard page, you have a corrupt database.

Luckily, WordPress comes with a database repair feature. To activate it, add the following code to the bottom of the wp-config file:

define (‘WP_ALLOW_REPAIR’, true);

Now save the wp-config file and visit https://www.yourwebsite.com/maint/repair.php. That’ll run the database repair feature.

The page will give you options to either repair or optimize the database. Since you’re dealing with a corrupted database, choose the repair option.

Once WordPress finishes fixing the database, make sure to remove the code you added. If you don’t delete it, anyone will be able to access the feature.

You can also repair your WordPress database using phpMyAdmin.

Once you login into phpMyAdmin, go to your databases and select all the tables. Now click on the dropdown menu located below the selected tables and choose the Repair table option.

When you finish repairing the database, visit your website again. The database connection error should be gone.

If the error persists, you might be having trouble with corrupt files.

#4 Fix WordPress Corrupt Files

Files get corrupted in a number of ways. Maybe there was an error during FTP transfer, or a hacker managed to gain access to your website’s backend.

No matter the case, you have to restore corrupted files.

Before you get into this, we recommend that you make a backup of the website. That way, you’ll get to roll back any changes if you make a mistake.

To repair corrupted files, you’ll have to replace the core version of WordPress on your website. With that said, remember that this won’t affect any of your plugins, themes, or media files.

Download a copy of WordPress; you can do that by visiting wordpress.org. Once the download finishes, unzip the file.

Now, delete the wp-content directory as well as the wp-config file.

From there, fire up the FTP client and overwrite the files on your hosting server with the files you downloaded on your computer. That will replace any corrupted files with clean ones.

Once the transfer is finished, clear your browser cache and visit your website. The error should be gone.

If the error is somehow still there, you need to contact your web host.

#5 Contact Your WordPress Hosting Service

If none of the methods discussed above managed to fix the error, all that’s left to do is contact your hosting provider. They’ll guide you through the necessary troubleshooting steps or even fix the problem by themselves.

And if the problem is on their end, they’ll inform you.

If nothing else works, they might restore your website to the latest backup. In most cases, that is the fastest way to solve this issue.

That’s why it’s essential that you have daily backups of your WordPress websites.

Conclusion on WordPress Error Establishing a Database Connection

In this post, we showed you how to fix WordPress Error Establishing a Database Connection in a few different ways. If you’re still experiencing problems with your WordPress database, contact us, and we’d be glad to help. Our  WordPress support team will fix any type of technical or security issue on your website.