Quick Fix: Error establishing A Database Connection WordPress

Updated on By

Error establishing a database connection is one of those annoying and most common errors you will encounter while working on your website.

That clean white page with the message “Error establishing a database connection” with no further explanation and no link may look like a dead end.

But it is not so!

This error can be solved easily and in this article, we have got you covered with everything you need to know about the error message.

So without wasting any more time, let us get started with how to get rid of the Error establishing a database connection so that you can get your website running live as soon as possible.

Understanding the error

You are sure that you did not make any change into your website, but somehow, now your website has ended up with the dead white page with the “error establishing a database connection” message.

error establishing a database connection

Since your website is not responding normally, it is an indication that something is wrong with your website.

Well, nothing can be too wrong to be fixed, when it comes to your WordPress website.

The error message is quite self-explanatory in itself “Error establishing a database connection” which means…

…for some reasons, your PHP commands cannot retrieve the query results because the connection to the database is not made.

Therefore, even the design is unreachable and the error message appears on the blank page.

So now, as we know that this error is due to a disconnected database and now we can proceed to know how to fix this error.

Reasons for this error to occur

To solve this error, the first thing you need to know is its cause.

There can be different reasons for this error to occur. Whereas it is difficult to determine what exactly causes the error establishing the database connection problem, the probable reason could be one of the below:

1) High Traffic

Very high traffic can sometimes be the cause of the “Error Establishing A Database Connection”.

Your database server might go unresponsive due to a traffic spike.

Generally, the hosting services promise you a service that traffic upsurge won’t affect the performance of your website.

But sometimes when bombarded with multiple requests at once, the server may stop responding to the request and you may get the error establishing the database connection error.

2) Incorrect Username and password
Error establishing database connection due to wrong credential

If you are using incorrect credentials to log in to make the connection. There can be a spelling mistake, unnecessary spacing problem, or wrong password.

With the incorrect password or username, the connection to the database is denied and so you may see the “Error establishing a database connection” message.

If this is the reason for the error, then you can easily fix it by checking your database connection credentials, and correcting or resetting them.

3) Server down

The database server might be down due to many reasons. The reason might be an upsurge in requests to be processed, power failure or any other reason.

Sometimes, when the server is down, so the connection request is denied and you get the Error establishing a database connection error.

4) Other database problems

Your website’s database is constantly been updated with your own and the user actions. It is constantly changed – data is updated, added, removed, and that may corrupt the database.

These actions can sometimes impact negatively and harm your database. This could be one of the reasons for the error establishing a database connection message.

5) Corrupt WordPress files

If your database is okay, and the password and username a legit too, then corrupt WordPress files can be the problem.

With the new updates, or by installing a bad plugin, or unsolicited code insertion through a hack, etc can corrupt your WordPress files. This too can cause this error.

If you are experiencing the error establishing the database connection message, then one of the above-listed reasons can be the cause. Now we will see the steps or procedures that can help you solve this error.

How to fix the Error Establishing the Database Connection

You must have a backup of your site before you proceed. For regular backups, you can use plugins like Backup Buddy or the Duplicator plugin.

Alternately, you can manually backup your WordPress files as well as a database before you proceed. Sometimes, even your host provides the backup features. So all you need to do is to

  1. Check if the error persists at /wp-admin & Repair database
  2. Incorrect Database Connection Credentials
  3. How to reset the database username and password?
  4. When the Database Server is down due to high traffic
  5.  Fixing the corrupted WordPress files
  6. Restore the latest backup
  7. Check with your hosting company

1. Check if the error persists at /wp-admin & Repair database

Try logging in to your /wp-admin dashboard, check if the problem is the same.

First of all, check if you are allowed to log in to your backend without any problem. Try logging into your website’s backend and if you get the same error, you can proceed with the next step.

But sometimes the error message can be a bit different. If you see a message that goes:

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

In this case, you can try repairing your database with the inbuilt mechanism by adding a line of code to your wp-config file.

error establishing a database connection

Open your wp-config.php file and add this line at the end of it.

define('WP_ALLOW_REPAIR', true);

Now you can navigate to this page

http://www.yourwebsitename.com/wp-admin/maint/repair.php

As shown in the image, this page allows you to either repair the database or to repair and optimize the database. You can click the button to perform the operation of your choice.

Note: Don’t forget to remove the command at the end of the processes. The link can be accessed without being logged in and can cause problems if not removed.

2. Check Your Database Connection Credentials

Sometimes the problem can be your database connection credentials used to make a connection. This is one of the most common reasons for this error to occur.

Naturally, the connection will only be granted if the username and password to make a connection to the database are correct. If they are not, you will see the Error establishing a database connection error.

What you can do is – Check the wp-config.php file that you can find in your WordPress root folder. The image below shows the part of your wp-config.php that needs to be checked.

As shown in the image, this file stores the database name, database username, password, and the hostname. This information is vital for making a connection.

You can access this file using FTP or the file manager option by logging into your host. Now we need to tally the information in this file with that on the server.

1.Checking the database name

Login to your host, and then choose the databases option

Now select phpMyAdmin 

The next screen will show you the databases you have stored on the server. Find the site’s database name from the list.

Compare this database name with that on the wp-config.php, if the name doesn’t match you should update your wp-config.php

Extra Tip: How to determine if the database is your site’s database?

Click on the database and it will show all the tables under the database. Now find the table – wp-options. It can be slightly different sometimes. For example: wpdb-options, ab-options, etc.

This table stores the information for your site URL and name.

Check the values, if the name and the URL matches, this is the database you’ve been looking for!

2. Checking the username and password for your database

If the name of your database is correct, then the problem can be regarding the username and password. Now, first of all, we need to check if the problem is the username and password.

There is a simple way to do this. You can create a PHP file as described below and run it to see if the connection is made or not.

Create a PHP file with the code given below: For this example, we are naming it connectiontest.php

<?php

$test Connection = mysql_connect('localhost', 'username', 'password');

if (!$testConnection)

{ die('Error: ' . mysql_error()); }

echo 'Database successfully connected!';

mysql_close($testConnection);

?>

Make sure that you replace the correct values for localhost, username, and password. Upload this file to your WordPress root directory and run it by using your site’s URL:

www.yoursitename.com/connectiontest.php

Once you run this file, it will either display a connection error message or indication that the connection is successful and the username and password are okay.

If the username and password are perfect, you can skip this step and proceed with the next one, but if there is a problem with the username and password, you can easily reset them:

3. How to reset the database username and password?

If the username and password for your database don’t match, you can reset it to get your website running normally. Here’s how you can reset your username and password.

You can do this using your cPanel option generally labeled as “MySQL Databases”.

Find the option “MySQL Users: Add New User”

This will allow you to create a new username and password. Click “Create User” and then find “Add User to Database”.

Select the username and password you just created, and click “Add”.

Once you get your new user created, don’t forget to update the wp-config file with the new username and password.

With the username and password assigned to your database and your wp-config file updated, your database connection error should go away.

However, if the problem persists, it can be the DB_HOST entry in the wp-config. Generally, it is “localhost”, but it could differ depending on your host.

You can contact your hosting company and ask them about it and make the correction recommended by them.

4. The Database Server is down due to high traffic

At times, the database server becomes irresponsive due to accessive requests being processed. This happens when the number of requests to be processed by the server is more than the number of requests it can handle.

error establishing the database connectionThis can lead to a slow down or completely crash of your database server. Here, there’s nothing you can do on your end, but to contact the hosting company to fix it for you.

The problem generally happens when you are using shared resources on the host. Even the hosting companies worry about promising the best uptime to their clients and therefore they shall fix it as soon as possible.

If you find your WordPress website having the traffic problems frequently, you should probably start using a caching plugin. The caching plugin saves a static HTML copy of your website’s posts and pages and servers it to users when they try to access them.

This reduces the use of resources on the server and with a reduced load, your website gets faster and performance optimized too.

5. Fixing the corrupted WordPress files

Problems in your WordPress files can also cause the Error establishing the database connection.

When your vital WordPress files are altered or corrupted, your website may start displaying the error establishing the database connection error.

The simple solution to this problem is to restore your default WordPress files.

You need to be extra careful when dealing with the WordPress system files because a mistake here can lead to bigger problems for your site. Therefore, it is recommended to backup before you start.

Follow the steps mentioned below to replace your WordPress files to counter Error Establishing the database connection:

  1. New & latest WordPress files can be easily downloaded from WordPress.org. You can now use a program like 7zip to unzip the downloaded zip package of WordPress files.
  2. From the extracted folder, delete the wp-config.php, which contains the login information, so that your login credentials remain the same.
  3. Next, you should also delete the wp-content folder from your unzipped folder, in order to ensure that you don’t lose the themes, plugin, and the media files stored by your website.
  4. When you have carefully followed the last 3 steps, you can use FTP to upload the remaining files to your WordPress root folder.

Note: Don’t forget to clear your browser’s cache before you try reloading your website after replacing the WordPress file.

If your database is not faulty, replacing the WordPress files should solve your problem. Uploading the fresh files should replace the damaged WordPress file and your website should start running normally.

6. Restore the latest backup

I can never emphasize enough the importance of backups. They are your best friends in time of crisis.

If your website has a recent backup, you can completely ignore all the steps mentioned in this article and restore your latest backup.

One of the easiest ways to get your site running right away is to remove the current files and restore the backup:

We recommend installing the backup in your localhost to check

  1. if the backup is restored securely without errors and breakage
  2. the restored site is not too old to be functional
  3. major changes in your site are not overwritten due to an old backup
Tip: It is highly recommended to install the backup in your local environment, or a test site before you actually replace your current website. So if any anomaly is found in the backup, you can turn back to your current site and try fixing the error instead.

If you find the backup fully functional and fit enough, you can replace your website with the backup and have your site running normally instantly.

You can use a plugin like the Duplicator plugin, for both, automatic backups and to restore your backup and get it live in minutes.

Here’s an article that can help you move your backup/local site to your live server.

This will result in the minimal downtime of your website due to the “Error establishing the database connection” error, and you won’t have to disappoint your visitors for long.

7. Check with your hosting company

Tried everything but the problem still won’t go away?

Then you should probably check with the hosting company to see if the problem is at their end. Contact your hosting support and provide them with all the details about your problem.

Don’t forget to mention the troubleshooting steps you have already taken, and any other detail about the recent changes into your site, or some suspicious activity if any noticed.

The hosting support staff will check on their end and suggest you a solution.

Choosing a reliable host

Another thing you should note here is the importance of choosing a reliable host. We highly recommend using Bluehost for your WordPress website because our users have had the best experience with Bluehost.

They have some of the best and affordable plans for hosting your website and promise the best uptime. Their WordPress optimized hosting makes hosting WordPress website easier and manageable.

Anyways, problems generally are caused when you are using the shared hosting space. The shared hosts, as the name suggest is shared among different clients and when requests are made, the same resources are used.

If you are serious about your website’s performance and uptime, you should choose dedicated or WP optimized hosting.

How To Avoid This Error in the future?

It’s really disappointing to see the white screen of error establishing the database connection and once it is fixed, you would want to make sure that it never appears again.

While there are no definite steps to completely avoid this error from happening, there are some effective measures that you can take to protect your website.

Choose a fine host

A secure and efficient host is what you should always choose. Not just to avoid this error, but because the type of host you choose has a direct impact on the overall performance and user experience of your website.

Also, changing web hosting every now and then isn’t very convenient. So make your decision wisely when choosing a web host. Make sure it is secure and protects your website files from external attacks.

It should also be capable of handling the traffic and excessive requests without making your website slow or unavailable.

Regularly backup your site

Backups are lifesavers for your website in times of crisis. There are a thousand ways to find and fix a problem with your website when it is down, but meanwhile, you can’t afford to disappoint your users & make them wait till you find the cure.

Here, it takes only minutes to restore the latest backup and have your website running again. You can even use backup as a temporary solution till you find and fix the problem with your website.

Restoring backup- Error establishing a database connection

Therefore, use a backup plugin, or choose a host that assures you regular backups of your website at the determined intervals.

So whenever the problem arises, you can quickly restore the latest backup and minimize the prolonged downtime required to fix the error.

You can also integrate the best of backup plugins for your website to make sure that your website is backed up regularly.
WordPress backup plugin

Avoid using plugins that may crash your WordPress websites

Plugins in WordPress are a great way to add features and extend the usability of your website. And thankfully, you can choose from tens of thousands of free and premium WordPress plugins to enhance the usability of your website.

However, you have to be careful while choosing the plugins for your website. Recklessly integrating different plugins into your website can harm your website in different ways.

Sometimes, wrong plugins (or overloading your site with different plugins) can cause problems in your site.

Be careful when you integrate a new plugin into your website, because a bad plugin or a plugin that conflicts with your website, may cause your site to break down.

When you integrate multiple plugins into your site, they may conflict with each other or your theme files and that too can result in corrupt files or database tables, which ultimately leads to Error establishing the database connection.

Malware scan and security measures

Sometimes, when your website is not secure enough, the hackers may find their way into your site’s internals and change the password or corrupt a file.

Security issues may cost you a lot including theft of data, corruption of files, or a website breakdown, etc. and result in a lot of damage.

Keep your system secure against the damage caused by hackers and malware by securing your website. You can do so by integrating security plugins, subscribing to some website security services, etc.

Final Words

The error establishing a database connection may look a lot disappointing, but with proper steps performed you can restore the normal functioning of your site.

If nothing works, you can contact your host and ask for assistance with the problem.

Also, don’t forget to share your stories on how you solved this error and what was the problem with your site.

Further reading: For smooth and flawless functioning of any website, proper maintenance is vital. So what are these maintenance routines and how you can do it on your own, without professional assistance?
A complete WordPress website Maintenance guide.

Disclosure: This page contains external affiliate links that may result in us receiving a commission if you choose to purchase mentioned product. The opinions on this page are our own and we don't receive additional bonus for positive reviews.