What Does “Error Establishing a Database Connection” Mean?
This error occurs when WordPress is unable to connect to the database that stores all your site’s content and settings. The site files (themes, plugins, etc.) are still there, but WordPress can’t fetch data like posts, pages, and user information because the database connection is broken.
This issue can stem from:
- Incorrect database credentials
- A corrupt database
- Unresponsive database server
- Too many concurrent connections
- Web host issues
Let’s troubleshoot each cause systematically.
1. Check Your wp-config.php File
The wp-config.php
file holds the database connection details. Even a small typo here can break your site.
Steps:
- Connect to your website via FTP or use your hosting file manager.
- Open
wp-config.php
. - Look for these lines:
define('DB_NAME', 'your_database_name');
define('DB_USER', 'your_username');
define('DB_PASSWORD', 'your_password');
define('DB_HOST', 'localhost');
Make sure:
- DB_NAME matches your database name.
- DB_USER and DB_PASSWORD are correct.
- DB_HOST is usually
localhost
, but some hosts use a different hostname.
Double-check with your hosting control panel if you’re unsure.
2. Test Database Connection Manually
To see if the issue is truly with credentials or the server, create a simple test script.
Create a file named dbtest.php
and upload it to your site’s root folder:
<?php
$link = mysqli_connect("localhost", "your_username", "your_password", "your_database_name");
if (!$link) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully!";
?>
Visit yourdomain.com/dbtest.php
. If it fails to connect, your credentials or server are the problem.
3. Repair Your WordPress Database
A corrupt database can also trigger the error.
How to fix:
- Add this line to your
wp-config.php
:
phpCopyEditdefine('WP_ALLOW_REPAIR', true);
- Visit:
yourdomain.com/wp-admin/maint/repair.php
- Choose “Repair Database” or “Repair and Optimize Database”
Once done, remove that line from wp-config.php
to secure your site.
4. Check with Your Web Host
If the above steps check out, your database server might be down or overloaded. Contact your hosting provider and ask:
- If your database server is running.
- If there’s a cap on concurrent database connections.
- If there are any server-side issues causing downtime.
Upgrading to a better hosting plan or switching to a managed WordPress host can prevent this in the future.
5. Restore a Backup (Last Resort)
If nothing works and your site was running fine recently, restore a backup of your database and files. This is why automated backups are critical for every WordPress site.