How To Fix WordPress Error Establishing A Database Connection

How To Fix WordPress Error Establishing A Database Connection

WordPress Error Establishing Database Connection

If you are a WordPress site developer or webmaster, then you might often find that the site that you have developed is down. This not only puts a negative impact on the site’s reputation and decreases its ranking but you also need to go through a terrific situation.

WordPress error establishing a database connection is a quite well-known error which is a kind of nightmare for all the WordPress site developers. Probably you find this error while entering your site’s URL. You can feel low when this error makes your site run in a loss.

It is so annoying when you do not know the way to get rid of this situation. Fixing this WordPress error seems tough until you do not know the proper procedure to do it. 

Once you know the proper way to fix this error, you will be able to fix the error and push your site ranking and traffic like the way you do. Though it looks a bit confusing, fixing this error is not a tough job to do.

What ‘Error Establishing A Database Connection’ Actually Is?

Before going to the solution, you need to know about the error in brief so that you understand the whole thing properly. When the PHP code is not able to connect to your MySQL database, this error occurs. Because the PHP fails to retrieve the information which is required to build the page fully. This is the reason you see a black page when the error occurs because there is no information at all.

If you are facing this error, then don’t get nervous. Here you are going to get all the details regarding the error establishing a database connection WordPress. You will get an idea about the error, the causes behind it and all the solutions to fix it. So, go through the below section and gather all the information that you need to get rid of the situation and fix the error easily by yourself.

Causes Behind this WordPress Database Connection Error

There is no doubt that if you know the causes behind the error bothering you, it will help you to understand the whole circumstance. And that will help you to end the problem more feasibly. Here are those reasons discussed in detail.

Wrong Login Credentials:

If you put the wrong username and password when you are going to access the admin panel, you will face this error. You are not allowed to access your database with the previous login credentials if those are changed recently. 

So you have to make sure that you are not entering any wrong login credentials.

Database Corruption:

Due to a number of faulty and incomplete login details, WordPress databases can get corrupted. This is another reason for which you can face this error.

Fix Error Establishing A Database Connection” With Feasible Solution:

Error Establishing A Database Connection


Here are the solutions that you have to follow to remove the WordPress error establishing a database connection and push the ranking and the traffic of your site.

Solution 1: Try Repairing the Database

In the first step, you have to make sure that the database is not corrupted. If you are getting this error while you are trying to access the WordPress admin page but you can see the site, then there is a high chance that you are dealing with a corrupted database. You will be able to repair your database easily with the help of the built-in WordPress feature.

  • First, you have to enable the feature as it is disabled by default. 
  • Then you have to go to the wp-admin.php file and add the code define( ‘WP_ALLOW_REPAIR’, true ). With the help of this code, your database can get repaired. 
  • Go to the after you add the line. You see a page that will show you two similar options. The options are ‘Repair’ and ‘Repair and Optimize’. 
  • With the help of these options, you will be able to repair and optimize your database in future.

If you are in a hurry, you can choose the ‘Repair’ option as the ‘Repair and Optimize’ option takes much time.

You have to also keep in mind that the Repair page which you are into right now is not a secure page. Anyone is able to access the URL and there is no need to login to access that. 

So double-check if you have removed the line of code that you put before after the repair is done. If you do not do this, then anyone is able to access your database in the future.

Solution 2: Try Changing the Login Credentials

Changing the Login Credentials

If you are still facing that WordPress error establishing a database connection and are not able to access your website after repairing your database, then you can check if there is any fault in your login settings. If you have changed some of your useful information or your hosting company and did not update it in the wp-config file manually, then your database login credentials might stop working.

So, you have to open the wp-config file again and look for the database login information. Probably you are going to find it near the top of the file. You will find the name of the database there as “DB_NAME”, the username of it as “DB_USER”, its password as “DB_PASSWORD” and the host of your database as “DB_HOST”. 

You have to double-check if all the things are correct or not after you go through all this information. In case, any of these is not correct, WordPress will not be able to connect the database.

You can use PHPMyAdmin if you want to check your database outside. This is a tool that helps you to access and edit the MySQL databases. But you have to keep in mind that this tool deals directly with the database of your site. 

So things can be messed up and you have to be aware of it.

  1. You are going to find a list of databases on your server after you login from your PHPAdmin account. Choose your database from the list. You have to click on the one that is similar to the name in the wp-config file.
  2. You are going to find many tables’ names in the database after you select the database name. Look for the wp_options table and hit the ‘Browse’ button after it to check if the database name is right that you have got.
  3. A page will appear and show you all the general settings including the name and URL of your WordPress website. If you find that it is not the same that you saw before in the wp-config file, then you have to fix it in the wp-config file before you proceed.

It is mainly done for the database name. There are a few things that you have to check to make sure the password and the username are correct. 

You can create a simple .php file to check the present username and password and you will get to know that if you are able to connect the database. So create a .php file, and add the below code after naming it.

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

You can go to the URL of the file and test the connection with the help of this code.

Then go to the cPanel called MySQL Database to get a different tool that you need to create a new database user and password. Once you are there, select Add New User that you are going to find under the heading of MySQL Users. You will see a new page asking you for the new login credentials.

Make note of the username and the password in the wp-config file that you have chosen. Then choose ‘Create User’ and select Add User to Database. After that, you have to put your new username and the WordPress database and hit the Add button. 

Now update the wp-config file with the new username and password to make sure the login credentials are correct.

These are the overall steps that you need to do to get rid of the WordPress error establishing a database connection. So, follow the above section and perform the solutions properly.

Comments are closed.

Open chat
Hello there!
Can we help you?
Call Now Button