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
Diagnose it
Read the values in wp-config.php and test them directly. Avoid leaving passwords in your shell history.
# 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 checkFixes
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.
Related reading
- Cloudflare error 521: web server is down: Fix Cloudflare error 521 "Web server is down": the origin refused the connection. Check the web server, firewall rules for Cloudflare IPs and DNS.
- Cloudflare error 522: connection timed out: Fix Cloudflare error 522 "Connection timed out": the origin did not complete the TCP connection in time. Overloaded servers, dropped packets and wrong DNS.
- CreateContainerConfigError in Kubernetes: how to fix it: Fix Kubernetes CreateContainerConfigError: missing Secrets and ConfigMaps, wrong keys and namespaces, with kubectl commands that show exactly what is absent.
- "Your connection is not private": how to fix it: Fix "Your connection is not private" (NET::ERR_CERT_*): expired or mismatched certificates, wrong device clocks, missing intermediates and Wi-Fi portals.
- MySQL "Lock wait timeout exceeded": how to fix it: Fix MySQL error 1205 Lock wait timeout exceeded: find the blocking transaction with the performance schema, shorten transactions and index your updates.
- ERR_SSL_PROTOCOL_ERROR: how to fix it: Fix ERR_SSL_PROTOCOL_ERROR ("This site can't provide a secure connection"): HTTP on port 443, old TLS versions, broken TLS setups, proxies and antivirus.
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.