Error establishing a database connection

Fix 'Database Error: Cannot Connect to MySQL' on WordPress

WordPress can't talk to your MySQL database. Usually a wrong password, crashed DB, or server hiccup. We'll walk through the real fixes.

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)

  1. Check wp-config.php credentials
    Open wp-config.php from 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 into wp-config.php. After you edit, save and reload the site. If still broken, move on.
  2. 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.
  3. Repair corrupted database tables
    Run this from the command line (replace your_db with your database name):
    mysqlcheck -u root -p --auto-repair your_db

    If you don't know the root password, use the database user from wp-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.
  4. Check MySQL socket or host
    If DB_HOST is localhost, try changing it to 127.0.0.1. Some setups have the MySQL socket configured only for the IP. Also, on some managed hosts it's something like mysql.yourhost.com — check your hosting docs.
  5. 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 in wp-config.php is wrong. Reset it in cPanel or via ALTER USER SQL.

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 (or my.ini on Windows) and add or increase:
    max_connections = 500

    Then restart MySQL.
  • Disable plugins manually. Rename the wp-content/plugins folder to wp-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 -h on 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.

Related Errors in Database Errors
0X000013DF Fix 0X000013DF: Corrupt Cluster Database Backup Why Your Stored Procedure Returns NULL Instead of Data pg_restore: out of memory pg_restore Hits Out of Memory: Quick Fixes That Work 0XC00A002A STATUS_CTX_SHADOW_DENIED (0XC00A002A) Remote Session Fix

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.