Quick Answer
Try wp db check in WP-CLI first. If that fails, check your MySQL credentials in wp-config.php — nine times out of ten, the DB_HOST or password got changed during a migration.
Why This Happens
WordPress stores all your posts, users, and settings in a MySQL database. When you hit this error, your PHP code can't open a connection to that database. It's not a plugin conflict — it's a connection issue. I've seen this on everything from cheap shared hosting to high-end VPS setups. The most common triggers: a bad wp-config.php after moving your site, the MySQL server crashed or got restarted, your connection limit got hit, or a corrupted database table.
Don't panic. This isn't a hack (usually) and it's not your content vanishing. Let's fix it step by step.
Fix Steps
- Check if MySQL is running. SSH into your server and run
sudo systemctl status mysql(ormariadbon some hosts). If it's dead,sudo systemctl restart mysql. On shared hosting, check your control panel — look for 'MySQL' or 'Database' status. - Verify your wp-config.php credentials. Open
wp-config.phpfrom your WordPress root. You'll see lines like:define('DB_NAME', 'your_database'); define('DB_USER', 'your_user'); define('DB_PASSWORD', 'your_password'); define('DB_HOST', 'localhost');Double-check these against your hosting dashboard. If you moved hosts, DB_HOST often changes from 'localhost' to something like 'mysql.example.com'. That tripped me up the first time too.
- Test the connection manually. From your terminal, try:
mysql -u your_user -p your_database. Enter the password. If it connects, your credentials are fine. If it says 'Access denied' or 'Can't connect to MySQL server', you've found the problem. - Repair corrupt tables. Add this line to your
wp-config.phpabove the debug settings:define('WP_ALLOW_REPAIR', true);. Then visityoursite.com/wp-admin/maint/repair.php. Click 'Repair Database'. Remove that line after — it's a security risk. - Check for socket vs. TCP conflicts. If DB_HOST is 'localhost', MySQL uses a Unix socket. Sometimes the socket path is wrong. Change DB_HOST from 'localhost' to
127.0.0.1— that forces a TCP connection. Works often on Managed WordPress hosts.
If the Main Fixes Fail
Sometimes the issue is deeper. Here's what to try next:
- Increase MySQL connection limits. Log into MySQL and run
SHOW VARIABLES LIKE 'max_connections';. If it's below 100, you might be hitting a limit. Edit/etc/my.cnfor/etc/mysql/my.cnfand addmax_connections = 150under[mysqld]. Restart MySQL. - Check for plugin corruption. Rename your
wp-content/pluginsfolder towp-content/plugins-old. If the site loads, a plugin was blocking the connection. Reactivate plugins one by one to find the culprit. - Reboot the whole server. Stale MySQL sockets can hang. A full
sudo rebootclears them. Yes, it's blunt. It works. - Restore from a backup. If you've tried everything and still see the error, your
wp-config.phpgot mangled. Get your last known-good config from backup or your host's download area.
Prevention Tips
- Use a staging site for migrations. I always clone to a staging URL first and verify the database connection before making changes live. That single step has saved me hours.
- Monitor MySQL uptime. Tools like
Mytopor simple cron scripts that checkmysqladmin pingcan alert you if the database goes down. On shared hosting, ask your host if they offer uptime monitoring for MySQL. - Keep wp-config.php read-only. Set file permissions to
400or440after you've finalized credentials. Prevents accidental edits. - Use object caching. Redis or Memcached offload repeated database queries. If your database does hiccup, your frontend stays up. It's not a full solution — but it buys you time.
That's the complete fix. You don't need to dig through forums — start with the DB_HOST change if you moved recently, or the MySQL restart if nothing changed. Nine times out of ten, one of those gets you back up.