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.
Access Your Site’s Files: To locate your site's files, use an FTP client or your hosting provider’s file manager.
Open wp-config.php: Find and open the wp-config.php file in the root directory.
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.
Contact Your Hosting Provider: Ask if there are any ongoing server issues.
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.
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);
Run the Repair Script: Navigate to http://yoursite.com/wp-admin/maint/repair.php and follow the instructions.
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.
Access phpMyAdmin: Log into your hosting account and access phpMyAdmin.
Select Your Database: Choose your WordPress database.
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.
Download WordPress: Get the latest version of WordPress from wordpress.org.
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.
Access Backup Files: Locate your backup files.
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.