Error establishing a database connection

How to fix an “Error establishing a database connection” in WordPress?

by Aushini Das

Have you seen an error saying “Error establishing a Database Connection” on your WordPress site? If yes, then you are not the only one. This fatal error can create problems and make your WordPress website inaccessible to the users.

This kind of error appears if WordPress is not able to connect with the database. There are several reasons which make this error occur. It makes it difficult to deal with the troubleshooting of this error, especially for beginners.

In this article, we will see some essential things and the methods of fixing this error that you should know if you use WordPress for your website. So, let’s begin.

Inferring the problem

Before landing on any conclusions and fixing methods, we need to understand exactly what the problem is. We got to know earlier that it is basically an error to establish a connection of WordPress with its Database. This self-explanatory term is not enough to solve the problem.

Here is what the error actually means:

WordPress uses two software technologies that provide us access to its unique abilities. These two are PHP and MySQL.

  • PHP: The core files of WordPress are written in this programming language. However, we don’t need to dwell deep into it. So, it is enough to know for now.
  • MySQL: WordPress uses this database technology to store all the content of the website. It includes everything – your pages, posts, more minor elements like the layout of the widgets, the title of your WordPress website, color settings, and so on. It means even the smallest information about your WordPress website is being kept with MySQL. (Indeed, an important part, Right?)

So, here is the way this error comes into action. With this, the possible reasons for such error may include the following:

  • Incorrect Login Credentials: Specific login details are required to access the database. When these are somehow changed, WordPress becomes unable to fetch any information from the database.
  • Corrupt WordPress Files: Failed updates of either plugin, themes, or even the software updates can cause the WordPress files to get corrupted.
  • Corrupt Database: It can occur due to a hacker’s attack, a rogue plugin, theme failing, and so on. It all messes up with the database and thus, causes such errors.
  • Database server got down: This can also be a probable cause of an error when the database server gets to be somehow down.
  • Too much website traffic: However, this is good for us, but it is still a problem that needs to be solved. This can make your database unresponsive because of spiked traffic on your website.

Also Read How to use vi editor in Linux/Unix?

Methods to fix error in Database Connection in WordPress

It is not always possible to identify the exact reason behind this error, but trying some of the quick fixes which work in almost every condition can be a good idea. So, here we will be discussing the most important and accessible methods of fixing this error. These are:

  1. Verification of Database Credentials: It is done in cPanel. WordPress basically talks to MySQL by using the following 4 pieces of information.
  2. Database name
  3. Database server’s hostname
  4. A password
  5. A username

In the installation root directory of WordPress, these four are stored in the form of variables in the file, wp-config.php. These appear as:

/**MySQL database username */

define(‘DB_USER’, ‘xxxxxx’);

Firstly, you need to verify the database credentials by comparing the values which are listed in the database file to those listed in wp-config.PHPfile. In cPanel, open phpMyAdmin, and in the left menu, look for the WordPress database. Keep in mind that the name of the database and the variable DB_NAME of wp-config.php should match.

Now, in the database entry, go on the ‘Users’ section and make sure that the database username and password are as same as the values being used by WordPress. If it is not so, then edit the values to make these match.

If the same error still persists, then you can check for the database or file corruption.

  • Repairing the Database: Database corruption is comparatively easy and is not risky for us to fix. So, before trying our hands on any other method, we should consider fixing the database.

The first method of repairing the database includes the following steps:

  • In cPanel, select phpMyAdmin.
  • Navigate to your WordPress database.
  • Now, select all tables given.
  • From the dropdown menu, click on the option saying ‘Repair table’.
Error establishing a database connection

The second method of database repairing works by the built-in repair mode of database in WordPress. You can get this activated just by adding a line to a file named wp-config.php

This line to be added is:

define(‘WP_ALLOW_REPAIR’, true);

After this, you have to navigate to the given URL:

Select the option saying ‘Repair’ and then wait for WordPress to do its work. It will take some time and after completion, make sure you remove the coding line added earlier to wp-config.php. It is important to remove because anybody can have access to the above-said URL.

  • Replacing WordPress corrupt files: Corruption of WordPress files can occur by malware, hardware problems, or user errors. What we will have to do here is to download new freshly downloaded files from the website while keeping your website’s themes and plugins as such.
  • First, install a WordPress copy, then unzip this copy and navigate to the WordPress directory. From here, you have to delete the wp-config.php file and wp-config-sample.php file, as well as delete the wp-content folder.
  • From the cPanel file manager, you have to open the WordPress root folder and then, from the local WordPress copy, upload the leftover folders into it.

This way, the core WordPress files will get easily replaced with the new files, and the error should go away.

Also Read Database Architecture in DBMS

The Bottom Line

However, the “Error establishing a database connection” is somewhat scary to see. But, after looking over all the information we shared, you must know how to resolve the issue now.

If any of the given methods of resolving this error does not work, you may need to restore the files and the database, which means the complete website. You can do it from the most recent backup of your website before the occurrence of this error.

Related Posts

Leave a Comment