“Error Establishing A Database Connection”. It’s one of the worst nightmares for any WordPress user. This error shows up when your WordPress site is unable to connect to the database to fetch the contents of your website. The database is where all the contents and information of your website is stored.

Needless to say, when this error shows up on your website, you start losing tons of subscribers, visitors, and revenue. Because all your visitors and customers will see is an error message. If you don’t fix this error soon enough, Google and other Search Engines might even stop displaying your website. But don’t worry even if Google stops displaying your website. They will start displaying your website in search results again in a few days once you fix the error.


WPEngine Promo Discount Code Exclusive WPEngine Coupon Code for AlienWP Visitors!
Get 5 Months Free & 30% Off All Plans.
Click the coupon code to copy and open the link »
alienwp30
Click To Open/Copy

WordPress Error

If you are seeing this error on your website, later in this article, I will guide you through the exact process we ourselves use to fix this error on our own websites.

What Does “Error Establishing A Database Connection” Mean?

To understand why this error shows up, you will have to first understand how WordPress works. When someone visits a page on your website, WordPress connects to your website’s database, fetches the contents of the page and then displays the page.

The database of your website is where all the information and content of your website is stored. This information includes login credentials for all the users (including your admin account) of the website. And when you see this error, it simply means that for some reason WordPress is unable to connect to the database.

When WordPress is unable to connect to the database, it won’t allow you access to the dashboard as there’s no way for it to verify your login credentials.

To connect to the database, WordPress needs to know the login credentials for the database. And if these login credentials are wrong or missing, it won’t be able to connect to the database.

There is more than one reason why you would see this error. But most of the time, this error happens due to incorrect database login credentials.

What Causes This Error?

This error simply means that WordPress can’t connect to your database to fetch and display the contents of your website. When you see this error on your website, one of the followings can be the case:

  • The login credentials of your database are wrong: This is what happens when you change the login credentials of your database and forget to update them in the wp-config file.
  • Your database is corrupted: There are a lot of reasons why this could happen to your database. One of which is uploading a badly coded plugin. It can also happen due to an error in the database hosting server.
  • Your database server host is down: If you are hosting the database on your own servers, then you will have to contact your developer. But if your web hosting company is responsible for hosting the database, there’s nothing you can do other than calling them and letting them know that it’s down. Most web hosting services offer great support and will fix this problem within minutes.

Whichever the above is the case for you, we will guide you through every possible scenario in this article. In most cases, this error shows up either when the database is curropted or the database login credentials aren’t valid. Both of which are easy to fix and won’t take more than a few minutes to fix.

Before you start making any changes to your website, we would highly recommend you to make a backup or make sure you have a backup of your website. In the unlikely case that you break something, a backup will allow you to go back to the previous state.

Do You See The Same Message On The Back-End?

When you try logging into your website dashboard, are you greeted with a message along the lines of “One or more database tables are unavailable. The database may need to be repaired.”?

If this is the message you see on the back-end, then it simply means that your database is corrupted. On the other hand, if you are seeing the same “Error Establishing A Database Connection.” message, then you can move on to the next section.

Fixing a corrupted database is easy and doesn’t take more than a few minutes. Here’s how you can do it:

First, Login to cPanel, open the File Manager app and then navigate to the WordPress installation folder. Now, find the wp-config.php file, select the edit option and add the following line of code at the end of it:

define(‘WP_ALLOW_REPAIR’, true);

What this line of code will do is it will allow you to enter the database repair mode for your WordPress website.

To start the repair process, visit the following page in your browser:

http://yoursite.com/wp-admin/maint/repair.php

On this repair page, you can either choose repair or repair and optimize. We recommend you to go with repair and optimize, but you should note that it takes more time than just the repair function.

One thing you need to note is that this page is accessible by everyone. You don’t need to login to visit this page and use the repair functionality. So, to make sure the users of your website can’t use this page, you will have to remove the line of code you added to your wp-config.php file once the process is over.

Check If Incorrect Database Login Information Is Causing This Error

To do this, you will have to log in to your cPanel account and open the File Manager app. Then navigate to the installation directory of your WordPress site. Now, view or edit the wp-config.php file to inspect the code.

You should see the login credentials used by WordPress to connect to your database. It should look something like this:

define('DB_NAME', 'database-name-here');
define('DB_USER', 'database-username-here');
define('DB_PASSWORD', 'database-password-here');
define('DB_HOST', 'localhost');

The above four lines contain your database name, username, password and the hostname of the database.

If you find any of these values in incorrect, you should change them with the correct values.

If you don’t know the name of your database, you can find it with PHPMyAdmin. It’s a free application that comes with all hosting solutions.

To use it, you will have to log in to PHPMyAdmin from your cPanel. Once you have logged into the PHPMyAdmin dashboard, you will see a list of databases. One of these databases should have the same name as the value right next to “DB_NAME” in your wp-config.php file. Click the Browse link for the database that has the same name.

Now, you will need to verify if this is the correct database that contains your website’s information. To do this, click the Browse link right next to the “wp_options” table.

If in this table, you can see the name of your website and URL, then it means this is the correct database. If this is not the case, you will have to do the same thing for all the other databases to find the correct database. Once you find the correct database, change the name of your database in the wp-config file with the name of this correct database.

If the name of the database is already correct, then the username and password can be the problem. To check if you are using the correct username and password, create a new file in the installation directory of WordPress and name it “db-check.php” and copy the following code to it:

<?php
$test = mysql_connect('localhost', 'root', 'password');
if (!$test) {
die('MySQL Error: ' . mysql_error());
}
echo 'Database connection is working properly!';
mysql_close($testConnection);

Now, visit the following URL:

http://yoursite.com/db-check.php

If you see an error message, then it means the username or password is incorrect. To fix this, you can create a new username and password with the MySQL Databases app in cPanel.

To do this, open up the MySQL Databases app in cPanel and click the Add New User link. All you have to do now is choose a good username and a strong password, and click the Create User button. These new database credentials are what you will now use.

Now, scroll down to the Add User To Database section, select the username of the user you just created and the WordPress database, and click the Add button. Doing this will add this new user that you just created to your WordPress site’s database.

Now that you have a new user, you will have to update the wp-config.php file with the new database login information.

To do this, open the File Manager app in cPanel, navigate to the WordPress installation directory and edit the wp-config.php file. In the wp-config.php file, change the value next to ‘DB_USER’ with the new username and the value next to ‘DB_PASSWORD’ with the new password.

This should fix the problem if it was due to incorrect database login credentials.

If your website is still showing the same error message, move on to the next section:

Still Seeing The Same Error Message?

If this is the case, then most probably it’s a problem related to your Web Host’s database servers. The only thing you can do in this case is to contact your Web Hosting provider and ask them to fix this problem.

If you have tried all the steps in this article, you shouldn’t hesitate to contact your Web Hosting provider. Because a lot of times this error can be a result some recent update from your Web Host’s side.

Most web hosts offer 24/7 support and are really responsive. If you contact your web hosting service, they will help you find the issue and fix it if it’s on their end. If your web hosting service suggests it’s an error on your side, then you should hire a Web developer to fix the problem.

Alternatively, you can also try reverting your site to a previous backup if you are sure you won’t lose any new data like your latest posts.

    Leave Your Thoughts

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