"Error establishing a database connection" means WordPress can't talk to its database. Without the database there are no posts, pages or settings to show, so the whole site goes down, including wp-admin. The cause is nearly always one of a handful of things, and you can usually tell which one in a few minutes.
Table of Contents
First, work out which kind of failure it is
Try /wp-admin/. If it shows a slightly different message, "One or more database tables are unavailable", the connection works but a table is damaged (see fix 4). If the front end and wp-admin show the same connection error, start with fix 1.
Fix 1: Check the four database lines in wp-config.php
WordPress reads four settings from wp-config.php in your site's root folder:
define( 'DB_NAME', 'your_database_name' );
define( 'DB_USER', 'your_database_user' );
define( 'DB_PASSWORD', 'your_database_password' );
define( 'DB_HOST', 'localhost' );If any of them is wrong, you get this error. It often happens after moving a site, changing a database password, or restoring a backup onto a different server. Compare each value with the database details in your hosting panel (look under Databases or MySQL). Watch for extra spaces and for hosts that use a name other than localhost. Your host's documentation or support page lists the correct database host.
Fix 2: Check whether the database server is running
Open your host's status page, or log in to phpMyAdmin from the hosting panel. If phpMyAdmin can't connect either, the problem is on the server side, not in WordPress. On shared hosting, a heavy traffic spike on the server can briefly overload the database. Wait ten minutes, then contact support with the exact time the error started.
Fix 3: Test the login details outside WordPress
This separates a wrong password from a broken site. Upload a one-off file called dbtest.php to the root folder:
<?php
$link = mysqli_connect( 'localhost', 'your_database_user', 'your_database_password', 'your_database_name' );
echo $link ? 'Connected' : 'Failed: ' . mysqli_connect_error();Open /dbtest.php in a browser. "Connected" means the details are right and the fault is elsewhere. A message such as "Access denied" means the user or password is wrong. Delete the file immediately afterwards, because it contains your credentials.
Fix 4: Repair damaged tables
Add this line to wp-config.php:
define( 'WP_ALLOW_REPAIR', true );Then visit /wp-admin/maint/repair.php and choose Repair Database. Remove the line when it finishes. The repair page needs no login, so leaving the line in place lets anyone run it.
Fix 5: Check the disk and database limits
A full disk or a hit database quota can stop writes and cause connection errors. Check your hosting panel for disk usage and database size. Clearing old backups and spam comments often frees enough room to recover.
Fix 6: Check the database user's permissions
In your hosting panel's MySQL section, confirm that the database user is assigned to the database with all privileges. A user can exist and still be unattached to the database after a restore.
Fix 7: Restore from a backup
If none of this works and a recent backup exists, restore the database from it. Do this last, because restoring also removes any content created after the backup was taken.
When a hack is the cause
Attackers sometimes change database credentials or inject code that makes the site throw this error. If the error appeared with no changes on your side, and wp-config.php looks different from what you remember, read how to know if your WordPress site has been hacked.
Related reading
- What should actually be in a WordPress maintenance retainer
- How to know if your WordPress site has been hacked
Back online? Check what else is fragile
When the site is back, run it through the free audit tool to see outdated plugins, missing backups signals and security gaps. If you want this handled for you, I fix connection errors and set up daily backups and monitoring on a care plan from $12 a month, or you can send me the details and I'll tell you what I'd do first.
Get the free WordPress Security Checklist 2026
25-point checklist PDF - malware detection, hardening guide, login security. Used by 500+ WordPress site owners.
- ✓ 25-point security checklist PDF
- ✓ WordPress malware scan guide
- ✓ Hardening checklist for any WordPress site
No spam. Unsubscribe any time.
You're in!
Check your inbox - the checklist PDF is on its way.
Need help with your WordPress site?
I'm a freelance WordPress developer who fixes exactly this kind of problem.
150+ projects. Clients in UK, US, UAE & Ireland. Fast turnaround.

Rajan Gupta
Freelance WordPress DeveloperI'm Rajan Gupta, a freelance WordPress developer based in India with 150+ projects delivered for agencies and businesses in the UK, US, UAE, and Ireland. I specialise in performance optimisation, Core Web Vitals, custom Sage/ACF builds, and WooCommerce development. I target 90+ mobile PageSpeed where technically achievable, with a before-and-after report.