When you open your WordPress website, you receive a notification stating “Error Establishing Database Connection” , reasons may be one of the following:

Error Establishing Database Connection
  • The database has been corrupted.
  • The login details provided in wp-config.php for the database are incorrect.
  • The tables in the WordPress database have been damaged or improperly imported.
  • The table prefix is wrong.

Here, we will learn How to fix An issue has occurred while trying to connect to the database. for your WordPress-based Website.

Best To FIX “Error Establishing Database Connection” In WordPress [With in 1 Minutes]

Firstly, verify the login details for the database.

After transferring your WordPress website from one hosting provider to another, you must make sure to revise the database connection information provided in the wp-config.php file.

English: To discover how to modify the wp-config.php file, click on this link DirectAdmin or  cPanel  .

Find the database connection details provided in your configuration file.

Verify the connection information provided and ensure that it is accurate. If any of the connection details are incorrect, please make the necessary updates, save the information, and then exit the application.

Refresh the webpage to see if the problem has been fixed or not.

Step 2 — Examine the table prefix.

Access PHPMyAdmin and select your database.

Error Establishing Database Connection

Verify the prefix provided prior to the name of the table. The table prefix is specified within the wp-config.php file, and it should match the one displayed in PHPMyAdmin.

How to resolve "Error Establishing Database Connection" in WordPress 2
How to resolve "Error Establishing Database Connection" in WordPress 3

Step 3: involves attempting to fix any issues with the WordPress Database.

cPanel offers the option to repair a database, and we will guide you through the process with the steps outlined below.

 Through cPanel 

 1.  Login into cPanel  .

 2.  Navigate to the  DATABASE  section and click on  MySQL® Databases 

How to resolve "Error Establishing Database Connection" in WordPress 4

 3.  Go to the  Modify Databases In the section, choose your database from the provided options, and then click on it. Repair Database. 

Modify databases

 4. A fresh graphical user interface (GUI) will appear, and the system will make an effort to automatically check and fix the database.

 Through PHPMyAdmin 

We also have the option to fix or restore our database using PHPMyAdmin. Now, let’s take a look at the steps involved in repairing the database through PHPMyAdmin.

 1.  Login into cPanel  .

 2.  Navigate to the  DATABASE  section and click on  phpMyAdmin. 

cpanle phpmyadmin

 3. In a separate browser tab, PHPMyAdmin will open. Select the specific database you wish to fix.

How to resolve "Error Establishing Database Connection" in WordPress 5

 4. If you are confident that the table is the cause of the problem, you have the option to choose that specific table or inspect all of the tables for repair.

How to resolve "Error Establishing Database Connection" in WordPress 6

 5.  From the drop-down  With selected:  choose  Repair table  . This will execute the  REPAIR TABLE If you have chosen specific tables, you can perform an SQL query on those tables. Alternatively, if you have selected all tables, the SQL query will be executed on all of them. Check All, and an inspection or fix will be performed on the tables.

How to resolve "Error Establishing Database Connection" in WordPress 7

 Through wp-config.php 

WordPress has pre-existing functionalities that are designed to fix any issues with the database. However, these functionalities are initially deactivated as a security precaution.

In this lesson, we will understand the process of activating the function, conducting the necessary fixes, and subsequently deactivating it in order to prevent any potential security risks.

Access the wp-config.php file and insert the code provided below.

Save and close the editor.

To access the website, open your web browser and enter the specified URL.

Please substitute “yourdomain.com” above with the name of your actual domain.

http://www.yourdomain.com/wp-admin/maint/repair.php

Press the  Repair Database  button, which will appear at page load.

Once you click on the button, you will be redirected to a page that displays live updates of checks and repairs taking place.

Once you have completed the process, please revisit the wp-config.php file and delete the line of code that was inserted to deactivate the repair function.

If the steps mentioned above did not resolve your issue, please proceed to restore your database using the previous backup you have.