Error Establishing a Database Connection in WordPress – how to fix it?

0
259

You open your WordPress website and it is not there any more. All you see is this scary screen with the Error establishing a database connection message:

error establishing a database connectionDo not freak out! This is something really common and there is no need to panic. In this WordPress tutorial I will show you how to fix it and get your website back to normal. It is really important to have a fresh copy of your website in such moments – just in case something goes wrong.

As you can guess this error means that your WordPress website can not connect to the database and can not retrieve the needed information to show your website properly.

First I would like to mention the most common reasons for getting the Error establishing a database connection message just to give you the main idea what could be causing the problem. Depending on the reason, I will explain how to fix your WordPress website. Here are the most common reasons that cause the issue:

1. Wrong MySQL database login details

2. WordPress DataBase is corrupted

3. MySQL Database user has limited privileges

4. MySQL Server is down

5. Other reasons (if none of the cases above is causing you the issue, you must contact your Hosting Provider and have them check this for you)

How to troubleshoot Error establishing a database connection?

There are few things you should check before doing anything to fix the Error establishing a database connection message. It is quite important to troubleshoot because this error could be caused by several reasons as I already mentioned.

The first thing you should do is to check if you see the same error when trying to log in to your WordPress admin panel.

There are two options here.

You will either see the same “Error establishing a database connection” message or something like “One or more database tables are unavailable. The database may need to be repaired”.

How to fix “Error establishing a database connection” showing on your website and “One or more database tables are unavailable. The database may need to be repaired” accessing your admin panel

If “One or more database tables are unavailable. The database may need to be repaired” is the case, it is obvious that you need to repair the database. And this can be done really easy by just adding a line in your wp-config.php file which contains the WordPress installation settings and configuration.You can access it by logging into your cPanel, choosing the File Manager, and then go to the folder where you installed WordPress site:

wp-config.phpSo the line that should be added at the bottom is :allow WP database repairOnce you add it save the changes and  go to http://www.yourdomain.com/wp-admin/maint/repair.php where you will see an options to repair and optimize your database. The screen looks like this:

repair and optimize your database

You can choose any of the above mentioned options. Repair and Optimize Database may take more time depending on your WordPress database size. Once you are done with all the steps mentioned above, there is one last step you need to undergo to finalize the process. This would be to go and remove the line you have added in your wp-config.php file due to WordPress website security reasons. As you will see this link can be freely accessed and deleting the repair code from your wp-config.php will disable access to the repair page and prevent anyone else from accessing your database.

This should fix the problem and if you clear your cache and refresh, you should see your website back to normal – up and running again.

How to fix “Error establishing a database connection” message showing on front end and WordPress admin panel

If this is your case – Check your database login settings in wp-config. There are 4 main things to check here:

The name of the database (“DB_NAME”)
The login username (“DB_USER”)
The login password (“DB_PASSWORD”)
The database host (“DB_HOST”)

WordPress DB_NAME, DB_USER, DB_PASSWORD and DB_HOST

Having one of those wrong or misspelled could cause you the trouble. These are the details that your database uses to connect to your WordPress website and it is a must to have them set correctly. If you are wondering how come everything was fine yesterday and today you are seeing something wrong here, well –  have you changed your WordPress database password from your cPanel? Have you changed the database user? Have you switched hosts? If the answer to any of those questions is YES, and your wp-config file wasn’t updated with the changes (it does not automatically change the same in the wp-config.php)- this is what caused the Error establishing a database connection to your WordPress site.

How to check if your WordPress DB_NAME, DB_USER, DB_PASSWORD and DB_HOST are correct.

The best you can do is to contact your hosting provider and ask them to provide you with this information, then see in your wp-config.php if they all match. I would best advise you to do so, simply because messing something by mistake in the PHPMyAdmin configuration might cause you a big trouble. Most of the hosting companies use localhost for DB_HOST, however there are some who can require you to use the IP address for example. So, remember to check this as well.

If you still wish to check the settings by yourself , access your cPanel and navigate to PHPMyAdmin tool where you will see a list of databases on the left side. Choose the one that matches the name in your wp-config.php file and you will see the names of the tables in your database. Then simply go to wp_options, click Browse and make sure all your general website settings are there – you should see your WordPress website’s name, the url and so on. If there is mismatch between the database name in the PhpMyAdmin and the wp-config.php file, edit the wp-config.php file and see the results.

As for the database user and pass you can either test the current ones by creating a .php file in the directory where you have installed your WordPress. You can name it for example testuserpass.php and  paste the following code :

<?php
$test Connection = mysql_connect(‘localhost’, ‘root’, ‘password’);
if (!$testConnection) {
die(‘Error: ‘ . mysql_error());
}
echo ‘Database connection working!’;
mysql_close($testConnection);
?>

Once changes are saved – go and check if it connects you by entering the following url : yourdomain.com/testuserpass.php

If yourdomain.com/testuserpass.php doesn’t load you will have to create new user and password from the MySQL Datatabases tool in your cPanel. Once you do so, update the same in your wp-config.php file and see if it will resolve the problem.

If you still get the same error – this means that you need to contact your hosting company and check the MySQL Server.

What to do if MySQL Server is down or your user have limited privileges and your WordPress site is not working?

If your MySql server is down or your user does not have the permissions needed , you will definitely see the Error Establishing a Database Connection when accessing your WordPress website or WordPress admin panel. You will not be able to open the site correctly at all. You need to contact your hosting company in order to have the issue fixed on their end. Once the MySQL server is up and working, you can test your site again – it should be loading fine. If this happens more than 2-3 times…well..it is time to find better WordPress hosting provider.

Thank you for reading this WordPress tutorial on Error Establishing a Database Connection in WordPress – how to fix it? I really hope I have managed to help with the most common solutions. This article will be expanded once new fixes are getting more common and popular, so that we can cover most of the cases and help more people quickly solve the  Error Establishing a Database Connection problem. Feel free to share and comment.

NO COMMENTS

LEAVE A REPLY