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

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

It’s one of the most dreaded WordPress issues:
“Error establishing a database connection.”
If you’re seeing this message, your website is offline, and potential customers are bouncing.

But don’t panic. In this guide, we’ll walk you through the exact steps to troubleshoot and fix the issue.

What Causes This Error?

WordPress relies on a MySQL database to store all your content. If WordPress can’t connect to that database, you get this error. Common causes include:

  • Incorrect database credentials (username, password, database name)
  • Corrupted WordPress files or database
  • Server issues (like MySQL not running)
  • Too much traffic is crashing the database

Step-by-Step Fixes

1. Check wp-config.php for Database Credentials

Your database name, username, password, and host must be correct.

Go to your WordPress root folder and open the wp-config.php file. Look for:

define('DB_NAME', 'your_database_name');
define('DB_USER', 'your_database_user');
define('DB_PASSWORD', 'your_password');
define('DB_HOST', 'localhost');

🔗 Need expert help? Get a Free Database Fix Review

2. Test the Database Connection Manually

Create a new PHP file (e.g., testdb.php) in your root folder and paste:

<?php
$test = mysqli_connect('localhost', 'db_user', 'db_password', 'db_name');
if (!$test) {
    die('Database connection failed: ' . mysqli_connect_error());
}
echo 'Database connected successfully!';
?>

Run it in your browser. If it fails, the issue is likely with your database host or login credentials.

3. Repair the WordPress Database

If you suspect database corruption, add the following line to your wp-config.php:

define('WP_ALLOW_REPAIR', true);

Then visit:

https://yourdomain.com/wp-admin/maint/repair.php

Click Repair Database.
Don’t forget to remove the line from wp-config afterward.

4. Check Your Web Hosting Server

The issue might be on the server side:

  • MySQL is down or overloaded
  • The server crashed due to a traffic spike
  • Hosting limits exceeded

5. Restore a Working Backup

If all else fails, restore a previous backup from your backup plugin or hosting panel.

No backup?
Let us help you set up automated backups so you’re never stuck again.

Bonus: Prevent This Error in the Future

  • Use managed WordPress hosting
  • Install uptime monitoring
  • Keep plugins & themes updated
  • Schedule regular database optimization

Check our WordPress Care Plans for peace of mind

Need Help Fixing This Error?

We fix WordPress issues daily — often within 1 hour.

👉 Get Expert Help Now or Request a Free Site Review

Final Thoughts

The “Error Establishing a Database Connection” in WordPress may look scary, but with the right approach, it’s fixable. Use this guide to troubleshoot smartly — or skip the stress and let us do it for you.

Subscribe to Our Newsletter

Subscribe Form
Scroll to Top