How to Solve “Error Establishing a Database Connection” in WordPress
The “Error Establishing a Database Connection” message appears when WordPress cannot communicate with your MySQL database. Without a working database, your site cannot load posts, pages, or settings. Fortunately, this issue is usually fixable with a few targeted steps.
Common Causes
- Incorrect database credentials in
wp-config.php(name, user, password, host). - Corrupted WordPress database tables.
- Database server downtime or overload from your hosting provider.
- Corrupted core files or incomplete updates.
- Too many simultaneous connections exceeding server limits.
Step-by-Step Fixes
1) Check Database Credentials
Open wp-config.php in the root of your WordPress installation and verify:
define('DB_NAME', 'your_database_name');
define('DB_USER', 'your_database_user');
define('DB_PASSWORD', 'your_database_password');
define('DB_HOST', 'localhost'); // sometimes different, e.g., 127.0.0.1 or a host address
Compare these values with the details provided in your hosting control panel. If they don’t match, update them and save the file.
2) Test Database Connection
Create a temporary file testdb.php in your root folder with:
<?php
$link = mysqli_connect('localhost','your_database_user','your_database_password','your_database_name');
if (!$link) {
die('Could not connect: ' . mysqli_connect_error());
}
echo 'Database connection successful';
?>
Visit https://yourdomain.com/testdb.php. If it fails, the issue is with credentials or the database server.
3) Repair the Database
Add this line to wp-config.php:
define('WP_ALLOW_REPAIR', true);
Then open:
https://yourdomain.com/wp-admin/maint/repair.php
Run “Repair Database.” Once done, remove the line from wp-config.php for security.
4) Check Database Server Status
- Log into your hosting control panel and confirm MySQL is running.
- Ask your host if the database server is experiencing downtime or high load.
- For VPS/Dedicated servers, restart MySQL manually if you have root access.
5) Reduce Resource Usage
If your site is heavy on plugins or traffic, the database may be overwhelmed:
- Enable caching (object cache, page cache).
- Use a hosting plan with higher resources or optimized database servers.
- Check for runaway queries with plugins like Query Monitor.
6) Reupload Core Files
If the problem persists, download a fresh copy of WordPress from wordpress.org and reupload everything except wp-content and wp-config.php. This ensures clean, uncorrupted files.
When to Contact Your Host
If you’ve confirmed credentials, repaired the database, and reuploaded core files but the error remains, contact your hosting provider. They can check:
- Database server health and uptime.
- Limits on concurrent connections.
- Possible disk space or server misconfigurations.
Summary
- Verify and correct database credentials in
wp-config.php. - Run a test connection script.
- Repair the database via WordPress’s built-in tool.
- Confirm MySQL server is running and not overloaded.
- Reupload WordPress core files if necessary.
By following these steps, you can usually restore your WordPress site quickly and prevent future “Error Establishing a Database Connection” issues.
👉 Need more help? Explore our WordPress Troubleshooting Guide for step-by-step solutions to the most common errors.