My whole site shows "Error establishing a database connection". Nothing was changed, but it started this morning. wp-admin shows the same.
How do I fix "Error establishing a database connection" in WordPress?
Every post is reviewed first. Usually reviewed within 24 hours.
1 reply
WordPress could not connect to MySQL or MariaDB, or the database is damaged. Check in this order.
1. Is the database server running? On a VPS:
sudo systemctl status mysql # or mariadb
sudo journalctl -u mysql -n 50
A common cause is the process killed by out of memory (check dmesg | grep -i oom). Add swap or lower innodb_buffer_pool_size and restart.
2. Are credentials right? Test the same values from wp-config.php manually:
mysql -h DB_HOST -u DB_USER -p DB_NAME
If this fails with access denied, the password changed or the user lost privileges. Reset: ALTER USER 'user'@'localhost' IDENTIFIED BY 'newpass'; and FLUSH PRIVILEGES;, then update wp-config.php.
3. Is DB_HOST right? localhost uses a Unix socket; 127.0.0.1 uses TCP. Some hosts need a specific hostname or port (host:3307).
4. Disk full? df -h. A full disk stops MySQL from writing and may crash it.
5. Corrupt tables. Add to wp-config.php temporarily:
define( 'WP_ALLOW_REPAIR', true );
Then open /wp-admin/maint/repair.php and run repair. Remove the constant afterwards, since that page needs no login.
6. Too many connections. Traffic spikes or bots can exhaust max_connections. Cache pages and block abusive IPs.
If only wp-admin fails but the front end works, the siteurl in the database or a plugin may be the problem rather than the connection. Always check the MySQL error log first; it usually names the exact reason.