How to Fix the “Error Establishing a Database Connection” in WordPress
-
Last Updated On
Your WordPress site is a house, and your database is the storage room holding all your furniture. If the key doesn’t fit or the door is jammed, the house is still there, but nobody can get inside. That’s exactly what happens when you see “Error Establishing a Database Connection.”
This error does not mean your site is gone. In almost every case, your content, posts, and design are still safe in the database; it’s just the connection that’s broken.
In this guide, we’ll not only explain the usual causes of the issue but also show you practical fixes, so you can bring your site back online without any hassle.
Table of Contents
The “Error Establishing a Database Connection” message means that WordPress is unable to connect to its database. Since almost everything on your site is stored in the database, WordPress needs this connection every time someone visits your site.
Here’s what normally happens:
When that connection breaks, WordPress can’t fetch any data. As a result, instead of your site, visitors see a blank screen with the error message like this:

Here’s a simple diagram to help you visualize the process:

It’s worth noting that sometimes the issue isn’t obvious right away; your homepage might still load from cached files, but you’ll usually notice it when you can’t log in to the WordPress admin dashboard.
In short, the error doesn’t mean your site is deleted. Your content is still safe in the database. It simply means WordPress can’t reach it, due to various reasons like incorrect login details, corrupted files, or a temporary server problem.
Now that you know what this error actually means, the next question is obvious: how does it happen?
This problem doesn’t have a single cause; it’s more like a chain where multiple weak links can break the connection.
Sometimes it’s as simple as a wrong database password. Other times, it could be corrupted files, a database that needs repair, or even a sudden spike in traffic that your server can’t handle. Let’s look at the most common causes one by one:
WordPress needs the correct database name, username, password, and host (server) to connect. If even one of these details is wrong in the wp-config.php file, the connection will fail.
This usually happens after changing hosting providers or updating your MySQL password without updating the configuration file.
Tip: If you’ve recently migrated your site, double-check credentials. You can also follow our WordPress migration checklist for smoother transfers.
Your database stores all your content and settings, but it isn’t strong enough to handle issues. A faulty plugin, theme conflict, interrupted update, or even accidental deletion of a table can cause corruption. When that happens, WordPress struggles to fetch information, and that results in this error.
Just like damaged files on your computer can stop a program from running, corrupted WordPress files can break the database connection.
This may happen after a failed update, a botched file transfer, or even a hack. When critical files aren’t working properly, WordPress can’t reach the database.
Sometimes the problem isn’t on your site, it’s on your hosting provider’s side. If the server is overloaded, temporarily down, or struggling with too many requests, your site won’t be able to connect to the database. This is common on shared hosting plans where resources are limited.
A sudden surge in visitors (like after a successful marketing campaign or viral post) can overwhelm your database with too many requests at once. If your hosting plan isn’t optimized for higher traffic, it can lead to temporary downtime and trigger a connection error.
If you’ve noticed performance issues before, you might need to speed up your WordPress website’s performance so it can handle traffic better.
Seeing this error might feel like your site is broken, but most of the time it’s just a small glitch between WordPress and the database.
You can try some quick fixes like checking if the database password is correct, repairing a corrupted table, or restarting your server. With these fixes, you can usually get your site back online in minutes. Let’s go through the most effective solutions, starting from the easiest and moving to the more advanced ones.
One of the most common reasons for the “Error Establishing a Database Connection” in WordPress is incorrect database login details. This usually happens after you move your site to a new host or update server settings.
Here’s how you can fix it:
In the wp-config.php file, look for these lines:
define( ‘DB_NAME’, ‘your_database_name’ );
define( ‘DB_USER’, ‘your_database_username’ );
define( ‘DB_PASSWORD’, ‘your_database_password’ );
define( ‘DB_HOST’, ‘localhost’ );
Now, log in to your hosting panel and go to Database Management. Check your DB name, user, password, and host. Make sure the details match with the wp-config.php.
If anything doesn’t match, update it in wp-config.php. Save the changes and refresh your site. And if you are still facing the issue, reset your database user’s password in the hosting panel and update it in the config file.
Sometimes, the error happens because your WordPress database is corrupted. This can happen due to frequent plugin/theme installs, failed updates, or broken table entries.
Here’s how you can fix it:
define(‘WP_ALLOW_REPAIR’, true);
http://yourdomain.com/wp-admin/maint/repair.php

Note: Once it’s done, remove the repair code from wp-config.php. Otherwise, anyone can access the repair page without logging in.
Here are some alternatives you can try if you don’t prefer to edit files:
wp db repair
Once complete, refresh your site to check if the issue is fixed.
Another common reason for the WordPress database error is corrupted WordPress files. This can happen during a failed update, a bad file transfer, or even after a hacking attempt. Here’s how you can fix it:
If you recently installed a plugin or theme, that might be the cause.
If you can access the dashboard:
If you can’t access your dashboard:
If the issue isn’t with plugins or themes, your core WordPress files may be corrupted. To replace them:
Note: Any custom code you added to .htaccess or wp-config.php may be lost when replacing files. Take a backup before starting this process.
If your database credentials are correct but you still see the error, the issue might be with your database server (MySQL). This usually happens on shared hosting when there’s too much traffic or limited server resources. Here’s how you can check and fix it:
If you’re on shared hosting, a sudden traffic spike might overload the MySQL server. To confirm, try opening another website hosted on the same server.
What to do: Contact your hosting provider via chat or support ticket and ask if your MySQL server is down.
If phpMyAdmin works fine, but your WordPress site still shows the error, test the connection manually:
<?php
$link = mysqli_connect(‘localhost’, ‘username’, ‘password’);
if (!$link) {
die(‘Could not connect: ‘ . mysqli_error());
}
echo ‘Connected successfully’;
mysqli_close($link);
?>
If it says Connected successfully, the database server is fine. If not, the problem is with user permissions.
Sometimes the database user doesn’t have the right privileges to connect. Here’s what to do:
If the issue happens often:
If nothing else works, restoring your site from a backup is often the quickest way to fix the database connection error. This will bring your site back to a stable version before the issue starts.
Here’s what to do:
If your host doesn’t provide automatic backups, you can restore using a backup plugin such as UpdraftPlus. Just log in to your WordPress admin (if accessible), upload your backup file, and restore.
Tip: Always keep regular automated backups of both your files and database. This way, you can recover your site in just a few clicks if problems occur again.
You’ve already tried the most common fixes, but sometimes the “Error Establishing a Database Connection” can still persist. In such cases, a few additional fixes might help you resolve the issue. Let’s go through them one by one.
If nothing else works, creating a fresh database might solve the issue. Don’t worry, it’s easier than you think. Here’s what to do:
Now, update WordPress to use this new database:
Refreshing your site now should connect it to the new database without errors.
Sometimes, a mismatch in your WordPress site URL causes connection issues, especially after moving domains or switching to HTTPS. To fix this:
UPDATE wp_options SET option_value=’YOUR_SITE_URL’ WHERE option_name=’siteurl’;
Yes, the classic “turn it off and on again” trick works for servers too. Rebooting clears glitches and can fix the connection error. Here are some hacks:
reboot
After the reboot, try loading your site again; it usually resolves hidden server issues.
Every WordPress database uses a table prefix, usually wp_ by default. If this prefix in your wp-config.php file doesn’t match the actual database prefix, you’ll see connection errors. Here’s how to check:
$table_prefix = ‘wp_’;
Sometimes, your hosting server runs out of resources, which can block the database connection. Some common issues are low PHP memory and MySQL connection limits.
To increase memory:
define(‘WP_MEMORY_LIMIT’, ’64M’);
This increases the memory limit and may resolve the error. If the issue continues, contact your hosting provider to check MySQL limits or upgrade your plan.
A faulty plugin or theme can sometimes break the database connection. To rule this out:
If it does, the issue is in a plugin or theme. Reactivate them one by one until you find the faulty one.
Your WordPress database user needs the right permissions to access and modify data. If these permissions are missing or revoked, your site won’t connect to the database.
Here’s how to confirm:
Once done, try reloading your site to see if the error is resolved.
If the error still isn’t clear, your server’s error logs can reveal what’s really going wrong. Both PHP and MySQL logs can show valuable details.
Here are the steps to check logs:
These logs often point you straight to the cause, so it can save time.
Pro Tip: Always create a backup before making changes to your database or configuration.
WordPress connects to your database through the wp-config.php file. You’ll need to enter the database name, username, password, and host details correctly for the connection to work.
The wp-config.php file. This file stores your database credentials (DB_NAME, DB_USER, DB_PASSWORD, and DB_HOST) and tells WordPress how to connect to your local database.
You can reset a WordPress database using a plugin like WP Reset, or manually by dropping all database tables via phpMyAdmin and reinstalling WordPress. Always back up before resetting.
Log in to phpMyAdmin, select your database, and use the Import option to upload your .sql backup file. Once imported, your website’s data will be restored.
Fixing the “Error Establishing a Database Connection” isn’t just about getting your site back online; it’s about understanding the root causes so it doesn’t happen again. Each troubleshooting step you take builds your knowledge of how WordPress and databases work together.
One best practice is setting up regular backups and monitoring tools. These can instantly alert you to issues before they become major outages, saving both time and revenue.
And if you want to leave the technical things to experts, our WordPress development team can help with everything from troubleshooting to performance optimization. We’ll keep your site secure, stable, and running smoothly. Reach out to us to get started.