HomeWordPress

How to Fix the Error Establishing a Database Connection in WordPress

How to Fix the Error Establishing a Database Connection in WordPress
Like Tweet Pin it Share Share Email

While using WordPress, you may be encountered with a number of errors. These errors can be caused by anything like, it can be your fault, system breaks, natural calamities and others. Even an increase in the traffic can break your website.

But this error establishing a database connection is the most common and frightening error that a WordPress user might encounter.

This error can be caused by many reasons and it means that you no longer have access to your website database and hence your site goes down.

This error is not something that can be taken lightly, and thus one should try and solve the issues instantly as this can have a diverse effect on your sales, traffic and analytics. In this article we will explain this WordPress error and also how to fix it to restore your website.

Why do you get this WordPress error?

If you are getting this error on your WordPress website then it’s because your WordPress is not able to create a data connection. All the data of your WordPress website like post data, the page data, plugin settings, login information, etc., all are stored in your WordPress database.

The data that is not stored in the database are the image content and the themes/core files. The reason why your WordPress is not able to create a connection can differ.

The database connection tells you nothing about the problem which cause it. There could be a number of reasons why your database is unapproachable.

It could be the reason that your database has been corrupted, the database login credentials are wrong or have been changed, the server not available, etc. Now let’s take a look on how to solve this problem.

Does this WordPress error issue occur for wp-admin as well?

The very first thing one needs to do is to check that the same error is on the both sides, the front-end and the back-end of your WordPress website.

If you are getting the same error establishing a database connection message on both the ends, then go to the nest step.

Or on the off chance that you are getting some different message on the both the sides, like one more database tables are unavailable, then the databases with theses error messages needed to be repaired.

Then you need to add the following given below line in your wp-config.php files. You need to add this just before this line ‘That’s all, stop editing! Happy blogging’ line wp-config.php.


define('WP_ALLOW_REPAIR', true);
  

The above given code will allow you to repair your WordPress database without having to login into your phpMyAdmin manually.

After you have done this then you can see the setting be visiting the page given: http://www.yoursite.com/wp-admin/maint/repair.php

Make sure you remember that the user does not need to be login for accessing this functionality when this define is set.

This is for the reason that its principle purpose is to fix adamaged database, Users can frequently not login when the database is corrupt. So, once you are finished fixing and optimizing your database, try to remove this from your wp-config.php.

In the event that this repair did not fix the issue, or you are experiencing difficulty running the fix at that point keep reading this article.

Checking the wp-config file:

This wp-config file is the most important file in your WordPress installation. This the section where there are details for WordPress to connect your database.

On the off chance if you change the user or root password, then you also need to change this file too. The first thing you needed to check is everything in your wp-config.php file same.

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

Your DB_host value many will not be localhost, it may be different depending on the host. It is localhost for the popular host like HostGator, BlueHost, SiteGround.

Some of the people also recommended that they solve their issue by replacing localhost with the IP.  Here IP address will differ from online internet host services.

And if everything on the file in WordPress is correct, then there might be something wrong with the server.

Do Check your Web Host (MySQL Server):

When your WordPress website is flooded with a lot of traffic, then frequently you will see this error establishing a database connection.  Fundamentally, your host server just can't deal with the load, especially when you are on shared hosting plan.

Your website will get slow and for certain clients will show the error. So, the best thing you ought to do is jump on the telephone or livechat with your hosting supplier and inquire them whether your MySQL server is responsive.

If you want to test your MySQL server is running all by yourself, then fir this one can do little things. One can check their other sites on the same hosting server if they are having any problem.

And after checking your other website, if they are too getting the same error establishing a database connection, then there is definitely something wrong with your MySQL server.

In other case if you do not have any other website then one can also check by simply going to your cPanel and try accessing your phpMyAdmin and then connect with the database.

If you are able to connect, then we have to verify that your user database has necessary authorization. Now create a new file called testconnection.php and copy the code given below.

<?php
$link = mysqli_connect('localhost', 'username', 'password');
if (!$link) {
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully';
mysqli_close($link);
?>

Ensure the you have replaced the username and the password. Check if the script is successfully connected, then the user has necessary authorization, and thus there was something other wrong. Go back and recheck your wp-config file and ensure the whole thing is correct.

If you are not able to connect to your database from the phpMyAdmin, then definitely somewhat is wrong with your server. The reason might be that your server is down or user does not have sufficient permissions.

With the encounter of this error, you do not need to worry as with the above given information can help you with your issue. In the case that you cannot solve your problem, then your web host is always at your back and you can call them.

We hope that this article helps you learn how to fix error establishing a database connection in WordPress.