"Error Establishing a Database Connection" in WordPress — The Real Fix Guide
A white screen with one sentence — "Error establishing a database connection." — and the whole site is gone: front-end, wp-admin, everything. Ninety percent of the time it's one of four things: wrong credentials in wp-config.php, a MySQL service that crashed, a corrupted wp_options table, or the host hit a resource limit. Here's how to diagnose which one in under 10 minutes, then fix it.
wp-config.php over SFTP, tests the DB credentials, compares them against your host dashboard, and opens a one-line PR — faster than SSHing in and editing by hand.
Key Takeaways
- Check
wp-config.phpfirst. WrongDB_USER,DB_PASSWORD, orDB_HOSTcauses 60%+ of cases — especially after a password reset or site migration. - If credentials are correct, MySQL is down. SSH in and run
systemctl status mysqlormariadb. Restart the service. - Front-end dies but wp-admin shows "one or more tables unavailable" = corrupted table. Add
define('WP_ALLOW_REPAIR', true);and run/wp-admin/maint/repair.php. - Check disk space and max_connections.
df -hfor full disk, host dashboard for connection limits — both kill the DB silently. - WP-CLI shortcut:
wp db check/wp db repair— instant diagnosis without touching wp-config.
What the Error Actually Means
WordPress renders every page by making a mysqli_connect() call to your database. If that call returns false, PHP gets no data, so WP throws the generic error screen:
Error establishing a database connection
This either means that the username and password information in your
wp-config.php file is incorrect or that contact with the database server
at localhost could not be established. This could mean your host's
database server is down.
The WordPress message itself doesn't tell you which of the four failure modes happened. To figure that out you need one debug line in wp-config.php:
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );
@ini_set( 'display_errors', 0 );
Reload the site, then open wp-content/debug.log. The real PHP error appears there — for example:
PHP Warning: mysqli_real_connect(): (HY000/1045):
Access denied for user 'wp_user'@'localhost' (using password: YES)
in /wp-includes/class-wpdb.php on line 1987
PHP Warning: mysqli_real_connect(): (HY000/2002):
No such file or directory in /wp-includes/class-wpdb.php on line 1987
PHP Warning: mysqli_real_connect(): (HY000/1040):
Too many connections in /wp-includes/class-wpdb.php on line 1987
Each error code maps to a different root cause:
| MySQL code | Meaning | Where to look |
|---|---|---|
1045 | Access denied — wrong user or password | wp-config.php DB_USER / DB_PASSWORD |
2002 | Can't find the socket — MySQL service down | systemctl status mysql |
1040 | Too many connections — host or config limit | max_connections in my.cnf, or host plan |
1049 | Unknown database — wrong DB_NAME or dropped DB | cPanel → MySQL Databases |
2005 | Unknown host — wrong DB_HOST value | wp-config.php DB_HOST |
145 / 126 | Corrupted MyISAM table | WP_ALLOW_REPAIR or wp db repair |
Step 1: Verify Credentials in wp-config.php
This is the highest-probability fix. Connect via SFTP or your host's File Manager and open wp-config.php at the site root. You're looking for these four lines:
define( 'DB_NAME', 'my_wp_db' );
define( 'DB_USER', 'wp_user' );
define( 'DB_PASSWORD', 'KhUj9!sQp2z...' );
define( 'DB_HOST', 'localhost' );
Now cross-check each value against your hosting control panel:
- cPanel: MySQL Databases → compare DB name + username. Click "Add User to Database" to reset the password if you don't remember it.
- Managed hosts (SiteGround, Kinsta, WP Engine, Cloudways): Sites → Database → show credentials.
- AWS / DigitalOcean / VPS: check your provisioning script or
mysql -u root -pthenSELECT user, host FROM mysql.user;
$, !, #, or ', make sure it's wrapped in single quotes in wp-config.php — PHP expands variables in double quotes and will silently corrupt your password.
Test the credentials without WordPress
Upload a tiny test file next to wp-config.php called db-test.php:
<?php
$link = mysqli_connect('localhost', 'wp_user', 'YOUR_PASSWORD', 'my_wp_db');
if (!$link) {
echo 'FAIL: ' . mysqli_connect_error();
} else {
echo 'OK — connected to ' . mysqli_get_host_info($link);
}
Visit yoursite.com/db-test.php. If this returns "OK" but WordPress still errors, the problem isn't credentials — skip to Step 3. If this returns FAIL: Access denied, you've confirmed the password is wrong. Delete db-test.php when you're done.
Step 2: Is MySQL Actually Running?
If MySQL itself crashed, no credentials will help. SSH into the server and check:
# Debian / Ubuntu
sudo systemctl status mysql
sudo systemctl restart mysql
# CentOS / RHEL / AlmaLinux
sudo systemctl status mariadb
sudo systemctl restart mariadb
# Check error log for the crash reason
sudo tail -n 100 /var/log/mysql/error.log
Common crash causes: out of memory (OOM killer logged in dmesg), corrupted InnoDB redo log (need innodb_force_recovery = 1 in my.cnf), or a stuck table lock. On shared hosting you can't restart the service — open a support ticket quoting the MySQL error code you found in debug.log.
Check disk space
A full disk silently kills MySQL writes. Run:
df -h
du -sh /var/lib/mysql/*
If any partition is at 100%, free space (clear old logs in /var/log/, old backups, /tmp/) then restart MySQL.
Step 3: Repair Corrupted Tables
If the front-end shows the generic DB error but /wp-admin/ shows a different message — "One or more database tables are unavailable" — that's a corrupted-table signature. Usually wp_options (after a plugin deactivation interruption) or wp_postmeta (after a disk-full write).
Method A: Built-in WordPress repair
Add this line to wp-config.php (anywhere above the "That's all, stop editing" marker):
define( 'WP_ALLOW_REPAIR', true );
Visit https://yoursite.com/wp-admin/maint/repair.php. Click Repair Database (safe) or Repair and Optimize Database (slower, defragments too). Remove that line when done — the repair page is public and doesn't require login.
Method B: WP-CLI (faster, safer)
If you have SSH access and WP-CLI installed:
# Check all tables for corruption
wp db check
# Repair corrupted tables
wp db repair
# Optimize after repair
wp db optimize
# Nuclear option: dump, drop, recreate, import
wp db export backup.sql
wp db reset --yes
wp db import backup.sql
Method C: Direct MySQL
mysql -u root -p my_wp_db
mysql> CHECK TABLE wp_options;
mysql> REPAIR TABLE wp_options;
mysql> REPAIR TABLE wp_postmeta;
Step 4: Host Resource Limits
On shared hosting, DB errors that appear only during traffic spikes usually mean you hit the max_connections limit (often set to 15-30 on cheap plans). Signs:
- Error appears intermittently, not constantly.
debug.logshows MySQL error1040: Too many connections.- Site recovers on its own after 30-60 seconds.
Fixes, in order of cost:
- Install a cache plugin (WP Rocket, LiteSpeed Cache, W3 Total Cache). Static HTML pages don't hit MySQL — this often fixes 90% of the spikes.
- Disable heavy plugins that fire queries on every page load (related-posts plugins, visitor counters, admin-ajax abuse).
- Kill bad bots. Add aggressive crawlers to
robots.txtor block by user-agent in nginx/.htaccess. AhrefsBot, SemrushBot, and scraper farms can easily exhaust connections. - Upgrade the plan. Shared → VPS or managed WordPress host with proper DB tuning.
Step 5: After a Migration
Migrations are the classic cause of this error. Anyone who just moved a site from one host to another and sees "Error establishing a database connection" should:
- Update
DB_NAME,DB_USER,DB_PASSWORD,DB_HOSTinwp-config.phpto the new host's values. - Confirm the SQL dump actually imported —
SHOW TABLES FROM my_wp_db;should listwp_posts,wp_options, etc. - If the new host uses a non-standard
DB_HOST(DreamHost, RDS, Hostinger, some SiteGround setups), use the exact value from the host's knowledge base — not "localhost". - Once connected, run
wp search-replace 'https://oldsite.com' 'https://newsite.com'to fixsiteurland post URLs.
When to Hand It Off to AI
Manual debugging works, but requires SSH, patience, and a tolerance for MySQL error codes. SimpleReview (Chrome extension or WordPress plugin) automates the whole triage:
- Install the extension and open the broken site. The DB-error screen loads — that's fine, SimpleReview doesn't need the site to work.
- Click the SimpleReview icon, then click the error text.
- Type "Fix DB connection" and click Fix it.
- SimpleReview reads
wp-config.phpover SFTP, compares credentials against your hosting API (when available), testsmysqli_connect, inspects disk + max_connections, and opens a PR with the one-line fix — usually an updatedDB_PASSWORDor a correctedDB_HOST.
Useful when you're on mobile, in a client meeting, or just don't feel like SSHing into a production server at 2 AM.
Fix the DB Connection Without Touching the Terminal
Click on the error — SimpleReview inspects your wp-config, tests MySQL, and opens a PR with the one-line fix.
Install SimpleReview Chrome Extension →Prefer a WordPress plugin? Download SimpleReview for WordPress → — installs in 30 seconds, no Chrome required.
Frequently Asked Questions
wp-config.php and the connection failed. Four possible causes: wrong DB_USER/DB_PASSWORD/DB_HOST values, MySQL service down, corrupted tables, or host resource limits (full disk, max_connections).DB_NAME/DB_USER/DB_PASSWORD/DB_HOST in wp-config.php against your host dashboard, (2) systemctl status mysql and restart if not active, (3) add define('WP_ALLOW_REPAIR', true); and visit /wp-admin/maint/repair.php, (4) check df -h and your host's max_connections setting./wp-admin/ shows "One or more database tables are unavailable", one or more tables are corrupted (usually wp_options). Fix with the WP_ALLOW_REPAIR built-in tool or wp db repair via WP-CLI.localhost or 127.0.0.1. DreamHost: mysql.yourdomain.com. AWS RDS: the instance endpoint. SiteGround sometimes uses a socket path. Always check your host's specific docs.wp-config.php still has the old host's DB credentials. Update all four DB_* constants to match the new server, confirm the SQL dump imported, then run wp search-replace for the site URL change.wp db check reports corrupted tables, wp db repair fixes MyISAM corruption, and wp db optimize defragments. For total reset: wp db export backup.sql, wp db reset --yes, wp db import backup.sql.