"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.

mysite.com
SimpleReview extension
⚠ 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.
WordPress 6.8 · PHP 8.2 · DB_HOST: localhost · DB_USER: wp_user
HomeAboutContact
✓ Connection restored · wp-config patched
Latest Posts
Comment×
Fix DB credentials in wp-config.php|
Fix it ✓ Done
waiting for selection…
Detected
ErrorDB connect fail
Filewp-config.php
Fix plan
Update DB_PASSWORD to match cPanel value · ping localhost to confirm MySQL up
Result
mysqli_connect OK. Front-end and wp-admin restored.
✓ PR opened
fix: update DB_PASSWORD in wp-config.php
wp-config.php · 1 line
Click SimpleReview → select the DB error → Fix it → wp-config patched, site back online
Site is offline right now? → SimpleReview reads your 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.php first. Wrong DB_USER, DB_PASSWORD, or DB_HOST causes 60%+ of cases — especially after a password reset or site migration.
  • If credentials are correct, MySQL is down. SSH in and run systemctl status mysql or mariadb. 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 -h for 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 codeMeaningWhere to look
1045Access denied — wrong user or passwordwp-config.php DB_USER / DB_PASSWORD
2002Can't find the socket — MySQL service downsystemctl status mysql
1040Too many connections — host or config limitmax_connections in my.cnf, or host plan
1049Unknown database — wrong DB_NAME or dropped DBcPanel → MySQL Databases
2005Unknown host — wrong DB_HOST valuewp-config.php DB_HOST
145 / 126Corrupted MyISAM tableWP_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:

Special characters break things. If your password contains $, !, #, 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:

Fixes, in order of cost:

  1. 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.
  2. Disable heavy plugins that fire queries on every page load (related-posts plugins, visitor counters, admin-ajax abuse).
  3. Kill bad bots. Add aggressive crawlers to robots.txt or block by user-agent in nginx/.htaccess. AhrefsBot, SemrushBot, and scraper farms can easily exhaust connections.
  4. 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:

  1. Update DB_NAME, DB_USER, DB_PASSWORD, DB_HOST in wp-config.php to the new host's values.
  2. Confirm the SQL dump actually imported — SHOW TABLES FROM my_wp_db; should list wp_posts, wp_options, etc.
  3. 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".
  4. Once connected, run wp search-replace 'https://oldsite.com' 'https://newsite.com' to fix siteurl and post URLs.
Rule of thumb: After a migration, read the new host's database connection doc before editing wp-config. Two minutes reading saves two hours debugging.

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:

  1. Install the extension and open the broken site. The DB-error screen loads — that's fine, SimpleReview doesn't need the site to work.
  2. Click the SimpleReview icon, then click the error text.
  3. Type "Fix DB connection" and click Fix it.
  4. SimpleReview reads wp-config.php over SFTP, compares credentials against your hosting API (when available), tests mysqli_connect, inspects disk + max_connections, and opens a PR with the one-line fix — usually an updated DB_PASSWORD or a corrected DB_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

What does "Error establishing a database connection" mean in WordPress?
It means PHP tried to connect to MySQL using the credentials in 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).
How do I fix the WordPress database connection error?
Work through a 4-step triage: (1) verify 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.
How do I know if my WordPress database is corrupted?
If the front-end shows "Error establishing a database connection" but /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.
What is DB_HOST supposed to be in WordPress?
Depends on the host. Most shared hosts: 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.
Why does this error appear after I migrated my WordPress site?
Because 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.
Can I use WP-CLI to fix a broken database?
Yes. 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.

Related WordPress Fixes

Sources