5 Fixes for Error Establishing a Database Connection in WordPress

Nothing short of finding you’ve run out of coffee in the morning can be as frustrating as finding “Error establishing a database connection” on your WordPres website. This error code indicates that your website has lost access to your WordPress database, and so it has gone down.

Error establishing a database connection

The worst part of your website going down is that it messes with your website’s traffic. The longer your website is down, the more likely you are to drop lower in the SEO rankings, to miss out on potential customers, and lose site visitors.

So the main point here is that it’s crucial to get your website up and running as soon as possible. As disconcerting as the error code is, don’t worry! We’ll go over the possible causes of the problems and what you can do to fix it in less than 20 minutes. Even if you’re new to WordPress, our step-by-step guides will be easy for you to follow. You got this!

What is Error Establishing a Database Connection?

As the name suggests, this error indicates that WordPress is unable to establish a connection to the database and retrieve important information so your website can properly present itself for your website visitors.

Whenever you get a website visitor, PHP runs the code of the page and fetches the necessary data from the database, so the website can be properly displayed in the visitor’s browser. The MySQL database is where most of your WordPress website’s information is stored – plugin and theme settings, post data, page data, meta data, login information, and so on. Media content, like images, and theme/plugin/core files are the only ones that aren’t stored in the MySQL database.

However, if WordPress fails to establish a connection to the database, your visitors will get Error establishing a database connection instead of the website displayed in their browser. As PHP can’t access the data, the page will be blank safe for that dreaded sentence.

Most of the time, this causes an issue both on the frontend of your website (the domain that is accessed by visitors) and on your wp-admin site, i.e. your WordPress dashboard (we’ll go over how you can check this in a moment). Sometimes, the error won’t immediately be visible from the frontend, as the site can load from the cache for a while. (This gives you a bit of leeway in not freaking out while you’re getting the problem fixed.)

The error establishing a database connection problem can be caused by several reasons:

  • Your database login credentials are wrong. 

WordPress uses a different set of login information in order to access its MySQL database. The error can occur if these login credentials are incorrect or have been changed.

  • Your database server is unresponsive. 

Sometimes this issue can be caused by your web host. The server could be temporarily down or unresponsive due to too much traffic overloading the database. A lot of people use shared hosts which have limited, sometimes insufficient resources for a large number of users.

  • Your database is corrupted. 

A database can get corrupted from the constantly changing plugins, themes, and user settings on your WordPress website. In the process of frequently installing and deleting plugins, you may not notice a missing or deleted file, which can corrupt the database. The same can happen due to an individually corrupted table.

  • Corrupt files in your WordPress installation. 

This can be caused by a glitch during installation or by hackers.

Now that you know the most common causes of error establishing a database connection in WordPress, let’s look at how we can troubleshoot and fix the problem together.

How to Fix the Error Establishing a Database Connection in WordPress

Before we go into the various ways you can fix error establishing a database connection in WordPress, we strongly recommend that you backup your website, i.e. your files and database. As we tinker with various settings to get your website up and running again, there’s always a chance that something may accidentally get done wrong. You don’t want to lose any of your data!

You can use a WordPress backup plugin like VaultPress, or you can manually backup the MySQL database via phpMyAdmin.

Without further ado, let’s start troubleshooting and fixing!

1. Check If the Error Code is the same in /wp-admin/

The first and easiest thing you can do is check if the error appears the same on the frontend of the website (the domain that visitors visit) and the backend – your WordPress dashboard, i.e. wp-admin.

If the message is the same (error establishing a database connection) on both of them, you can skip this step.

If the message is slightly different on each, like it’s “Error establishing a database connection” on the frontend website but “One or more database tables are unavailable. The database may need to be repaired” in your WordPress wp-admin, then you will need to repair the database.

To do this:

  1. Open the wp-config.php file. Find the line that says:

“That’s all, stop editing! Happy blogging.”

  1. Insert the following code just before that line:
define(‘WP_ALLOW_REPAIR’, true);
  1. Check the settings by accessing http://www.domain.com/wp-admin/maint/repair.php, where domain is your website’s name.
  1. The ensuing window will provide you with two options: Repair Database and Repair and Optimize Database. You can click on either, though we recommend the latter option for optimized performance.
  1. Once WordPress is done repairing and optimizing the database, which will take a couple of minutes, go back to the wp-config.php file.
  1. Delete the line we added earlier (i.e. define(‘WP_ALLOW_REPAIR’, true);).
  1. Try accessing your website. If you can now access your WordPress dashboard and website, you’ve fixed the issue!

If this didn’t work, don’t worry! Let’s move onto the next possible solution.

2. Check if Your Database Login Credentials are Correct

Incorrect database login details are one of the most common causes of the error establishing a database connection. So the best thing to do at this point if check if they are, in fact, correct. This can especially happen if you recently migrated to a new web host. If this is the case, there’s a good chance that something went a bit awry in the transition.

Before we begin: if you don’t have access to your MySQL server directly, you should immediately contact your web host.

Oftentimes, the error establishing a database connection may be due to too much traffic on your website overwhelming the host – and this is especially the case if you’re using a shared host. Simply get on livechat with your web hosting provider so that they can check for you if your MySQL server is responsive and working properly. If you have multiple websites on the same hosting provider, and the same error appears on all of them, this means that most likely there’s an issue with the MySQL server and your host should get it sorted.

If you want to try sorting it out yourself – don’t worry, it’s easy! You just need to check if the values for the credentials are correct in each corresponding file.

To do this:

Via FTP Client

  1. Connect to your site via SFTP. (If you’re using cPanel, which we’ll cover in a bit, go to File Manager and browse the settings in the root file.)

(If you don’t know how to connect via FTP yet: open your browser or file explorer, and type in ftp://[FTP-server-IP-or-domain-name], where server IP or domain name are your server’s IP or domain name, then press Enter. Write in your username and password and select Log On to connect to your FTP server. Your username and password are provided by your web host. If you don’t know them, check your email to find the information when they – probably – sent it when you set up your website. Alternatively, contact your host via live chat and ask for this information.)

  1. Go to the wp-config.php file. Open the root file.
  1. Check the values under the following headlines:

Database Name

// ** MySQL settings ** //

/** The name of the database for WordPress */

define(‘DB_NAME’, ‘xxxxxx’);

MySQL database username

/** MySQL database username */

define(‘DB_USER’, ‘xxxxxx’);

MySQL database password

/** MySQL database password */

define(‘DB_PASSWORD’, ‘xxxxxxxxx’);

MySQL hostname (server)

/** MySQL hostname */

define(‘DB_HOST’, ‘localhost’);

  1. Check the values against the values on your server.
  1. If the values are correct, they should be the same for your site and for your server. This means that something else is causing the issue, and you can skip to the next step.
  1. If the values are different for your site and your server, you need to edit the values in your wp-config.php file to match those in your server.
  1. Once you’ve updated the values to be correct, reload your WordPress website. It should work now!

Via cPanel

Checking the database login credentials using cPanel is quite easy

To check your database login credentials using cPanel:

  1. Go to cPanel > Databases. Login to phpMyAdmin.
  1. On the bottom-left side of the screen, you’ll your database name. (Ignore information_schema database, it’s used by the webhost and irrelevant to us right now!)
  1. Check the database name in cPanel and see if it matches the DB_NAME value in your wp-config.php file.
  1. If the values are the same, something else is causing the issue, and you can skip to the next step.
  1. If the values are different in cPanel and your wp-config.php file, you need to edit the values in your wp-config.php file to match those of your server.

We can also check if the database is correct by seeing whether it contains the URL of your wordpress website. To do this:

  1. Go to phpMyAdmin> Database > wp_options.
  1. At the top part of the wp_options table, you’ll see the information concerning the name and URL of your website. See if they are correct and match your website data.
  1. If the database name was correct, but you’re still seeing the error establishing a database connection, you need to check the username and password. So:
  1. Go to the root directory of your WordPress website.
  1. Create a new PHP file inside the root directory. Name it anything you like, but make sure to add the .php extension at the end (example: dbcheck.php).
  1. Add the following code, only change the values of db_user and db_password so that they match the ones in your wp-config.php file.

<?php
$test = mysqli_connect(‘localhost’, ‘db_user’, ‘db_password’);
if (!$test) {
die(‘MySQL Error: ‘ . mysqli_error());
}
echo ‘Database connection is working properly!’;
mysqli_close($testConnection);

  1. Go to the corresponding file on your WordPress file by visiting:

https://domainname.com/dbcheck.php, where domainname is the name of your domain, and dbcheck is whatever you decided to name the new php folder.

  1. If the database issue was fixed by this point, you’ll get the following message:

Database connection is working properly.

  1. If there’s a problem with your username and password credentials (or there’s still an issue with establishing a database connection), you’ll get the following error message:

MySQL Error: Access denied.

To fix this, you’ll need to reset your credentials. So let’s keep moving!

  1. First, delete the test file, i.e. what we dubbed dbcheck.php.
  1. Go to cPanel > Databases > MySQL Databases.
  1. Create a new MySQL user at the bottom of the page. You may choose your own unique username and password, or allow the password generator tool do the job for you.

Alternatively, just change the password for the existing database user.

  1. At the bottom of the list, add the new user to your database. The ensuing screen will allow you to choose which privileges you want to assign to the new user. Click on All Privileges.
  1. Update your wp-config.php file with the new credentials under the DB_USER and DB_PASSWORD values.
  1. Repeat the PHP file test. If the issue was resolved, you’ll now be getting the beautiful Database connection is working properly message.

If you’re still experiencing issues, the problem may be a wrong hostname (under DB_HOST). Most commonly, this value will say localhost. If you think this may be causing the issue, contact your host about it as soon as possible!

If checking and correcting your credentials didn’t work in fixing the error establishing a database connection, let’s move onto the next solution you can try to fix the issue.

3. Fix Corrupt Files

We will go over fixing two types of possibly corrupt files: a corrupt WordPress core version on your site or corrupt plugins/themes files.

In the first case, your WordPress core version files may become corrupt from a number of reasons, including:

  • Hackers getting a hold of your site information;
  • Glitches during migration from one host to another;
  • Host-end issues;
  • Incorrectly transferred files via FTP client.

In the second case, a certain plugin or theme file may have become corrupt in cases such as:

  • A recent update;
  • A faulty connection with an external service;
  • An unintentional faulty edit in one of the theme or plugin folders on your end.

Let’s look at how you can fix the corrupt files in each case.

How to Fix Corrupt Files in Your WordPress Core Version

Before you continue with this step, make sure you backup your WordPress website.

What this solution basically entails is reinstalling the core version of WordPress for your website. Your other information, such as media, themes, and plugins, will remain untouched and can be retrieved when you reset your website.

To do this:

  1. Go to the WordPress website and download a new copy of WordPress.

Download a new copy of the latest version of WordPress

  1. Unzip the WordPress file which downloaded on your PC.
  1. Find and delete the wp-content folder and the wp-config-sample.php file.
  1. Connect to your site via SFTP, and then upload the remainder of the files from the latest WordPress download. This step basically overwrites all the WordPress core version files, and if any of them were corrupt, they will no longer be a problem since they’ll be replaced.
  1. Flush your browser cache. To do this, go to Customize and control Google Chrome > More Tools > Clear browsing data. Check all boxes to flush your browser cache.

How to clear your cache in Chrome

  1. Reload your website. If the issue was corrupt core WordPress files, the error establishing a database connection problem should now be resolved!

How to Fix Corrupt Files From Your Themes and Plugins

Plugins and themes are created by web developers from across the globe, so it’s not uncommon that they may cause glitches due to faulty codes or misconfigured updates.

We went over several different scenarios that may have resulted in a corrupt plugin or theme file. If your themes and plugins haven’t recently gone through an update, you haven’t connected your website to an external service, or weren’t fumbling with the files, the chances that this is causing the issue are slim. If nothing else worked so far, however, there’s no harm in trying!

To fix corrupt files from your plugins:

  1. Connect to your host via FTP client.
  1. Go to the WordPress root folder, which may be named something like public_html or www. You may be directly inside the root folder upon connecting via FTP.
  1. Navigate to the wp-content folder and find the plugins folder. Rename it to something else, like plugins_old.
  1. Reload your website, and see if the problem persists. If the error establishing a database connection message is gone, it means that one of your plugins was causing the issue, and we’ll go on to find out which one. If the error message persists, change the folder’s name back to plugins, because clearly plugins are not the culprit. Instead, you’ll need to repeat the same process with the themes folder.
  1. If the website loaded properly, we now need to find which exact plugin was causing the issue. To do this, we’ll test each plugin one by one. Go back to FTP and rename your plugins folder back to plugins. This will reactivate all plugins.
  1. Rename each individual plugin folder one by one, so as to deactivate them individually. The easiest way to change each name is to just add an underscore (_) after each plugin’s name. Reload your website after each deactivation.
  1. When your website loads, it means that the plugin that was deactivated at the time was the little bugger causing this whole mess. Delete it. Don’t look back. Maybe contact the plugin’s web developer to let them know there’s an issue that should get fixed – if the problem was indeed a recent update.

To fix corrupt files from your themes:

To fix potentially corrupt theme files, you’ll need to deactivate your website’s custom theme, and revert back to a default WordPress theme. These are the Twenty Somethings, like the Twenty Nineteen or the latest WordPress theme, Twenty Twenty.

Deactivating a custom theme may fix the Error establishing a database connection problem

Deactivating themes is quite similar to deactivating plugins.

To do this:

  1. Connect to your host via FTP client.
  1. Find the wp_content/themes folder.
  1. Rename it to anything, like themes_old. Renaming the themes folder will make WordPress revert back to the default theme. (Note: if you’ve deleted the default themes from your WordPress account, just download any one of them from the WordPress repository and add it to your themes folder.)
  1. Try loading your website again. If your website loads without a problem, it means that your custom theme was causing the error establishing a database connection. As we suggested earlier in the case of finding a faulty plugin, you can again contact the theme developer letting them know about the problem so they can fix it. You can also post a support ticket in the WordPress repository.

If the error persists after renaming both the plugins and themes folders in your root directory, something else is causing the issue. The next step – which we’ve been circling around for a while, is…

4. Contact Your Web Hosting Provider

If nothing worked so far, your best bet is contacting your web host. The error establishing a database connection may be stemming from the database server. As we mentioned already, your host’s server may be overloaded, especially if you’re using a shared host. Too many website visitors may also be causing the issue, especially if you’re on a budget plan that has a limit on the number of connections your website can generate at a given time.

Pro tip: To avoid this issue in the future, getting a caching plugin like W3 Total Cache or WP Super Cache can help reduce the database interaction on your website.

Shared hosts use a limited amount of resources for a number of clients. If there are too many clients, and either you or someone else is getting a lot of website traffic, it may cause the entire server to go down and cause this issue.

You can contact your web host via live chat and get quick support, or open a ticket. If you’re a DomainNameSanity client, get in touch with us and our tech support will check and resolve the problem in no time.

In the end, if your website is getting increasing numbers of visitors, it may be time for you to migrate to a slightly pricier plan. A better plan will provide you with more storage and support for heavier traffic and page weight.

5. Restore Your Website to the Latest Backup

This step can be the last thing you try or the first, especially if you recently made a backup of your website.

If you recently backed up your website, and you don’t think much important data will get lost in the process, try restoring your website to the latest backup version. You will probably need to restore your files and your database to complete the process.

If you’re not sure how to do this, just contact your host. You host should have a backup restore procedure ready for you!

Final Words

While the Error establishing a database connection problem is frustrating, there are a number of ways to fix it. When you run your own website, it’s inevitable to come across these sorts of error messages and other annoying glitches. As most of them aren’t too difficult to fix, the best thing to do is try to get it done as soon as possible, so as not to lose any website traffic or drop in SEO rankings.

In the end, all you need to do is keep calm and read over our guide on how to fix the issue! Hopefully, one of our suggested solutions worked for you and your website is up and running again.

View Related Articles

Why Are My Emails Going to Spam?

There is no experience more frustrating than having your emails end up in the spam folder of your recipients rather than their inboxes. If you’ve found that your emails are going to spam instead of your subscribers’ inboxes, keep reading to find out why.

How to Become Tech Savvy

In a world where technology shapes our society and everyday life, knowing how to operate a computer and other common tech gadgets becomes a survival skill.

What two protocols are used to deliver mail messages?

Emailing today is such an easy task. Compose, hit send, and your message travels across oceans and borders to reach its recipient. It is such an easy task that people’s attention starts to drown in their full inboxes, which are also littered by spammers, phishers and other members of the not-so-nice population of netizens.

Leave a Comment

Your email address will not be published. Required fields are marked *