How to Fix “Error Establishing a Database Connection” in WordPress

← Back to all WordPress fixes

Introduction

Few WordPress messages are as alarming as opening your site and seeing a bare page that simply reads “Error establishing a database connection.” Your content, your pages, your dashboard — all gone, replaced by a single line of text. The good news: this error almost always comes down to a handful of specific causes, and most are fixable in a few minutes once you know where to look.

This guide explains what the error means and walks you through fixing it, step by step, from the most common cause to the rarest.

What “Error Establishing a Database Connection” Means

WordPress stores all of your content — posts, pages, settings, users — in a database. On every page load, it has to connect to that database to fetch what it needs. This error means WordPress tried to connect and couldn’t. Either the login details it’s using are wrong, the database server isn’t responding, or the database itself is damaged.

Because nothing loads without that connection, the whole site goes down at once — which looks scary, but the cause is usually narrow and specific.

First, Check Whether wp-admin Shows a Different Message

Before changing anything, visit yoursite.com/wp-admin. If you see a message like “One or more database tables are unavailable. The database may need to be repaired,” your credentials are fine and the problem is a corrupted database — skip to the repair step. If wp-admin shows the same connection error as the front end, work through the steps in order below.

Step 1: Check Your Database Credentials in wp-config.php

This is the single most common cause — especially right after migrating to a new host or changing a database password. Connect to your site via FTP or your host’s File Manager and open wp-config.php in the root folder. Check these four lines:

  • DB_NAME — the database name
  • DB_USER — the database username
  • DB_PASSWORD — the database password
  • DB_HOST — usually localhost, but some hosts use a specific server address

Compare each value against what your hosting control panel shows under MySQL/Databases. A single wrong character — often the password or the host — is enough to trigger the error. Correct any mismatch, save the file, and reload your site.

Step 2: Confirm the DB_HOST Value

If the username and password are definitely correct, DB_HOST is the next suspect. On most setups it’s localhost, but some hosts (and many managed or cloud platforms) require a specific hostname or IP. Your host’s documentation or control panel will list the correct value — if it’s anything other than localhost, update wp-config.php to match.

Step 3: Repair a Corrupted Database

If wp-admin reported that tables need repair, WordPress has a built-in repair tool. Add this line to wp-config.php, just above /* That’s all, stop editing! */:

define( 'WP_ALLOW_REPAIR', true );

Then visit yoursite.com/wp-admin/maint/repair.php and choose Repair Database (or Repair and Optimize). When it finishes, remove that line from wp-config.php — leaving it in place is a security risk, because the repair page has no login protection.

Step 4: Check Whether Your Database Server Is Down

If your credentials are correct and the database isn’t corrupted, the database server itself may be overloaded or offline — common on shared hosting during traffic spikes, or when too many sites share one server. Two quick checks:

  • Open phpMyAdmin from your hosting panel. If it won’t connect either, the issue is server-side — contact your host.
  • Ask your host whether your account has hit a connection or resource limit. A brief support ticket often resolves what looks like a site bug but is really a server condition.

Step 5: Restore From a Backup

If the error appeared right after an update, a migration, or some manual database editing — and the steps above don’t fix it — the cleanest path back is to restore a recent backup from before things broke. This instantly returns your database (and files, if needed) to a known-good state instead of hunting for the exact thing that went wrong.

If the error also blocks your dashboard, you can still restore: Nota Backup & Restore’s Emergency Recovery works independently of wp-admin. And if a plugin or core update triggered the problem, an automatic before-update backup gives you a snapshot from moments before to roll back to.

How to Avoid This Error in the Future

  • Keep recent backups, off-site. A current backup turns “my database is corrupted” into a five-minute restore. See how to back up WordPress to Google Drive.
  • Test risky changes on staging. Database-altering updates are safest tried on a staging copy before they touch live.
  • Back up before every update. Updates are a frequent trigger — an automatic pre-update snapshot means instant rollback.

Frequently Asked Questions

Why did this happen suddenly when I didn’t change anything?
The most common “out of nowhere” cause is the database server being temporarily overloaded on shared hosting, or your host changing/expiring a database password. Check with your host if your credentials look correct.

Will I lose my content?
Usually not — the error is about connecting to your data, not deleting it. Once the connection is restored (or you repair the database), your content comes back. A backup is your guarantee either way.

Is editing wp-config.php safe?
Yes, as long as you change only the specific values and keep a copy of the original first. Download a backup of the file before editing so you can revert if needed.

I fixed the credentials but it’s still broken — now what?
Repair the database (Step 3), then check whether the database server is down (Step 4). If both are fine, restoring a recent backup is the most reliable way back.

Conclusion

“Error establishing a database connection” looks like a disaster, but it’s almost always one of a few fixable causes: wrong credentials, a corrupted database, or an overloaded server. Work through the steps in order and you’ll usually be back online quickly. And the surest safety net of all is a recent backup — with one in place, even the worst-case version of this error is just a quick restore away.

Don’t let a database error become a lost site

Keep automatic, off-site backups with Nota Backup & Restore — and restore in minutes even when wp-admin is down. Start your 14-day free trial — no credit card required.