“Error Establishing a Database Connection” in WordPress: 6 Fixes in Order
The six causes of WordPress’s “Error establishing a database connection”, in the order to check them: credentials, database server status, corrupted tables, overloaded hosting, a hacked install, and core file damage.
“Error establishing a database connection” is WordPress’s bluntest failure: the white screen means your site’s PHP files are running but cannot reach the MySQL database where every post, page, setting and order lives. Your content is almost never lost – the connection to it is broken. Work through these six causes in order; they run from most to least likely.
Before you start: is it everyone, or just you?
Load the site in a private window and from your phone on mobile data. If wp-admin loads but the front end doesn’t (or vice versa), note that – it points to corrupted tables (fix 3) rather than credentials. If nothing loads anywhere, continue from fix 1.
Fix 1: Check the database credentials in wp-config.php
The most common cause, especially after a migration or a hosting change. Open wp-config.php in your file manager and check four lines:
define('DB_NAME', 'database_name');
define('DB_USER', 'database_user');
define('DB_PASSWORD', 'password');
define('DB_HOST', 'localhost');
Compare each value against the database section of your hosting control panel. A password reset, a migration that copied old values, or a host that uses a database hostname other than localhost will all produce exactly this error. Correct any mismatch and reload.
Fix 2: Check whether the database server is down
If the credentials are right, the MySQL service itself may be stopped or unreachable – common on low-quality hosting under load. Check your host’s status page or control panel for database service health, and raise a ticket if MySQL is down; that fix is on their side. If this happens repeatedly, it is a hosting-quality problem, not a WordPress problem – our guide to the real cost of unreliable hosting covers when to stop tolerating it.
Fix 3: Repair corrupted database tables
If wp-admin shows “One or more database tables are unavailable”, tables are corrupted – often after a server crash mid-write. Add this line temporarily to wp-config.php:
define('WP_ALLOW_REPAIR', true);
Then visit yourdomain.co.uk/wp-admin/maint/repair.php and run “Repair Database”. Remove the line immediately afterwards – the repair page needs no login while it is enabled.
Fix 4: Overloaded hosting resources
On shared hosting, a traffic spike, an aggressive bot crawl, or a runaway plugin can exhaust the database connections your account is allowed. The tell-tale: the error comes and goes. Short term, ask your host what hit the limits; longer term, add caching so fewer requests touch the database at all, and consider whether your plan matches your traffic. A cached page serves without a single database query.
Fix 5: Rule out a hack
Malware sometimes corrupts wp-config.php or the database user itself. If credentials look wrong and nobody changed them, or files show recent modification dates you can’t explain, follow our hacked WordPress site recovery guide before assuming an innocent cause.
Fix 6: Restore from backup
If all else fails – or the repair in fix 3 reports unrecoverable tables – restore the most recent working backup of the database. This is the moment daily automatic backups pay for themselves; on Switchweb, snapshot backups can roll the database back in a few clicks.
FAQs
Is my content gone?
Almost certainly not. The database still exists; WordPress just can’t reach it. Even in the worst case, last night’s backup has everything up to that point.
Why does it happen right after migrating to a new host?
Because wp-config.php still carries the old host’s database name, user or hostname. Fix 1 resolves it in minutes – and a managed migration should never leave you here.
It fixes itself and comes back – why?
Intermittent database errors are resource exhaustion (fix 4) until proven otherwise. Caching and a properly resourced plan cure it; restarting things only postpones it.
The short version
Credentials, server status, table repair, resources, hack check, restore – in that order. If you would rather someone else read the logs at 9pm, our support team does this daily; and if your current host’s database keeps falling over, moving to hosting that doesn’t is a one-off fix. Talk to us.
Looking to get started on Switchweb? Take a look at our website hosting plans or message us about a free trial.