ZeroToWP
errorsby Marvin

How to Fix "Error Establishing a Database Connection" in WordPress

Share this article

There is something uniquely panic-inducing about opening your WordPress site and seeing nothing but the words "Error establishing a database connection" on a blank page. No header, no content, no sidebar — just that one devastating line. I have seen this error dozens of times on client sites, and the first time it happened to me, I thought my entire site was gone.

It was not. And yours is not gone either. This error simply means WordPress cannot talk to the MySQL database where all your content lives. The content is still there — WordPress just cannot reach it. Let me show you how to fix it.

WordPress requirements page showing database server requirements

What Causes This Error?

WordPress needs a database to store everything — posts, pages, comments, settings, user accounts. Every time someone visits your site, WordPress connects to MySQL, pulls the data, and builds the page. When that connection fails, WordPress has nothing to display.

The four most common causes are:

  1. Wrong database credentials in wp-config.php — This is the cause about 80% of the time. Someone changed a password, the host migrated the database, or the credentials were entered incorrectly.
  2. Corrupted database — Tables can become corrupted after a server crash, a failed update, or a hosting issue.
  3. Database server is down — The MySQL server on your host is overloaded or temporarily offline.
  4. Corrupted WordPress files — In rare cases, core WordPress files that handle the database connection are corrupted.

Step 1: Check Your wp-config.php Credentials

This is the fix 8 out of 10 times. Open your wp-config.php file (in the root of your WordPress installation) via FTP or your hosting file manager. Look for these four lines:

define('DB_NAME', 'your_database_name');
define('DB_USER', 'your_database_username');
define('DB_PASSWORD', 'your_database_password');
define('DB_HOST', 'localhost');

Each of these values must match exactly what your hosting account has configured. Here is how to verify them:

  1. Log in to your hosting control panel (cPanel, Plesk, etc.).
  2. Find the MySQL Databases section.
  3. Check that the database name listed in DB_NAME exists.
  4. Check that the database user listed in DB_USER exists and is assigned to that database.
  5. If you are unsure about the password, you can reset it in the control panel and update DB_PASSWORD in wp-config.php.

Common gotcha: DB_HOST. Most hosts use localhost, but some (like certain GoDaddy plans) use a specific hostname like dbserver123.hosting.com. Check your host's documentation or ask their support team if you are unsure.

After updating the credentials, save the file and reload your site. If it works, you are done.

Step 2: Test the Database Connection Manually

If you are not sure whether the credentials are correct, you can test the connection with a simple PHP script. Create a file called testdb.php in your WordPress root directory with this content:

<?php
$link = mysqli_connect('DB_HOST_VALUE', 'DB_USER_VALUE', 'DB_PASSWORD_VALUE', 'DB_NAME_VALUE');
if (!$link) {
    die('Connection failed: ' . mysqli_connect_error());
} else {
    echo 'Connected successfully!';
}
mysqli_close($link);
?>

Replace the placeholder values with your actual database credentials from wp-config.php. Upload the file and visit yourdomain.com/testdb.php in your browser.

  • If you see "Connected successfully!" — the credentials are fine, and the issue is elsewhere.
  • If you see "Connection failed" — the credentials are wrong, or the database server is down.

Important: Delete testdb.php immediately after testing. Leaving it on your server is a security risk.

Step 3: Repair the Database

If your credentials are correct but the error persists, your database might be corrupted. WordPress has a built-in repair tool. Add this line to wp-config.php:

define('WP_ALLOW_REPAIR', true);

Then visit: https://yourdomain.com/wp-admin/maint/repair.php

You will see two options:

  • Repair Database — Fixes corrupted tables.
  • Repair and Optimize Database — Fixes corrupted tables and optimizes them for better performance. This takes longer but is more thorough.

Click "Repair and Optimize Database" and wait for it to complete. You will see a status report for each table.

Important: Remove the WP_ALLOW_REPAIR line from wp-config.php after you are done. This page does not require authentication, so leaving it enabled is a security risk.

Step 4: Repair via phpMyAdmin

If the WordPress repair tool does not work (or you cannot access it), you can repair database tables directly in phpMyAdmin:

  1. Log in to your hosting control panel and open phpMyAdmin.
  2. Select your WordPress database from the left sidebar.
  3. Click Check All to select all tables.
  4. From the "With selected" dropdown, choose Repair table.

phpMyAdmin will attempt to repair each table and show you the results. Look for any tables marked as "corrupted" — those were the problem.

While you are in phpMyAdmin, you can also check that the wp_options table contains the correct siteurl and home values. Browse the wp_options table and look for these two rows — they should contain your site's URL.

Step 5: Check if the Database Server Is Down

If your credentials are correct and the database is not corrupted, the MySQL server itself might be down or overloaded. This is especially common on shared hosting during traffic spikes.

To check:

  • Try accessing phpMyAdmin — If phpMyAdmin also fails to connect, the database server is likely down.
  • Check your host's status page — Most hosting providers have a status page showing known issues.
  • Contact your host — Ask them if the MySQL server is experiencing issues.

If the database server is down, there is nothing you can do except wait for your host to fix it. If this happens frequently, it is a sign that your host is overselling their servers. Consider moving to a more reliable provider — see my hosting comparison guide for recommendations.

Step 6: Check for Corrupted WordPress Files

In rare cases, the files that handle the database connection (wp-includes/wp-db.php or related files) can become corrupted. To fix this:

  1. Download a fresh copy of WordPress from wordpress.org.
  2. Extract the ZIP file on your computer.
  3. Upload the wp-admin and wp-includes folders to your server via FTP, overwriting the existing ones.
  4. Do NOT overwrite wp-content — that folder contains your themes, plugins, and uploads.
  5. Do NOT overwrite wp-config.php — that file contains your database credentials and settings.

Step 7: Check if Only the Admin Area Is Affected

Sometimes the front-end of your site works fine, but the admin area (/wp-admin/) shows the database connection error. This usually means your database is functional but certain tables (specifically wp_options or wp_users) are corrupted.

Use the database repair methods from Steps 3 and 4 above, paying special attention to the wp_options and wp_usermeta tables.

Preventing This Error in the Future

Once your site is back online, take these preventive steps:

  • Set up automated backups — Both files and database. My WordPress backup guide shows you how.
  • Keep your database optimized — Use a plugin or cron job to clean up post revisions, spam comments, and transient data regularly. See my database optimization guide for details.
  • Never change database credentials without updating wp-config.php — This sounds obvious, but it is the most common cause of this error.
  • Choose reliable hosting — Database server downtime is a hosting problem. Quality hosts have redundant database servers with failover. If yours does not, it might be time to migrate to a better host.
  • Keep WordPress and plugins updated — Failed updates can corrupt database tables.

The "Error establishing a database connection" message is scary, but the fix is almost always straightforward. In most cases, it takes less than 10 minutes. Your content is safe — you just need to reconnect WordPress to it.

For more troubleshooting help, see my complete guide to common WordPress errors.

M

Written by Marvin

Our team tests and reviews WordPress products to help beginners make confident choices.

Learn more about our team →

You might also like

Leave A Reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.