How to Fix the "Error Establishing a Database Connection"

This is one of the most common and alarming errors for WordPress users. It means your website is "UP" (the server is running), but it can't communicate with your database. Since your database stores all your content—posts, pages, settings, and users—the site can't build the page and crashes.

What Does This Error Mean?

Simply put, your website's files (the PHP) tried to log in to your MySQL database to get the page content, but the login failed. The server is working, but the critical connection between the code and the content is broken.

How to Fix Database Connection Errors

This error almost always comes down to one of four things. Check them in this order.

1. Check Your `wp-config.php` Credentials

This is the #1 cause. Your wp-config.php file (located in the root of your WordPress install) contains the database login credentials. If these are wrong, the connection will fail.

  • Connect to your site via FTP or your host's File Manager and open wp-config.php.
  • You will see these four lines:
    • define( 'DB_NAME', 'your_database_name' );
    • define( 'DB_USER', 'your_database_user' );
    • define( 'DB_PASSWORD', 'your_password' );
    • define( 'DB_HOST', 'localhost' ); (This is usually 'localhost' but not always)
  • Now, go to your hosting control panel (cPanel) and find the "MySQL Databases" section.
  • Carefully check that the database name, the user associated with it, and the host are 100% identical to what's in your wp-config.php file. A single typo will cause the error.
  • Pro Tip: If you're unsure of the password, just reset it in the MySQL Databases section for that user, and then paste the new password into your wp-config.php file.

2. Check if Your Database Server is Down

Sometimes, your web server is running fine, but the separate MySQL server (which hosts your database) has crashed. This is common on cheap shared hosting.

  • How to check: Try to access your `phpMyAdmin` from your cPanel. If you can't, or if it's very slow, the database server is likely down.
  • How to fix: You can't. You must contact your hosting provider's support team immediately and tell them "My MySQL server seems to be down."

3. Repair a Corrupted Database

Your database itself might be corrupted. WordPress has a built-in repair tool for this.

  • How to fix: Open your wp-config.php file again and add this line at the bottom: define('WP_ALLOW_REPAIR', true);
  • Now, visit this URL in your browser: https://yoursite.com/wp-admin/maint/repair.php
  • You will see a page with a button to "Repair and Optimize Database." Click it.
  • CRITICAL: When you are finished, you *must* remove that line of code from your wp-config.php file, as it leaves your database vulnerable.

4. Check with Your Hosting Provider

If none of the above worked, the problem is almost certainly with your host. Your account may have exceeded its resource quotas (like database connections) and been temporarily throttled. Contact your host's support team, tell them you have an "Error Establishing a Database Connection," and let them know you already checked your config file. They will be able to check their server logs and find the root cause.