“Error Establishing a Database Connection” means WordPress tried to reach MySQL and failed. The fix depends on why it failed — and the three most common causes look identical from the outside. This guide works through them in order from fastest to check to most involved.
First: Is It Just the Frontend or Everything?
Try loading yoursite.com/wp-admin separately. Two different outcomes:
- Frontend down, wp-admin loads: The database is reachable but something in the theme or a plugin is throwing the error on public pages. Deactivate plugins and switch to a default theme.
- Both down: WordPress can’t reach the database at all. Work through the steps below.
If the error message is different — “This site can’t be reached” or a connection timeout — that’s a DNS or server issue rather than a database one. The ‘This site can’t be reached’ troubleshooting guide covers that path.
Step 1: Check Your Database Credentials in wp-config.php
Wrong credentials in wp-config.php are the most common cause and the fastest to rule out.
- Connect via FTP or open your host’s File Manager.
- Open
wp-config.phpin your WordPress root directory. - Find these four lines and check each value:
define('DB_NAME', 'your_database_name');
define('DB_USER', 'your_database_username');
define('DB_PASSWORD', 'your_database_password');
define('DB_HOST', 'localhost');
Cross-reference these against your hosting control panel. In cPanel, go to MySQL Databases — the database name and user are listed there. The password was set when the user was created; if you don’t have it, reset it from cPanel and update wp-config.php to match.
DB_HOST is not always ‘localhost’. Some hosts use a socket path or a remote hostname. Check your host’s documentation if localhost isn’t working — this trips up a lot of migrations.
If you have WP-CLI available, you can verify the config values without opening the file:
wp config get DB_NAME
wp config get DB_USER
wp config get DB_HOST
Step 2: Check Whether MySQL Is Actually Running
Correct credentials still fail if the database server is down. On shared hosting, contact your host’s support and ask whether MySQL is running — this is the fastest route. On a VPS or dedicated server where you have SSH:
sudo systemctl status mysql
# or on some systems:
sudo systemctl status mariadb
If it’s stopped:
sudo systemctl start mysql
You can also test the connection directly with a quick PHP script. Create a file called dbtest.php in your WordPress root:
<?php
$conn = mysqli_connect('localhost', 'DB_USER', 'DB_PASSWORD', 'DB_NAME');
if (!$conn) {
die('Connection failed: ' . mysqli_connect_error());
}
echo 'Connected successfully';
mysqli_close($conn);
?>
Load it in a browser. If it fails with the correct credentials, the problem is the MySQL server, not WordPress. Delete dbtest.php after testing — don’t leave database credentials exposed in a public file.
Step 3: Repair a Corrupted Database
If credentials are correct and MySQL is running but you’re still getting the error, database table corruption is likely — especially if the site went down during a crash or forced shutdown.
Option A — WordPress repair tool:
- Add this line to
wp-config.php:
define('WP_ALLOW_REPAIR', true);
- Visit
https://yoursite.com/wp-admin/maint/repair.php - Click Repair and Optimize Database.
- Remove the
WP_ALLOW_REPAIRline fromwp-config.phpimmediately after — leaving it in is a security risk.
Option B — WP-CLI (faster if you have server access):
wp db check
wp db repair
Option C — phpMyAdmin: Log in, select your database, go to the table that shows “in use” or “crashed” status, and run the Repair operation from the table actions menu.
Step 4: Replace Corrupted WordPress Core Files
If corruption extends to WordPress core files themselves (rare, but it happens after a botched update or hack), replacing them brings things back without touching your content or settings.
- Download the latest WordPress version from wordpress.org/download.
- Extract the zip and upload the
wp-adminandwp-includesfolders via FTP, overwriting the existing copies.
Two things to leave untouched: the wp-content folder (your themes, plugins, uploads) and wp-config.php. Overwriting either will cause real data loss.
Via WP-CLI this is cleaner:
wp core download --skip-content --force
Step 5: Check Hosting Resource Limits
On shared hosting, MySQL connection limits per account are a real constraint. If your site gets a traffic spike or a runaway plugin opens too many connections, the host’s limit kicks in and all new connections are refused — which looks identical to a database error.
- Check your host’s control panel for MySQL connection usage or error logs.
- Deactivate plugins that run frequent database queries (heavy caching plugins, stats trackers, backup plugins with aggressive schedules).
- If you’re consistently hitting limits, it’s a signal the hosting tier isn’t right for the site’s traffic — not a WordPress problem to solve with config tweaks.
How to Prevent Database Connection Errors
Two habits prevent most of these:
- Regular backups with off-site storage. If the database is corrupted beyond repair, a recent backup is the only real recovery option. UpdraftPlus with remote storage (Google Drive, S3, Dropbox) covers this. More on the full backup workflow in the WordPress maintenance guide.
- Keep your server environment current. Old PHP versions and outdated MySQL have known bugs that cause instability. Keeping PHP up to date also covers most of the security patches that prevent the kind of compromises that corrupt databases in the first place.
Database connection errors after a hack are also more common than most people realize. If your site has been behaving strangely before this error appeared, read through how WordPress sites get compromised — a corrupted database is sometimes the symptom, not the root cause.
If you hit a maintenance mode error during the same period, those are separate issues — the .maintenance file fix handles that one.
Still Getting the Error?
If you’ve worked through all five steps and the site is still down, the problem is almost certainly on the host side — either a MySQL service issue they need to fix, or a corrupted database that needs to be restored from a backup. Get in touch and I can take a look. Database recovery and server-side diagnosis are part of what a WordPress care plan covers — including making sure you have a recent backup to restore from if it comes to that.


