Skip to content
Your Hosting Ad Goes Here KBAssets

How to Fix the “Error Establishing a Database Connection” in WordPress

The “Error Establishing a Database Connection” in WordPress usually occurs when WordPress can’t connect to the database. This error is often due to incorrect database credentials, a corrupted database, or issues with the database server. Here’s how to troubleshoot and fix this error.


Step 1: Confirm Database Credentials

Ensure your WordPress configuration file has the correct database credentials.

  1. Access wp-config.php: Use your hosting control panel’s File Manager or an FTP client to locate the wp-config.php file in your website’s root directory.
  2. Check Database Details:
    • Locate these lines in the file:
    • Ensure each value matches the details in your hosting account.
define('DB_NAME', 'database_name');
define('DB_USER', 'database_user');
define('DB_PASSWORD', 'database_password');
define('DB_HOST', 'localhost');
  1. Save Changes: If any details were incorrect, update them and save the file.

Step 2: Check Database Server

In some cases, your database server may be down.

  1. Test Database Connection:
    • Create a new PHP file in your website’s root directory, such as dbtest.php.
    • Add this code to the file and save it.
    • Replace localhost, database_user, and database_password with your actual database details.
<?php
$link = mysqli_connect('localhost', 'database_user', 'database_password');
if (!$link) {
    die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully';
mysqli_close($link);
  1. Run the Test:
    • Go to https://yourdomain.com/dbtest.php.
    • If you see “Connected successfully,” your server is working. If you see an error message, your server may be down—contact our support team
  2. Delete dbtest.php after testing.

Step 3: Repair the Database (If Corrupt)

If you can connect to the database but still see the error, your database may be corrupt.

  1. Enable Database Repair:
    • Open wp-config.php and add this line near the bottom:
    • Save and close the file.
define('WP_ALLOW_REPAIR', true);
  1. Run Repair Script:
    • Visit https://yourdomain.com/wp-admin/maint/repair.php.
    • Click Repair Database or Repair and Optimize Database to fix any corruption.
  2. Remove Repair Code: After repair, delete the define('WP_ALLOW_REPAIR', true); line from wp-config.php to prevent unauthorized access.

Step 4: Restore a Backup (If Available)

If you have a recent backup, restoring your database might be the quickest solution.

  1. Log in to your hosting control panel and locate the Backup tool.
  2. Restore Database: Choose the most recent backup of your database and restore it.

Step 5: Contact Support Team

If none of the above steps work, reach out to our support team for assistance. They can check for server issues or help identify database connection problems.


By following these steps, you should be able to resolve the “Error Establishing a Database Connection” and get your WordPress site back online.

Was this article helpful?
YesNo
Back To Top