GENERAL INFORMATION

WordPress : How to resolve the Error Establishing a Database Connection error

WordPress is one of the most popular content management systems in the world, used by millions of websites for its flexibility and user-friendliness. However, like any software, it can encounter technical issues. One of the most common and frustrating errors for site administrators is the “Error Establishing a Database Connection.”


This critical error makes your site inaccessible because WordPress cannot connect to the database. Resolving this issue can be difficult for beginners due to the various possible causes.

In this article, we will guide you step by step to fix this error.

This error indicates a loss of communication with your WordPress database, taking your site offline. It is crucial to resolve it quickly as it affects your sales, traffic, and analytics. We will cover common causes and simple solutions to get your site back online quickly.

What does the WordPress message “Error Establishing a Database Connection” mean?

The database connection error occurs when two essential parts of WordPress—PHP (Hypertext Preprocessor) and MySQL (My Structured Query Language)—stop communicating with each other.
PHP is the scripting language used to perform actions or operations on the server, while MySQL is the relational database management system that stores and manages WordPress data, including user information, posts, and settings. When these two essential parts stop communicating, WordPress cannot retrieve the necessary data to display your website.

What causes the message “Error Establishing a Database Connection” in WordPress?

A database is software that facilitates the storage, organization, and retrieval of data for other software. As a content management system, WordPress uses a database to store all your content and other site data, connecting to this database each time a visitor accesses your site.
To connect to the database, WordPress needs the following information:

Database name
Database username and password
Database server
This information is stored in the wp-config.php file.

Therefore, if:

Your database credentials are incorrect,
The database is corrupted,
The database server is down,
it can lead to the error “Error Establishing a Database Connection.”

How to fix the “Error Establishing a Database Connection” issue in WordPress?

Start by checking that your database connection information is correct, as this is usually the primary reason for the “Error Establishing a Database Connection” message.
This is particularly common after migrating to a new hosting provider. The connection details for your WordPress site are stored in the wp-config.php file, which is typically located at the root of your site. This file contains four crucial pieces of information that must be correct for the connection to be established successfully.

To access it, log in to your cPanel account, then look for the Files section. Next, click on File Manager.


Then, click on Public_html where the root files of your website are located.


Look for the wp-config.php file, then right-click to edit it.

Here is an example below of what the file looks like once opened.


Now, you need to check your current values against those on your server to ensure they are correct.

You can confirm this information from your cPanel dashboard.

In the Databases section, click on MySQL Databases.


Here, you will find the database name and username in the Current Database section. Be sure to copy them into a notepad for later addition to the wp-config.php file.


Then, scroll down to the Current Users section where you will find the Change Password link next to your database name. Clicking it will redirect you to a new screen where you can change the database password as desired.


After verifying and confirming all the details of your database, you can modify them in the wp-config.php file if necessary.

Next, try visiting your website to see if the database connection error persists.

If the error continues, this indicates that there is another issue to resolve.

Corrupted WordPress core files

One possible reason you might see the “Error Establishing a Database Connection” message is the corruption of WordPress core files. To resolve this, you will need to replace only the core version of WordPress on your site. Remember to back up your site before starting to avoid losing any changes you may have made to files such as .htaccess or wp-config.php.
To do this, you can reinstall the essential files using WP Toolkit.

Restore a backup version

As a last resort, you can always restore a backup version where everything was functioning correctly. However, keep in mind that all changes made after the selected date will be lost.

Articles Liés