How to Fix the WordPress "Error Establishing a Database Connection" Error
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.
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.
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.
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.
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.
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.