Quick answer for advanced users
Check your wp-config.php database credentials match what's in MySQL. Then run mysqlcheck -u root -p --auto-repair --all-databases from the command line. If that doesn't fix it, restart the MySQL service.
Why this happens
WordPress stores all its content in a MySQL database. When you see the dreaded "Error establishing a database connection" message, it means WordPress tried to connect to that database but couldn't. This could be something simple — you changed your hosting password and forgot to update wp-config.php. Or it could be a server crash where MySQL stopped running. I've seen this happen after a plugin update that corrupted a table, or when the database server runs out of memory and just quits.
The error shows up as a blank white screen or a plain text message. No WordPress admin, no site, nothing. You have to fix it from outside WordPress — usually via FTP, cPanel, or SSH.
Fix steps (try in this order)
- Check wp-config.php credentials
Openwp-config.phpfrom your site root via FTP or file manager. Look for these lines:define('DB_NAME', 'your_database_name');
define('DB_USER', 'your_database_user');
define('DB_PASSWORD', 'your_password');
define('DB_HOST', 'localhost');
Make sure the database name, user, and password match what you set up in your hosting control panel. A common mistake: you changed the password in cPanel but forgot to copy it intowp-config.php. After you edit, save and reload the site. If still broken, move on. - Verify MySQL is running
SSH into your server (or open the terminal in your hosting control panel) and run:sudo systemctl status mysql
or for older systems:sudo service mysql status
If you see "Active: inactive (dead)" or "failed", start it:sudo systemctl start mysql
On shared hosting, you can't start services. Contact support and ask them to restart MySQL. - Repair corrupted database tables
Run this from the command line (replaceyour_dbwith your database name):mysqlcheck -u root -p --auto-repair your_db
If you don't know the root password, use the database user fromwp-config.php:mysqlcheck -u your_db_user -p --auto-repair your_db
This scans every table and fixes corruption. After it finishes, check your site. - Check MySQL socket or host
IfDB_HOSTislocalhost, try changing it to127.0.0.1. Some setups have the MySQL socket configured only for the IP. Also, on some managed hosts it's something likemysql.yourhost.com— check your hosting docs. - Test connection manually
Log into MySQL from the command line:mysql -u your_db_user -p -h localhost
Type the password. If it says "Access denied for user", the password inwp-config.phpis wrong. Reset it in cPanel or viaALTER USERSQL.
Alternative fixes if main steps fail
- Restore from backup. If you have a recent backup of the database (from a plugin like UpdraftPlus or via phpMyAdmin export), restore it. Corruption doesn't always get caught by
mysqlcheck. - Increase MySQL connection limits. If your site gets a lot of traffic, MySQL might hit its max connections. Edit
/etc/mysql/my.cnf(ormy.inion Windows) and add or increase:max_connections = 500
Then restart MySQL. - Disable plugins manually. Rename the
wp-content/pluginsfolder towp-content/plugins_old. If the site loads, a plugin caused the DB crash. Rename it back, then rename plugin folders one by one to find the culprit. - Check for disk space. MySQL needs room to write. Run
df -hon your server. If the disk is 100% full, free up space — delete old backups, logs, or unused themes.
Prevention tips
Set up automated daily database backups. Most hosting panels offer this for free. Also, keep a local copy of wp-config.php with the correct credentials — I've seen people lock themselves out after a host migration because they forgot the password. Finally, update your plugins and themes regularly; old plugins are a top cause of table corruption.