Error Establishing a Database Connection in WordPress: Causes and Fixes
WordPress shows 'Error establishing a database connection.' This guide covers the four real causes: wrong credentials in wp-config.php, a database server that is down or overloaded, a corrupted database, and a changed host, with the steps to check each.
The message Error establishing a database connection means WordPress tried to reach its MySQL or MariaDB database and could not. WordPress stores every post, page, setting, and user in that database, so without a connection it cannot build any page and shows only this message. There are four common causes: the credentials in wp-config.php are wrong, the database server is down or overloaded, the database is corrupted, or the site moved and the connection details no longer match. You can tell them apart with a few quick checks, in the right order.
WordPress is telling you it cannot talk to its database. The question is whether the database is wrong, down, damaged, or simply not where WordPress is looking.
Check whether the admin shows a different message
Start by checking what you see in the dashboard, because the two parts of the site can fail differently. Visit yourdomain.com/wp-admin. If you see Error establishing a database connection there too, the problem is the connection itself, which is the usual case. If instead the admin says One or more database tables are unavailable, the database is reachable but damaged, which points to cause 3 below. If the front end errors but the admin shows a prompt to repair the database, that is also cause 3. Before you change anything, take note of when it started and whether anything changed first, such as a host migration, a password reset on the hosting account, or a plugin update.
Cause 1: wrong credentials in wp-config.php
Cause 1 is wrong credentials in wp-config.php, and it is the most common after a migration or a password change. Open wp-config.php in the site's root folder and find four values: DB_NAME, DB_USER, DB_PASSWORD, and DB_HOST. Compare each with the database details shown in your hosting control panel, where the database, its user, and the user's assigned privileges are listed. A single character difference, a database user that was deleted or never given access to that database, or a password that was changed on the host and not in wp-config.php will all produce this exact error. DB_HOST is usually localhost, but some hosts use a specific hostname or a port, so use whatever your host documents.
Cause 2: the database server is down or overloaded
Cause 2 is the database server being down or overloaded, which has nothing to do with your files. On shared hosting, a traffic spike or a neighbor's heavy usage can make MySQL refuse connections, and the error then comes and goes. Try loading the site again after a few minutes, check your host's status page, and look in the hosting panel for any resource or connection limit warnings. If other sites on the same server show the same error, it is the server. Contact your host, because only they can restart or fix the database service. A site that errors under load, especially during traffic spikes, may also be on a plan that is too small for it.
Cause 3: a corrupted database
Cause 3 is a corrupted database, often after a crash, a full disk, or an interrupted import. WordPress has a built-in repair tool. Add define( 'WP_ALLOW_REPAIR', true ); to wp-config.php, then visit yourdomain.com/wp-admin/maint/repair.php and choose Repair Database. Remove that line from wp-config.php as soon as you are done: the repair page works without being logged in while the setting is on, so leaving it enabled is a security risk. If the repair reports tables it cannot fix, restore the database from a backup, since the backup from before the corruption is the reliable way out.
Cause 4: you moved or changed hosts
Cause 4 is a move or host change. After migrating to a new host, the database name, user, password, or hostname almost always differ from the old server, and wp-config.php still holds the old values. Re-check all four values against the new host's details. Also confirm the database was actually imported into the new server, and that the table prefix in wp-config.php, the line beginning with table_prefix, matches the prefix of the tables in the imported database. A mismatched prefix makes WordPress look for tables that do not exist under that name. If the error appeared with no migration or change at all, and the credentials are correct and the server is up, check for a compromise; see our guide on the WordPress site is hacked warning signs. For other errors that take a site offline, see the 500 internal server error guide.
Get the database error fixed
Frequently asked questions
What causes 'Error establishing a database connection' in WordPress?
+Four things: incorrect database credentials in wp-config.php (name, user, password, or host), a database server that is down or overloaded, a corrupted database, or a site that moved to a new host while wp-config.php still holds the old details. Checking them in that order resolves nearly every case.
How do I fix the database connection error?
+Compare DB_NAME, DB_USER, DB_PASSWORD, and DB_HOST in wp-config.php with the database details in your hosting control panel. If they match, check whether the database server is up and whether your host reports problems. If the admin shows a repair prompt or missing tables, run WordPress's built-in repair tool, then restore from backup if it cannot fix the damage.
Is it safe to use the WordPress database repair tool?
+Yes, as long as you remove the WP_ALLOW_REPAIR line from wp-config.php when finished. While that setting is on, the repair page can be opened by anyone without logging in, so it should only be enabled briefly. Take a database backup before repairing.
Why does the error come and go?
+An intermittent error usually means the database server is overloaded or hitting a connection limit, often on shared hosting during traffic spikes. If it keeps recurring, the site may need a larger hosting plan, caching, or database optimization.
Did I lose my website data?
+Almost certainly not. The error means WordPress cannot reach the data, not that it is deleted. Posts and pages remain in the database. Do not reinstall WordPress or delete the database while troubleshooting, and take a backup before making changes.
More from Insights
Let's keep the conversation going
We're equipped to tackle your challenges head-on. Learn more about how Inspirable can help your organization grow.