Skip to main content
All CollectionsTroubleshooting and FAQsCommon Issues
How to Fix the "Error Establishing a Database Connection" in WordPress
How to Fix the "Error Establishing a Database Connection" in WordPress

Learn how to troubleshoot and fix the Database connection issues in WordPress to quickly restore your site's functionality.

Updated over 4 months ago

Encountering the "Error Establishing a Database Connection" message on your WordPress site can be both frustrating and alarming, especially if you're not sure where to start with troubleshooting.

This common issue can prevent visitors from accessing your site and disrupt your workflow.

Fortunately, understanding the root causes and systematically addressing them can help you quickly restore your site to full functionality.

Check Your Database Login Credentials

The most common reason for this error is incorrect database login credentials. WordPress stores these details in the wp-config.php file.

  1. Access Your Site’s Files: To locate your site's files, use an FTP client or your hosting provider’s file manager.

  2. Open wp-config.php: Find and open the wp-config.php file in the root directory.

  3. Verify Credentials: Ensure the DB_NAME, DB_USER, DB_PASSWORD, and DB_HOST values are correct.

Check Your Database Server

Sometimes, the database server might be down.

  1. Contact Your Hosting Provider: Ask if there are any ongoing server issues.

  2. Test Connection: Use a simple PHP script to test the connection to your database server.

Create a new file named testconnection.php with the following code and upload it to your site’s root directory:

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

Replace 'localhost', 'username', and 'password' with your actual database host, username, and password. Then, access this file via your browser (http://yoursite.com/testconnection.php).

Repair Your Database

If the database credentials are correct and the server is up, your database might be corrupted.

  1. Add Code to wp-config.php: Add the following line to your wp-config.php file to enable automatic database repair:

    define('WP_ALLOW_REPAIR', true);

  2. Run the Repair Script: Navigate to http://yoursite.com/wp-admin/maint/repair.php and follow the instructions.

  3. Remove the Repair Code: Remove the repair line from your wp-config.php file.

Check Your Site’s URL in the Database

Sometimes, the site URL in the database might be incorrect.

  1. Access phpMyAdmin: Log into your hosting account and access phpMyAdmin.

  2. Select Your Database: Choose your WordPress database.

  3. Check wp_options Table: Look for the siteurl and home rows in the wp_options table. Ensure both are correct.

Re-upload Core WordPress Files

Corrupted WordPress core files can also cause this error.

  1. Download WordPress: Get the latest version of WordPress from wordpress.org.

  2. Re-upload Files: Upload the wp-admin and wp-includes folders to your site using an FTP client.

Important Note: Don’t overwrite the wp-content folder or wp-config.php file.

Restore a Backup

If you regularly back up your site, restoring a recent backup might resolve the issue.

  1. Access Backup Files: Locate your backup files.

  2. Restore Database and Files: Follow your backup tool’s instructions to restore both the database and site files.

Contact Your Hosting Provider

If none of the above steps work, contact your hosting provider for further assistance. They can check server logs and help identify the issue.

Note: Check out the How To Fix “Error Establishing A Database Connection” in WordPress article for an in-depth step-by-step tutorial.

Did this answer your question?