How do I fix an Error Establishing Database Connection?

Does your website is showing “Error Establishing Database Connection”? If you are facing this problem and are searching for how to solve it, then trust me you have landed on the right page because today in this article I am going to talk about the reasons that can cause the error of establishing the database connection and how you can solve them. So, without wasting time, let’s move on to the article.

So, if you are a regular WordPress user, it is possible that you have faced this error once in your lifetime. This error occurs because your WordPress blog stops making a connection with your MySQL database. Now there are so many reasons why your website is down and saying this “Error Establishing Database Connection” So before troubleshooting this error, we need to understand how WordPress works.

What does “Error Establishing Database Connection” actually means?

WordPress is software-based on two different languages PHP and MySQL. MySQL uses to store all data (also we can call that it works on the back-end) while PHP works on the front end making a connection with it and managing functions of your WordPress blog. In simple words, we can say that PHP saves all your records and every single detail of your website such as themes, plugins, author details, and then make a connection to MySQL to retrieve the data as per need. So, if PHP is not able to make a connection with your MySQL database, your website ends up showing “Error Establishing Database Connection.” Below I have added some of the possible reasons which can cause this Error.

What can cause this error?

So,  we know that why this error occurs but identify the actual reason is tough though. There may be so many reasons why your website is not making a connection with the database server, but all of them fall under the three categories that I have mentioned below-

Login Credentials of Database is wrong or maybe changed- Your database server is separated with a username and password. So, if you have recently changed them, it can cause this error.

Your Database is corrupted- This means that your WordPress database goes corrupted. Reason can be anything like maybe you have installed a nulled plugin or something else that has corrupted the data stored in the database. Though it is not a common reason that can cause this error, you have to be careful before uploading any plugin to WordPress. Make sure that the WordPress plugin or a theme is downloaded from a trusted source.

The server that hosts your website is down- There are some possibilities that the server, where you have hosted your WordPress blog is not able to able the traffic comes to your site.

Solutions to Fix “Error Establishing a Database Connection”

Below I have added three different methods for solving this error. So, let’s begin with the first one-

Fixing error with Database Repairing option-

Before proceeding to this step, you have to confirm that your database is corrupted or not. For this, you can follow these simple steps-

  • First, go to your website and note down the error showing. (Of course, it would be “Error Establishing a Database Connection”)
  • Now visit the back-end of your WordPress blog. It will be the URL of your dashboard. Merely go to yourdomain.com/wp-admin and check that the error is the same or not.

If your WordPress is showing the same error on both sides, then you don’t need to follow this method. But, if your WordPress is showing different errors such as “One or more tables are unavailable. The database may need to be repaired.” Or any other error, you have to repair your database, and you can use this guide for that. 😉

So, for repairing the database, you need to follow the steps given below-

  • First of all, you need to open the wp-config.php For that, you have login into your cPanel and then open the file manager and go to this directory- file manager >> public_html >> domain root >> wp-config.php >> edit
  • Now you have to copy the code given below and paste it to just before the line “That’s all, stop editing! Happy Blogging”

define(‘WP_ALLOW_REPAIR’, true);

  • After adding this code, simply open this URL http://www.yourdomain.com/wp-admin/maint/repair.php in your browser and you will see an image like this.
  • Now you will get two options to repair your database. You can choose any of them, but I will suggest you choose ‘Repair and Optimize Database.’

That’s it; Now it will repair your database. All you just have to keep in mind that if you will choose to repair and optimize the database, it will take a bit of extra time.

Also, you have to remove that code we have added to the wp-config.php file. Because if you do not remove that code, anyone can access your database repair page without any password.

Now check that your issue is resolved or are you still facing the same error. If you are still facing the same error, just move to the next method provided below.

Checking the wp-config.php file

Wp-config.php is one of the most important files on your WordPress blog because it contains all of the important data of your WordPress blog. If you have recently made any changes to your database server, you have to update that in the wp-config.php file too.

So, first of all, open the wp-config.php file and look for the following lines of the code.

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

/** MySQL database username */
define( ‘DB_USER’, ‘username_here’ );

/** MySQL database password */
define( ‘DB_PASSWORD’, ‘password_here’ );

/** MySQL hostname */
define( ‘DB_HOST’, ‘localhost’ );

Check the value in this code. If any of the value is incorrect, update them with the correct one and save it. Now check that your site is working fine or not.

Note- Remember that DB_HOST is not always the ‘localhost.’ It varies on your web host. While many of the major web hosts like Hostgator, Bluehost, 000webhost, and siteground use localhost as their default one.

Contact your web host

If you are still facing this issue, then there may be a reason that your site has received an amount of traffic that is difficult to handle by your web host. Contact your web host support and tell them the details about this error along with the troubleshooting methods you have tried. They will help you with this.

Getting a site down is one of the biggest fear for every blogger or someone who runs a business online.  The more time it takes to resolve, the more loss you have to face.

It is not a very tough task to solve this error; you can fix it with just a few simple clicks. All you just have to keep calm and understand the reason instead of being panic.

Conclusion;-

Hope this article has helped you in solving this error. If you have any question related to any method or have any suggestions for improving this article, please share with us in the comment section below.

Share via
Copy link