fixing WordPress error establishing database connection

Fixing the WordPress Error Establishing Database Connection

If you surf the web quite often then you must have seen one liner: Error Establishing Database Connection at least once. This one liner is actually an error which prevents you from getting the desired result. It may frustrate you, but it is even more frustrating for any owner of the website. If you are a WordPress beginner, you are more likely to come across this error. In case, the above message appears, it means your website is not working and you are losing traffic causing loss in the lead generation and ultimately causing a loss in revenue.

Now the problem is how this error can be fixed. Moreover, what does the Error Establishing Database Connection exactly mean? How it could be avoided in WordPress?

In this article we will try to answer all these questions and help you restore the working of your website.

What does WordPress Database Connection Error mean?

In simple words, the error means that the WordPress is unable to establish a connection with the database. Let us try to understand this in detail.

WordPress consist of two languages: PHP and MySQL. PHP is used to post or retrieve the information while MySQL is used for the database storage. When a user types something in the url, WordPress uses PHP to retrieve the data related to that page in the MySQL database and sends it to the user’s browser.

Now, if the PHP fails to connect with MySQL database to retrieve the related data to build the required page, in that case you receive the message of Error Establishing Database Connection.

What are the reasons of this Error?

The error message may give a clear meaning but the reasons behind are not as clear. The connection error may come from many reasons such as:

Incorrect Login Credentials

Incorrect login details ( like user name and password ) may give rise to this situation. It won’t allow you to access the database.

Corrupted Database

The database can be corrupted by so many reasons. Even uploading a bad plugin may corrupt the database.

Over Loaded Database Server

If your website is experiencing a heavy traffic which is beyond the capability of the server.

Or if the server is not working properly because of some problems with your web hosting company.

Troubleshooting of Error Establishing Database Connection 

Step 1:

First of all, you have to check whether you are getting the same error on the front-end as well as on the back-end (WP admin). If it is same then you can skip this step and move on to the next one.

In case you see some other messages on the back-end like “one or more database tables are unavailable. The database needs to be repaired.” It means that the database is corrupted and needs to be fixed. You won’t find this such a difficult task.  All you need to do is just locate the wp-config.php file and add a simple code.

Locating the wp-config.php file is also an easy game. Just got to the cPanel, open the file manager. Now go to the public_html folder and there you find the wp-config.php file. From here things get even easier. Right click on the wp-config.php file and on in the edit mode.

Just add the following code there :

define  ( ‘WP_ALLOW_REPAIR’ , true );

Save the changes and go to mysite.com/wp-admin/maint/repair.php

create WordPress database connection cPanel
Create WordPress database connection cPanel

Replace mysite with your actual website url. It will redirect you to a WordPress page where you will find two options. One will be Repair Database and the other will be Repair and Optimize Database. You can choose any one of the two options.

It will help you fix the database without a login. So when you have done with the reparation, remove the code from the wp-config.php file to avoid any security issue.

If the issue has been resolved and the error has gone then great you can enjoy working. But if it is still there, then you have to continue the troubleshooting and move to the next step.

Step 2:

Check the WP-config Login details

Next step is to check your database login credentials, if these are correct or not. For that, you have to again open the wp-config.php file. Check the login details for your database there. It will be somewhat like :

define ( ‘DB_NAME’, ‘database name’);

define ( ‘DB_USER’, ‘database username’);

define ( ‘DB_PASSWORD’, ‘database password’);

define ( ‘DB_HOST’, ‘localhost’);

‘database name’                  is  the name of your database

‘database username’            is your user-name

‘database password’            is your login password

‘localhost’                          is the database host

Check the wp-config Login details
Check the WP-config Login details

If any detail is wrong, the database connection error will come. Now all you need to do is to recheck the credentials using PHPMyAdmin.

Login to PHPMyAdmin >> click on the database tab >> check the list of the database. If you can find the DB_NAME as in the wp-config.php file, then it is great. If you are unable to find the database name then it may be issue with the server and you need to contact your web host.

Checking the user-name and password

To check the username and password you have to create a file with .php extension. Create a file test.php ( you can use any name instead of test), store this file in the same folder where you found the wp-config.php file. Once you have created this file, open it in the edit mode and put the following code:

<? php

$test = mysql_connect (‘localhost’,’root’,’password’);

if (!$test) {

die (‘unable to connect’ .mysql_error());

}

echo ‘CONNECTED;

mysql_close($test);

?>

After creating the file, just go to the url of your website like

mysite.com/test.php

If you get a message of successful connection (CONNECTED in this case) then your login details are absolutely correct. In case you find some other error messages, your login details are not correct and you need the new login details.

Creating New Database User and Password

Go to the cPanel click on the MySQL Databases. Scroll down to MySQL Users, you will find the option of ADD NEW USER there. Create a new user with new user name and password and click on CREATE USER. Prepare note of these login details for the wp-config.php file. After creating the new user >> go to the MySQL Databases again >> find the option of Add User To Database. Select the user you created, select your database and click on ADD. Save the changes and update your wp-config file with the new information.

At this point, your database name, user name and password everything is fine. Lets talk about the DB_HOST. Mostly this value remains to be ‘localhost’ depending on your host’s settings. You can contact your web hosting company and ask for the value of DB_Host.

Step 3:

Corrupted Files – Sometimes the error takes result from some corrupted WordPress files. It can be resolved simply by restoring the corrupted files. But be very careful! always remember to manage a backup before replacing or restoring anything. It will help you avoid many problematic situations.

We have tried here to explain the steps to fix the problem of Error Establishing Database Connection. If the error still remains and you have done all the troubleshooting steps. You can talk to your web hosting company about the error and the implemented steps. They may help you in that case to overcome the issue. In the end what matter is a resolution and it is not that much difficult, all you need to do is concentrating to find the cause of the error and eliminate it.

Ratings

5 out of 5 stars based on 42 rating(s).