WordPress 'Error Establishing a Database Connection' Fix

Your WordPress site can't talk to MySQL. Usually a credentials or server glitch. I'll walk you through the common fixes fast.

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

  1. Check if MySQL is running. SSH into your server and run sudo systemctl status mysql (or mariadb on some hosts). If it's dead, sudo systemctl restart mysql. On shared hosting, check your control panel — look for 'MySQL' or 'Database' status.
  2. Verify your wp-config.php credentials. Open wp-config.php from 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.

  3. 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.
  4. Repair corrupt tables. Add this line to your wp-config.php above the debug settings: define('WP_ALLOW_REPAIR', true);. Then visit yoursite.com/wp-admin/maint/repair.php. Click 'Repair Database'. Remove that line after — it's a security risk.
  5. 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.cnf or /etc/mysql/my.cnf and add max_connections = 150 under [mysqld]. Restart MySQL.
  • Check for plugin corruption. Rename your wp-content/plugins folder to wp-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 reboot clears them. Yes, it's blunt. It works.
  • Restore from a backup. If you've tried everything and still see the error, your wp-config.php got 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 Mytop or simple cron scripts that check mysqladmin ping can 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 400 or 440 after 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.

Related Errors in Database Errors
0X00001AB5 ERROR_TRANSACTION_REQUIRED_PROMOTION (0X00001AB5) Fix 18456 SQL Server Error 18456: Login Failed for User 0X0000217D Fix ERROR_DS_OUT_OF_VERSION_STORE (0X0000217D) 0X8004D01E XACT_E_REENLISTTIMEOUT: Fix the 0X8004D01E Timeout Error

Was this solution helpful?

EP
Erropedia Team
Tech Support Editors
The Erropedia editorial team researches and documents real-world tech errors from across Windows, Linux, macOS, networking, databases, cloud platforms, and more. Every solution is reviewed for accuracy and updated as software and systems evolve.