Error Establishing a Database Connection on WordPress: A Step-by-Step Fix

“Error Establishing a Database Connection” stops both the public pages of a WordPress site and its own wp-admin dashboard at the same time, with no partial access and nothing in the interface left to click on for a clue. WordPress’s own developer documentation describes it as a database connection problem that affects the whole site, not a display glitch on a single page. It shows up most often right after a hosting migration or a change of host, the moment the database credentials a site depends on move without every piece following along, which is also when panic sets in because the dashboard that would normally help diagnose the problem is exactly what’s unreachable. If a migration is what brought you here, our guide to All in One WP Migration and Backup is worth a look once the site is back up, so the next move goes through a tool built for it. For now, let’s work through this together, in the order that actually narrows down THE cause instead of trying all nine fixes at once.

Start here: is this your site, or your host’s?

Reload the page once or twice and wait a minute or two before changing anything. A connection error that clears up on its own, or one that only hits some pages while others load fine, points at a brief hiccup on the server rather than a broken configuration. Check whether the front end and wp-admin are both down, since a database that’s genuinely unreachable takes out both at once. Before touching a single file, rule out your host. WordPress’s official list of common WordPress errors names two host-side causes directly: the database “has met its quota and has been shut down,” or the server itself is simply down. Your host’s status page, its support account on social media, or a quick message to its support team will confirm or rule this out in minutes, and if it’s confirmed, there’s nothing to fix on your end: you wait for your host to restore service.

WordPress developer documentation page listing common WordPress errors, including the database connection error
WordPress’s own developer documentation lists a full database quota and a down server among the direct causes of this error.

Check the four database credentials in wp-config.php

Once your host’s status page comes back clean, the error is almost always a mismatch between what your site expects and what the database actually has. Open wp-config.php, at the root of your WordPress install, and look at four constants: DB_NAME, DB_USER, DB_PASSWORD, and DB_HOST. WordPress’s own documentation confirms these four values are exactly what connects your site to its database, nothing else in that file plays a role in this specific error. A single wrong character in any of the four, often left over from a migration or a password reset done on one side but not the other, is enough to trigger this error. If you have any doubt about what the database actually expects, compare or reset these credentials from your host’s admin panel. Depending on your host, that panel might be cPanel, Plesk, hPanel, or another interface entirely, but the database section works the same way everywhere: it shows you the real username, lets you reset the password, and tells you the correct host value.

Still stuck? Isolate the exact MySQL error with a test script

When the four credentials look right but the error persists, a small standalone PHP script, commonly named something like test-bdd.php, settles the question. Dropped at the root of your site with the same four values hardcoded and a few lines calling mysqli_connect(), it returns the exact MySQL error code instead of WordPress’s generic message, telling you plainly whether it’s a wrong password, an unknown database name, or a host it genuinely can’t reach. Delete this script from your server the moment you’re done with it. A file sitting in a public folder with your database password written in plain text is a credential leak the instant anyone requests that URL directly, so this step only stays safe if it’s also the shortest-lived file you create during this whole process.

Before you go further: back up everything

If the credentials and the test script both check out and the error is still there, the problem sits deeper, in the database itself or in WordPress’s own core files. Every fix past this point touches something that can be made worse by a rushed step, so this is the one paragraph in this guide worth reading twice: make a full backup, files and database both, before you repair, replace, or restore anything below. If wp-admin is reachable at all, a backup plugin handles this in a couple of clicks; if it isn’t, your host’s control panel almost always offers a one-off database export and a file manager download as a fallback. Skipping this step to save five minutes is how a fixable connection error turns into actual data loss.

Close-up of a USB drive being plugged into a laptop
A full backup, files and database, before any of the steps below: the one step in this guide worth never skipping.

Let WordPress repair its own database

WordPress ships with a built-in repair tool for exactly this situation, disabled by default. Add one line above the “stop editing” comment in wp-config.php: define( 'WP_ALLOW_REPAIR', true );, then visit /wp-admin/maint/repair.php on your own domain. WordPress’s own documentation on wp-config.php also spells out the trade-off this tool makes: that repair page does not require anyone to be logged in while the constant stays enabled, precisely so it still works when a broken database has locked you out of wp-admin too. That’s also exactly why it has to come out again as soon as the repair finishes: remove the line, or set it back to false, the moment the tool has done its job. phpMyAdmin, if your host gives you access to it, offers the same table-level repair as a manual alternative.

WordPress developer documentation page explaining the wp-config.php file and the WP_ALLOW_REPAIR constant
WordPress’s own documentation on wp-config.php is also where the WP_ALLOW_REPAIR constant, and its no-login caveat, is spelled out.

Database fine, site still broken? Replace WordPress’s core files

A repaired database that still produces the same error points at corrupted core files rather than corrupted data, most often the wp-admin and wp-includes folders. WordPress’s own guide to updating WordPress describes the fix for exactly this case: download a fresh copy of WordPress from wordpress.org, delete the existing wp-admin and wp-includes folders on your server over FTP, then upload the new ones in their place. Your content stays untouched as long as wp-content and wp-config.php are left alone, since that’s where your posts, media, themes, and plugins actually live, not in the two folders you’re replacing. If you’d rather understand what each of those core folders is actually responsible for before touching them, our explainer on what WordPress Core actually is covers that ground.

Check your disk space and hosting limits

A database that’s grown past what your hosting plan allows produces the exact same error as a wrong password, which is why the quota cause named earlier deserves a second look here if nothing else has worked. Check the storage and database size shown in your host’s panel against your plan’s limit. A database that’s merely close to full rather than completely over quota often behaves erratically instead of failing outright, timing out under load or during backups rather than every single time. Clearing out old post revisions, spam comments, and unused plugin tables, or caching more aggressively so the database is queried less often, buys headroom without a plan upgrade; if the site has simply outgrown its current hosting tier, that upgrade is the honest next step rather than another workaround.

Nothing worked? Restore a recent backup

If the error survives every step above, restoring a backup taken before the problem started is the remaining option, and the reason the backup step earlier in this guide matters even if you never expected to need it. UpdraftPlus, one of the most widely used backup plugins for this, lists over 4 million active installations and a 4.8 out of 5 rating on its official WordPress.org page, and its own documentation describes restoring a site by choosing which components to bring back, plugins, themes, the database, and confirming the restore in a couple of clicks. If wp-admin itself is unreachable, restoring from outside WordPress, through your host’s own backup tool or by re-importing a database export in phpMyAdmin, is the fallback. With no usable backup and no fix above working, contacting your host’s support directly, with the exact error message and what you’ve already tried, is the last remaining step.

UpdraftPlus official plugin page on WordPress.org
UpdraftPlus’s official WordPress.org listing: over 4 million active installations, 4.8 out of 5, restore handled in a few clicks.

FAQ

Will I lose my posts and media if I see this error?

Not by itself. In most cases this error means WordPress can’t reach the database right now, not that the database or your media files have been deleted. The real risk to your content comes from rushing the repair or core-file steps without a backup first, not from the error appearing in the first place.

How do I know if the problem is on my host’s side?

Check your host’s own status page and support channels before touching any file. If the database has hit its storage quota or the server itself is down, that’s confirmed there, and no amount of editing wp-config.php on your end will fix it until your host restores service.

Is it safe to leave WP_ALLOW_REPAIR enabled afterward?

No. While that constant is set to true, the database repair page works for anyone who requests the URL, logged in or not. Remove the line, or switch it back to false, as soon as the repair tool has finished its job, the same way a test script with hardcoded credentials should never stay on the server.

What if none of these nine fixes work?

Restoring a recent backup, through a plugin or directly from your host’s own backup tool, covers nearly every remaining case. If that’s not available either, your host’s support team, given the exact error message and the steps already tried, is better placed than any generic guide to look at what’s actually happening on your specific server.

Our take

What actually matters here isn’t memorizing nine fixes, it’s working through them in order: host, then configuration, then database and files, backing up before the third tier and never after. Most cases resolve at the second step, a wrong credential caught in wp-config.php, long before anything resembling a repair tool or a core file replacement becomes necessary. If you’re setting up backups for the first time as a result of this scare, our step-by-step guide to installing a WordPress plugin covers getting UpdraftPlus or a similar tool running before you need it again.

A man relaxing with his hands behind his head, smiling at his laptop
The site is back, the dashboard loads again, and the next backup is already scheduled.

Photo: Greta Hoffman/Pexels, Aleksander Dumała/Pexels, Mikhail Nilov/Pexels.