fbpx

The “error establishing a database connection” is a common WordPress error and can be panic striking especially for beginners. This is because once you encounter the error within your website, you are not in a position to view any other website content. There is just a a blank screen with the text.

This error can be frustrating to your site users since they are not in a position to view anything on the site. Fortunately this error can be fixed. It is important to have this error fixed as soon as possible.

Within this article we will look into what the “error establishing a database connection” is, its common causes, as well as how to go about fixing the error.

Table Of Contents

What is the “Error Establishing a Database Connection”

Common Causes of the Error

How To Fix “Error establishing a database connection” In WordPress

Conclusion

What is the “Error Establishing a Database Connection”

This error basically means that your site is not in a position to connect to your database.

In order to get a full understanding of the error, let’s begin by covering how the WordPress content management system works.

WordPress relies on two pieces of technology in order to properly work. These include PHP which is a server-side programming language, and MySQL which is a database server.

All functionalities in WordPress are defined in static files that are coded in PHP. The MySQL database server on the other hand manages WordPress databases. A WordPress database stores all the data within your website such as posts, pages and media items.

When a user types in a URL within their browser, PHP fetches the content from your database and this content is then presented within your browser. If the user is presented with the “Error Establishing a Database Connection”, then this means that PHP is not able to connect to your database to fetch the requested content.

Below is a sample display of the error:

In the above screenshot, the PHP-MySQL connection is not present hence the error is generated.

Common Causes of the Error

There are a number of reasons why you can encounter the “error establishing a database connection”. Some of these include:

Incorrect Database Credentials

This is actually one of the common reported causes of this error.

Similar to your WordPress dashboard where you need a username and password to login into it, also your website requires credentials to connect to your database. If these credentials are altered by any chance, then your website will render the “error establishing a database connection”.

Server Overload

If your server could be having numerous requests than it can handle at a given time, then this can as well lead to the “error establishing a database connection”.

This is especially common in websites running on a shared hosting.

Corrupt Database

Unnecessary manual changes to your database could render the database as corrupt. For example if you happen to delete a row or table within your database that is perhaps required for a database connection to occur, then you are likely to encounter the “error establishing a database connection”.

Poorly coded themes and plugins can also lead to a corrupt database. If for example you have a plugin that is rarely updated, hackers can exploit the plugin and use to execute SQL queries that alter your database contents, thereby leading to a corrupt database.

Corrupt WordPress Files

Another cause to the error is having in place corrupt WordPress files within your installation. It is possible that you may have WordPress files in place that may have been modified in order to infringe the database connection. This is a practice used by hackers to create a backdoor to your website, thereby triggering the “error establishing a database connection”.

Failed WordPress updates are also a likely cause of of corrupt WordPress files.

How To Fix “Error establishing a database connection” In WordPress

Despite the panic caused by the “Error establishing a database connection”, this error can actually be fixed. We however recommend carrying out a full backup to your site prior to carrying out any changes. Here is a guide on how to backup your WordPress site.

Once done with the full backup, you can now proceed to trying out the fixes shared below.

Verify Your Database Login Credentials

In order for your website to connect to your database, database credentials are required. These credentials are stored within the Wp-config.php file which is within the root of your WordPress files. You can access the file via FTP with the help of an FTP software such as Filezilla. Alternatively, you can access the file via your hosting panel such as cPanel. Below is a screenshot illustration on the file:

Once you locate the file, select to edit it and within it you will find the details below that are required for your site to access the database:

  • The Database Name (“DB_NAME”)
  • The MySQL database username (“DB_USER”)
  • The MySQL database password (“DB_PASSWORD”)
  • The MySQL host name (“DB_HOST”)

Below is a screenshot of these details:

You now need to countercheck whether these details match with those within your database manager.

To begin with, we will first confirm the database name and host name. As for this, you will need to access the phpMyAdmin from your hosting panel.

If you are using cPanel, you can access the phpMyAdmin from the Databases section as illustrated below:

Upon accessing phpMyadmin, click on the “Databases” tab and check whether you have a database with a similar name to what you have within your wp-config.php file. This is the name after ‘DB_ Name’ in your wp-config.php file.

You can ignore the information_schema database in place as this is your host database.

Still in the same section, check on your database server. This can be checked upon clicking your database and checking on the server name. Below is a sample illustration on this:

You will need to compare this with the value in your wp-confg.php file under DB_Host.

The next step is to check on the user credentials.

If your site is under cPanel, you can have this information checked upon from the “MySQL Databases” section.

Click on it and check on the “Current Users” section. Here, you should have a view of the username and a link to change your password as illustrated in the screenshot below:

If the username does not match what is within the wp-config.php file, alter the value in the wp-config file to the value within the current users section.

As for the password, you can alter it from the “Change Password” link. Prior to doing so, you can however quickly test whether the credentials within your wp-config.php work.

To carry this out, within the root of your WordPress files, create a file named dbtest.php as illustrated below:

Select to edit the file and add the following code:

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

Within the code, replace localhost with your ‘DB_Host’ name if it is not localhost. Also do replace root and password with your ‘DB_USER’ and ‘DB_PASSWORD’ respectively.

Next, access your site file from your browser by typing the address yourdomain.com/dbtest.php . Ensure that you replace yourdomain.com with the actual domain URL.

If the connection is successful, you will have a view of the “Connected Successfully” message. Below is a sample screenshot.

In the above case, you don’t need to alter your credentials as it is evident that your login credentials are not the cause of the issue.

On the contrary, if the credentials are not correct, you will have a view of such a message “Could not connect: Access denied for user…” . In such a case, you will need to alter the password if the username is correct, by clicking on the “change password” link. Alternatively, you cam create a new user and use the new credentials within the wp-config.php file.

Repair Your WordPress Database

The “Error establishing a database connection” can also be caused by a corrupt database. If the error is not due to the credentials or rather if the connection was successful in the previous step then you can consider repairing the database.

In order to repair your database, you will need to carry out the following:

i) Access your wp-config.php in the root of your theme files

ii) Select to edit the wp-config.php file

iii) Add the code below within the file:

define ( 'WP_ALLOW_REPAIR' , true);

The code should be added just before the line:

/* That's all, stop editing! Happy publishing. */

iv) Within your browser type in your url in the format yourdomain.com/wp-admin/maint/repair.php . Ensure that you replace yourdomain.com with your actual domain.

Once this is done, you will have a view of such a display within your site:

Next, click on the “Repair Database” button in order to start the automatic database repair process.

If this does not fix the issue, then you can try out other solutions offered as well.

Theme and Plugins Check

In some cases, themes and plugins that you have in place may cause “Error establishing a database connection”. In order to ascertain that you can carry out the following:

Plugins Check

i) Access your site files via FTP using a Software such as Filezilla or a hosting panel such as cPanel

ii) Within the root of your site files, navigate to the Wp-contents folder and rename the plugins folder to “plugins-old”

iii) Access your site and see if the “Error establishing a database connection” is resolved.

iv) If the error is resolved, rename the “plugins-old” folder back to “plugins”

v) Access the plugins section within your WordPress dashboard and activate the plugins one by one checking whether the error is resolved in any instance, in order to identify the plugin causing the issue.

Once you identify the conflicting plugin, disable it once again and consult the plugins’ author about the issue.

Themes Check

i) Similar to the plugins check, access your site files via FTP

ii) Within the root of your site files, navigate to the Wp-content > themes folder and rename your currently active theme. You can do so by adding a full-stop to it

iii) Visit your site to see if the “Error establishing a database connection” is resolved

If the error is resolved, this means that the issue is being caused by your theme. You will need to consult the theme author or support in such a case.

Fixing Corrupt WordPress files

In some cases the error may also be caused by corrupt WordPress files especially if a hacker had gained access to your site files.

In order to check whether this is the case, you will need to carry out the following:

i) Carry out a full backup to your site. Here is a guide on how to go about this.

ii) Download WordPress from wordpress.org

iii) Unzip the download

iv) Delete the wp-content folder as well as the wp-config.php and wp-config-sample.php files.

v) Upload the remaining files to the root of your site files via FTP using a Software such as Filezilla or a hosting panel such as cPanel.

This will overwrite the existing files with new files. Once this is done, reload your web page and see whether the “Error establishing a database connection” is now resolved.

Consult Your Hosting Provider

If the above suggestions do not get to help, it is advisable that you consult your hosting provider about the “Error establishing a database connection”. It could be that there could be a downtime on the database server.

Your hosting provider is in a position to check into the issue and determine the exact cause and fix to it.

Conclusion

The “Error establishing a database connection” is a common WordPress error. Should you encounter the error within your website, we recommend trying out the methods above to resolve the issue. It is also important that you carry out full backups to your site as they serve as great restoration points should such an error occur.

We hope that the methods outlined above will be of help in resolving the “Error establishing a database connection” in the future. Should you have any questions or comments, feel free to raise them in the comments section down below.

Comments to: How To Fix Error Establishing A Database Connection In WordPress

    Your email address will not be published.