🎉 Free WordPress fix for our first 50 sites — in exchange for an honest review. Claim a spot →

How to Fix the WordPress "Error Establishing a Database Connection" Error

The short answer "Error establishing a database connection" means WordPress can't talk to the database where your site's content lives — usually because the login details in wp-config.php are wrong, the database server is down or overloaded, or the database itself is corrupted. Your content is almost always still there; the connection between WordPress and the database is the part that broke. It's one of the most common WordPress errors, and in most cases it's fully recoverable.

What you're seeing

  • The entire site (and often /wp-admin too) shows only the white page text "Error establishing a database connection."
  • The message appears suddenly with no plugin or theme change on your end — often right after a host migration, update, or traffic spike.
  • Sometimes the front end loads but the admin shows a slightly different message like "One or more database tables are unavailable. The database may need to be repaired."
  • Refreshing brings the error back intermittently, which usually points to an overloaded or rate-limited database server.
  • Other sites on the same hosting account may be down at the same time.

What causes it

Wrong database credentials in wp-config.php

WordPress reads the database name, user, password, and host from wp-config.php to log in to the database. If any of these changed — after a host migration, a password reset, or a manual edit — the login fails and you get this error. This is the single most common cause.

The database server is down or unreachable

Your database often runs as a separate service (sometimes on a different host than your files). If that MySQL/MariaDB service has crashed, been restarted, or the host value points to the wrong address, WordPress has nothing to connect to. This is common on shared hosting during maintenance or outages.

Exceeded database connection limits

Most hosts cap how many simultaneous database connections an account can open. A traffic spike, a runaway plugin, or a poorly optimized query can max out that limit, so new requests are refused. The error then comes and goes depending on load, which is the tell-tale sign of this cause.

Corrupted database tables

An interrupted update, a server crash, or a disk problem can leave one or more database tables corrupted. WordPress may connect but fail to read the tables it needs, often showing the "database may need to be repaired" variant. WordPress has a built-in repair tool for exactly this situation.

Server resource exhaustion

If the server runs out of memory or disk space, the database process can be killed or refuse new connections. This is common on undersized hosting plans and after a sudden surge of traffic or a backup process filling the disk. Restarting the database or freeing resources usually restores service.

A hacked or compromised site

Malware or an attacker can alter wp-config.php, change database logins, or hammer the database until it falls over. If the settings look correct but were changed without your knowledge, treat the site as compromised. In that case, fixing the connection is only step one — the security cleanup matters more.

How to fix it yourself

These steps are safe to try yourself, but back up your database before any repair so a bad situation can't get worse.

  1. Back up your database first

    Before touching anything, export a copy of the database via your host's phpMyAdmin or a backup tool, and download a copy of wp-config.php. If you can't reach phpMyAdmin because the database is down, at least save wp-config.php. This gives you a safe restore point if a fix goes wrong.

  2. Verify the settings in wp-config.php

    Open wp-config.php and confirm the database name, user, password, and host exactly match what your host shows for the database. Watch for a changed password or a host value that isn't "localhost" on your host. Correcting a single wrong value here resolves a large share of these errors.

  3. Check whether the database server is up

    Log in to your hosting control panel and try to open phpMyAdmin or the database manager. If it won't load or the database service is stopped, the problem is the server, not your site — restart the database if your panel allows it, or open a ticket with your host. If other sites on the account are also down, this is almost certainly the cause.

  4. Repair the database with WP_ALLOW_REPAIR

    If you see the "database may need to be repaired" message, add the line define('WP_ALLOW_REPAIR', true); to wp-config.php, then visit yoursite.com/wp-admin/maint/repair.php and run the repair. Remove that line from wp-config.php immediately afterward, because leaving it in is a security risk. Always have the backup from step one before running this.

  5. Rule out overload and contact your host

    If the error is intermittent, it's likely a connection limit or resource ceiling, which you usually can't fix from WordPress alone. Give your host the exact error text and ask them to check the database service, your connection limit, and server resource usage. They can see logs and restart services that you can't.

Rather not risk it? We'll fix it for you.

If your settings look right and the site is still down, the error keeps returning, or you suspect a hack, stop guessing and let a senior engineer take it. Mend's Emergency Rescue ($299) starts with a full backup, diagnoses the real cause, fixes it, and documents exactly what was wrong and what we changed. It's backed by a money-back guarantee — if we can't fix it, you don't pay.

Backup-first Documented fix Money-back guarantee
$299
Emergency Rescue · flat
Rescue my site →

Frequently asked

Did I lose my content?
Almost certainly not. This error is about the connection to your database, not the data inside it, so your posts, pages, and settings are usually intact and waiting. Once the connection is restored, your content reappears as it was.
Why did this happen suddenly when I didn't change anything?
Most sudden cases trace back to something on the server side — a host migration, a database restart, a traffic spike hitting connection limits, or a maintenance window. You didn't have to do anything for the settings or the database service to change underneath you. That's why checking with your host is often part of the fix.
Is it safe to edit wp-config.php myself?
Yes, as long as you download a copy first so you can restore it if something goes wrong. wp-config.php is a plain text file, and correcting the database settings is a common, recoverable edit. Just change only the values you're certain about and keep your backup handy.
What does WP_ALLOW_REPAIR do, and is it safe?
It temporarily unlocks WordPress's built-in database repair tool at /wp-admin/maint/repair.php, which can fix corrupted tables. It's safe when used briefly with a backup in hand, but you must remove the line from wp-config.php right after, since leaving it active exposes the repair page to anyone. Always export your database before running a repair.
How fast can Mend fix this?
Emergency Rescue ($299) is built for exactly this — a senior WordPress engineer takes the case quickly, backs up the site first, and works the real cause rather than guessing. Most database-connection failures are resolved the same day, and you get a clear write-up of what broke and how we fixed it. If we can't fix it, the money-back guarantee means you don't pay.