How to Fix Error Establishing a Database Connection in WordPress?

Updated on

How to Fix Error Establishing a Database Connection in WordPress


Nothing can tremble a WordPress website owner than seeing a message like 🛑”Error establishing a Database Connection” on the screen. We all panic after noticing any type of failure that makes our WordPress website go down [eg: “This Account Has Been Suspended” – WordPress Down[Fix]]

Don’t be upset. Although it will be seeming like a way serious issue, we assure you that it can be easily resolved. In this article, you will learn ❌ How to Fix Error Establishing a Database Connection in WordPress website and we will also discuss the major reasons for getting this error and how can we prevent it.  

Some other common wordpress errors you might want to read about are – Error 504 Gateway Timeout in WordPress, 405 Method Not Allowed Error, Error 404 Page Not Found In WordPress, White Screen of Death Error in WordPress, The link you followed has expired error in WordPress,Are you sure you want to do this Error WordPress,  503 Service Unavailable Error,

When do we get this database connection error message?

Before we explain this, first let us tell you how your website is connected to your database. As you know, WordPress stores all of your website’s content in a MySQL database. This it uses PHP commands to connect to your website’s MySQL database, get the information it needs, and then displays everything on the screen.

Now, when a visitor visits your website, PHP executes the code on the page and then queries the information in the database. After which, the content on the page is displayed on the visitor’s browser. The problem arises when due to some reason, the process doesn’t function properly or the code doesn’t execute, and, WordPress doesn’t understand what to do next and retrieves the message as shown below.

fix-error-establishing-a-database-connection-in-wordpress What Does This Error Means?

Every bit of information present on your WordPress website is stored in your MYSQL database.  This includes your posts, pages, layout design, widgets, plugins themes etc. In simple words, every bit of information about your website is stored in your database.

When you get an error like this, it means your website is no longer communicating or has access to your database, and thus your entire website goes down.  As a result, no content is visible to the user. A blank white screen with a message ” connection is not working properly” is displayed whenever any user tries to connect to your website.

This is a kind of HTTP Error Code 500 which means ” Server error codes indicating that the request was accepted, but that an error on the server prevented the fulfillment of the request.”This same status code appears when there is an “internal server error“.

It essentially means that something went wrong on the server and the requested resource was not delivered. When everything is working properly your site will generate a 200 HTTP status code, which means everything is fine.

“200 Status Code which means “Everything is OK.” This is the code that is delivered when a web page or resource acts exactly the way it’s expected to.”

Related Issues – WordPress Stuck in Maintenance Mode | Parse Error Syntax Error Unexpected in WordPress

 Most common reasons for this error in WordPress?

Here are a few common reasons explaining why this error exactly occurs on a WordPress website:

☑️ Incorrect Database login credentials in WP config

The most common reason for getting this kind of error is the incorrect database login credentials. There are no similar credentials for your WordPress website login and database login. A database login requires separate credentials which should be too strong to guess.  If there is any change in username or password, then WordPress will not be able to fetch any data from the database.

☑️ Corrupted Database

What if your database is corrupted? Yes, this could be the reason behind the error. We all have some plugins and themes of older versions and are no longer in use. But no one takes out time to deactivate or delete them. Now, this is how loopholes occur and let the hackers easily hack your website.

Another possibility could be an unsuccessful update of your WordPress database which includes missed plugins, themes or even failure in WordPress software update.

☑️ Compromised files in your WordPress Installation

This might be possible that your WordPress installation has certain corrupted files which are creating the error in connection with the database. Such files might be the loopholes or backdoors placed by the hackers in order to regain access to your website.

☑️ Database Server Issues

Your database server may have some issues which require quick resolving.  A number of things may go wrong with your database server like

  • the database being overloaded from a traffic
  • unresponsive database due to too many connections requests at one time. Though it is good for your website, it may create trouble.

This type of problem generally occurs due to shared hosting where a number of users are hosted on the same server.

☑️ WordPress database tables have been damaged

 How to Fix the Error Establishing a Database Connection?

Before initiating how to fix this database connection error, we recommend our readers to take a backup of your entire site along with the database. Make this a habit whenever you try to fix any error on your WordPress website.

There are a few plugins which automatically take a backup of your site every day so that you don’t need to have a manual setup every time you update your website or make any changes to it.

You can use WordPress backup plugins such as UpdraftPlus or Duplicator to backup both your files and your database.

📢 Now, let’s start with Troubleshooting .

☑️ Check Your Database Login Credentials

The major reason discovered behind the occurrence of this message is incorrect login credentials used to log in the database.  This happens for most of the people who have migrated to a new host. Make sure you are using right credentials.

The connection details for your WordPress site are stored in the wp-config.php file.


The file contains four important pieces of information that need to be correct for a successful database connection.

  1. The name of the database (“DB_NAME”)
  2. The login username (“DB_USER”)
  3. The login password (“DB_PASSWORD”)
  4. The database host (“DB_HOST”)

If any one of these values is incorrect, WordPress will not be able to connect to the database.

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');


You can easily locate the wp-config.php file in the cPanel.  Simply click on “File Manager,” and browse to the root of your site, and right click to edit the file. There,  you have to verify all the input values with the ones on the server to ensure that they are correct.

 file manager

DB_Name: The first thing to verify is the database name. You can check it through cPanel dashboard. Login into phpMyAdmin and check out the name of your database.

When you click on your database name, a screen listing the names of the tables in the database will appear. Click on the “Browse” button next to the table named wp-options, and see if you find your site name and URL along with other settings.

If yes, and the name matches your wp-config entry, you have the right database. If the URL is right, but your wp-config file is wrong, change the name there to match the right database.

Next, verify the username and password. You can test it via two ways:

Create a PHP test file to verify whether the credentials from wp-config connect you to the database. Paste the following code:

$test Connection = mysql_connect('localhost', 'root', 'password');
if (!$testConnection) {
die('Error: ' . mysql_error());
echo 'Database connection working!';

Add database credentials to the new text file and save it.

Now, type the URL of the file into your browser and try it; you’ll either get the “Connected successfully” message or the “Could not connect” message.

If you didn’t find the username and password in the table, create a new one in the wp-options. Be sure to make changes in the wp-config file as well.

If you don’t know how to add anew user, follow these steps:

  1. Go to the cPanel and click on “MySQL Databases”.
  2. Scroll to the MySQL Users. from there, add anew user.
  3. Create a new username and password and create “Create User”.
  4. Now find “Add User to Database”. Select the username and password you just created, and click “Add”.
  5.  Also, update the wp-config file with the newly created credentials.

Check if now the error is resolved.

☑️ Improving Compromised Database:

If the error still persists, the database might be corrupted. The worst thing that can appear in a WordPress database is missing tables which happens if hundreds of tables are added or removed by new plugins.

This may result in “error establishing a database connection” message displayed on front end whereas ” One or more database tables are unavailable” on the back end.

It is important to note that WordPress has repair mode which you can turn on  Simply add the following to the bottom of your wp-config.php file.

define(‘WP_ALLOW_REPAIR’, true);

Later, browse to the following location on your WordPress site:


You will then have the option to repair the database or repair and optimize the database. Since you are probably troubleshooting an interruption on your site, we recommend operating the repair database option as it is quicker.

Make sure to remove the line of code added to your wp-config.php file after executing the database repair.

You can also execute the repair of your database using cPanel.

☑️Repair DataBase Using phpMyAdmin:


In order to run the database repair from within phpMyAdmin, follow these steps:

  • Simply log in to phpMyAdmin,
  • Click on your database, and select all the tables.
  • Then from the drop-down click on “Repair table.” This is essentially just running the REPAIR TABLE command.

☑️Fixing Corrupt Files:

The second probable cause of getting this error is that your files might be corrupted. This may happen while sharing files through FTP which is not so secure and thus, a hacker can easily gain access to your files. This issue is necessary to fix as quick as possible.  Be sure to create a backup before cleaning up the corrupted files.

To do so, follow these steps:

  1. As we recommended, make a backup of your WordPress database manually.
  2. Now, download the latest version of WordPress from here and replace the older version with the latest one.
  3. Unzip the backup created and remove the corrupted files.  You’ll want to delete the wp-content folder, as well as the wp-config-sample.php file.
  4. Now, upload rest of the files to the updated version of WordPress through a secure sharing application i.e. SFTP.
  5. Clear out the browser cache. Now, again check if the error still occurs.

☑️ Fixing Issue with the Server:

We tried to fix every possibility leading to this error . But if nothing worked that means you need to check your hosting provider because it may be occurring due to an issue with the database server.

As we discussed earlier, if its the case where a number of concurrent connection are utilizing your database at the same time. This can slow down the speed or even lose connections with the database.

In order to minimize interaction with your WordPress database, you can use caching plugins like W3 Total Cache, WP Super Cache, Quick Cache etc.

And, if we talk about shared hosts utilizing same resources on servers, you can fix this problem with a high-performance best managed WordPress hosting, so that it is easy to handle large amounts of traffic to your WordPress sites.

 How to prevent this in future?

In order to avoid any database connection error on your WordPress website, here are few steps:

  1. Quality & Secure WordPress web hosting. Work only with hosts that have optimized their platforms to work with WordPress.
  2. Make sure to create regular backups every time you update your site You can also install a simple backup plugin like UpdraftPlus that works automatically.
  3. Keep the plugins and themes updated.
  4. Delete the WordPress plugins or themes that are no longer in use and scan your wordpress themes & plugins for malware periodically
  5. Always keep your WordPress up-to-date with the latest versions.

Need Help?

Certainly. this error is a matter of concern for a WordPress site owner but we have outlined a few quick ways to fix this error.  And, if this does not resolve the issue with your website or you need an expert help, we are always here to assist you.

To get started contact us here. We hope this article helped you learn how to fix error establishing database connection in WordPress easily.

Other Popular WordPress Tutorials: