Skip to content

Troubleshooting guide

WordPress "Error establishing a database connection": fix

WordPress could not reach MySQL or could not log in. Credentials, a stopped database and exhausted connections cover nearly all cases.

3 min read10 sectionsUpdated

On this page

Key points

  • Test the same credentials from the command line.
  • Memory-starved hosts see MySQL killed by the OOM killer.
  • Bots on xmlrpc.php can exhaust connections.
  • DB_HOST often needs to be something other than localhost.

What it means

When WordPress starts up, it connects using the values in wp-config.php. If that fails for any reason, it shows this page. It says nothing about which reason, so you need to test each one.

Common causes

  • Wrong credentials, after a migration or password change.
  • MySQL stopped or crashed, often from low memory.
  • Too many connections during a traffic spike or bot attack.
  • Damaged tables.
  • Wrong DB_HOST, such as a managed-host address.

Diagnose it

Read the values in wp-config.php and test them directly. Avoid leaving passwords in your shell history.

bash
# Values WordPress uses
grep -E "DB_(NAME|USER|HOST)" wp-config.php

# Is MySQL running? Was it killed?
systemctl status mysql --no-pager
journalctl -k --since "2 hours ago" | grep -iE 'oom|killed process'

# Test the login (prompts for the password)
mysql -h 127.0.0.1 -u wp_user -p wp_database -e 'SELECT 1'

# With WP-CLI
wp db check

Fixes

Correct wp-config.php. Start MySQL, and if it was killed, add memory or swap, and reduce its buffer sizes. Repair tables with wp db repair. If connections are exhausted, block abusive traffic by disabling xmlrpc.php and rate-limiting wp-login.php. Add page caching, so most visits never touch the database. Keep a recent database backup before making any repair at all.

If only the admin area fails

A message in wp-admin saying that one or more tables are unavailable points to corruption. Add define('WP_ALLOW_REPAIR', true);, open /wp-admin/maint/repair.php, and remove the line afterwards, since that page requires no login. Take a backup before repairing anything. On small servers, a very common pattern is that MySQL is OOM-killed nightly during backups or traffic bursts: the site recovers when it restarts, and the site owner sees only occasional reports. Kernel logs tell the truth about that.

Catch it automatically

An uptime check with a keyword on the homepage alerts on this within a minute. MySQL monitoring tracks connections and restarts, and WordPress monitoring covers the rest.

Frequently asked questions

Why does it come and go?

MySQL is likely being restarted or running out of connections under load.

What should DB_HOST be?

Usually localhost, unless your host gives you a specific address.

Can this cause data loss?

Normally not. It is a connection failure.

Watch your own site and services with VitalRix

Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.