How to Fix the “Error Establishing a Database Connection” in WordPress (Step-by-Step Guide)

Hesam Seyedi ● February 27, 2025

Introduction

You open your website, expecting business as usual—and then, bam. Instead of your homepage, you’re hit with a cold, lifeless message:

“Error Establishing a Database Connection.”

No warning. No explanation. Just a broken site staring back at you.

Why This Needs to Be Fixed ASAP

Right now, your website is down.

Visitors can’t see your content, customers can’t buy from you, and if you rely on your site for business, every second of downtime could mean lost revenue.

But it gets worse—Google notices. If your site stays broken for too long, search engines might start ranking it lower.

That means even after you fix it, you could lose traffic and visibility for weeks.

The Good News? You Can Fix It—Fast

This error looks bad, but it’s usually an easy fix

In most cases, it comes down to a simple connection issue between WordPress and your database.

We’ll walk you through step-by-step solutions, starting with the easiest fixes (like checking your database login details) and moving on to more advanced troubleshooting if needed.

And don’t worry—you don’t need to be a developer to follow along. We’ll keep things straightforward, with clear instructions for beginners and pro tips for advanced users.

By the time you’re done, your site will be back online, and you’ll know exactly what to do if this ever happens again.

Quick Troubleshooting Steps (Summary)

Before we dive into the nitty-gritty details, here’s a quick rundown of the most effective fixes.

These steps tackle the most common reasons for the “Error Establishing a Database Connection” so you can try the easiest solutions first.

If one of these works, you’re golden. If not, no stress—we’ll go deeper into each fix right after this.

  1. Check your database credentials. Open your wp-config.php file and confirm that the database name, username, password, and host are correct. Incorrect credentials are the most common reason for this error.
  2. Verify that the database server is running. Most hosting providers use localhost as the database host, but some require a different setting. If you’re on a VPS or dedicated server, make sure MySQL is running and not overloaded.
  3. Repair the database. If your database has corrupted tables, WordPress’s built-in repair tool or phpMyAdmin can fix the issue quickly.
  4. Reinstall core WordPress files. If credentials and the database check out, corrupted WordPress files might be the cause. Re-uploading fresh WordPress files (excluding wp-content and wp-config.php) can resolve this.
  5. Update the database URL. If you recently moved your site, WordPress might still be looking for the old URL. Updating the database URL via phpMyAdmin can fix this.
  6. Restart MySQL or contact your host. If you’re on a VPS or dedicated server, restarting MySQL might resolve a temporary issue. If all else fails, contact your hosting provider—they may need to investigate or restore a backup.

With this summary in mind, let’s go step by step through each fix to get your site back up and running.

1. Check and Update Database Credentials in wp-config.php

Step 1: Verify Database Credentials in wp-config.php

To check and update your database credentials: 

1. Open your wp-config.php file, located in your WordPress root folder.

2. Look for the following lines:

define('DB_NAME', 'your_database_name');
define('DB_USER', 'your_database_user');
define('DB_PASSWORD', 'your_database_password');
define('DB_HOST', 'your_database_host'); // Often 'localhost'

3. Compare these values with the actual database credentials in your hosting provider’s settings.

Step 2: Find the Correct Database Credentials

For cPanel hosting (e.g., Bluehost, SiteGround, HostGator):

  • Log into cPanel and go to MySQL Databases to find your database name and user.
  • Use phpMyAdmin to verify that the database exists.
  • Update wp-config.php with the correct DB_NAME, DB_USER, and DB_PASSWORD values.

For Managed WordPress Hosts (e.g., Kinsta, WP Engine):

  • Managed hosts often use custom database hosts instead of localhost.
  • Find your database credentials in your host’s dashboard (e.g., WP Engine’s Environment Settings or Kinsta’s Database Section).
  • Update DB_HOST in wp-config.php if needed.

For Local Servers (e.g., XAMPP, MAMP, LocalWP):

  • The default database username is usually 'root' and the password is either blank or 'root'.
  • Ensure that MySQL is running in your local server control panel.

After updating wp-config.php, save the file and reload your site. If the issue was incorrect credentials, your site should now connect successfully.

Step 3: Test Your Database Connection

If you’re unsure whether your credentials are correct, create a test connection script:

1. Create a new file in your WordPress root directory.
2. Name it test-db.php,
3. Copy and paste the following code into the file:

<?php
// Load WordPress configuration if available
require_once('wp-config.php');

// Attempt to connect to the database
$link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

if (!$link) {
    die('Could not connect: ' . mysqli_connect_error());
}

// Set character set to utf8mb4 for better compatibility
mysqli_set_charset($link, 'utf8mb4');

echo 'Connected successfully';

// Close the connection
mysqli_close($link);
?>

4. Upload this file to your website.
5. Visit http://yoursite.com/test-db.php in your browser.
6. If you see “Connected successfully”, your credentials are correct, and the issue might be elsewhere.
7. If you see a connection error, double-check your wp-config.php file to ensure the database credentials are correct.

It is important to note that after testing, delete the test-db.php file from your server to prevent unauthorized access to sensitive database information.

Step 4: Check if the Database Server is Running

Even if your credentials are correct, WordPress won’t connect if the database server is down.

  • If you’re using shared hosting, contact your hosting provider to check if MySQL is running.
  • If you’re on a VPS or dedicated server, restart MySQL using:
sudo systemctl restart mysql
OR
service mysql restart

Step 5: Repair a Corrupt Database

If your database is corrupted, WordPress might not connect properly.

Try running a database repair:

1. Enable WordPress database repair mode by adding this line to wp-config.php:
service mysql restart

2. Visit: http://yoursite.com/wp-admin/maint/repair.php
3. Click Repair Database.

Once done, remove the define('WP_ALLOW_REPAIR', true); line from wp-config.php.  

Final Steps

If none of these steps work, consider restoring your database from a backup or contacting your hosting provider for further support.

2. Check Your Database Host and Server Status

So, you’ve double-checked your database name and password, and they’re correct. But your site is still throwing the “Error Establishing a Database Connection” message.

Why is that still happening?

The issue might be with your database host or server status—essentially, where WordPress is trying to connect.

If it’s looking in the wrong place or if the database server isn’t running, the connection will fail.

What’s the Database Host, and Why Does It Matter?

Every WordPress site needs to know where to find its database.

That location is defined in your wp-config.php file under:

define('DB_HOST', 'your_database_host');

In most cases, this is set to “localhost”, meaning the database is on the same server as your WordPress site.

However, not all hosting providers work this way. Some use separate database servers with custom hostnames like:

  • db123.hostingprovider.com
  • A private IP address

If WordPress is using the wrong host, it won’t be able to connect—no matter how correct your username and password are.

How to Check and Fix Your Database Host

1. Verify Your Database Host in wp-config.php

Open your wp-config.php file (located in your WordPress root folder).

Look for the DB_HOST line:

define('DB_HOST', 'localhost');
If you’re on shared hosting or a VPS, and your provider hasn’t told you otherwise, “localhost” is probably correct.
2. If You’re on Managed WordPress Hosting (Like WP Engine, Kinsta, or Cloudways)
  • Some managed WordPress hosts use custom database hosts instead of “localhost”.
  • Log into your hosting control panel or check their documentation for the correct database host.
  • If the host is different, update DB_HOST in your wp-config.php file.

How to Check If Your Database Server Is Running

Even if your DB_HOST is correct, WordPress still won’t connect if the database server (MySQL) is down.

Here’s how to check:

For Shared Hosting (cPanel, Bluehost, SiteGround, etc.)
Log into cPanel and open phpMyAdmin.

  • If phpMyAdmin opens normally and you can browse your database, MySQL is running fine.
  • If phpMyAdmin fails to connect or times out, the database server may be down. Contact your hosting provider’s support team.
  • Check if other sites on the same hosting account are also experiencing database issues.
    • If multiple sites are affected, it’s likely a server-wide MySQL issue.

For VPS or Dedicated Servers

If you have root access, you can manually check MySQL’s status:

  1. Run this command in your server’s terminal:
sudo systemctl status mysql
  • If MySQL is inactive or failed, restart it:
sudo systemctl restart mysql

2. Try Rebooting the Server

  • A simple server restart can fix temporary glitches causing database connection failures.

If You Suspect High Traffic or Server Overload

  • On shared hosting, your database may temporarily become unavailable due to too many concurrent connections.
  • If this happens, contact your hosting provider—they can check if the server is overloaded or if your site has hit its resource limits.

Next Steps

If you’ve confirmed that DB_HOST is correct and MySQL is running, but the error persists, the database itself might be corrupt.

3. Repair the WordPress Database

A corrupted database can also cause the “Error Establishing a Database Connection” message. 

Sometimes, WordPress will give a more specific error inside the /wp-admin area, such as:
“One or more database tables are unavailable… the database may need to be repaired.”

However, on the frontend, you may still see the same general connection error.

Database corruption can happen for several reasons, including failed updates, crashed tables, or even malicious activity. Running a database repair can fix broken tables and restore normal site functionality.

How to Repair the Database

WordPress has a built-in repair mode that allows you to fix database issues quickly.

1. Enable Repair Mode

  • Open your wp-config.php file (located in your WordPress root folder).
  • Add the following line just before the comment: “That’s all, stop editing! Happy blogging.”
define('WP_ALLOW_REPAIR', true);
  • Save the file.

This enables a special database repair page that does not require you to log in.

2. Run the Repair

This enables a special database repair page that does not require you to log in.

http://YOURSITE.com/wp-admin/maint/repair.php
(Replace “YOURSITE.com” with your actual domain.)
  • You will see two options:
    • Repair Database – Fixes issues quickly.
    • Repair and Optimize Database – Fixes issues and improves performance, but takes longer.
  • Click Repair Database to begin the process.
WordPress will attempt to fix any crashed or corrupted tables. Once completed, you’ll see a success message or details about any remaining issues.

3. Disable Repair Mode (Important!)

  • After the repair, immediately remove the repair mode line from wp-config.php:
define('WP_ALLOW_REPAIR', true);
  • If left enabled, anyone can access the repair page without logging in, which is a security risk.

Alternative Methods to Repair the Database

If you prefer not to edit wp-config.php or need another approach, you can repair the database using your hosting tools.

1. Repair the Database in cPanel

  • Log into your hosting cPanel and open MySQL Databases.
  • Scroll to the Modify Databases section.
  • Select your WordPress database from the Repair
  • Database dropdown menu.
  • Click Repair Database to start the process.

This runs MySQL’s REPAIR TABLE commands on your database. If successful, you will see a confirmation message.

2. Repair the Database Using phpMyAdmin

  • Log into phpMyAdmin from your hosting control panel.
  • Select your WordPress database from the left sidebar.
  • Check all tables.
  • From the dropdown menu at the bottom, choose Repair table.

phpMyAdmin will attempt to fix any corrupt tables automatically.

3. Repair the Database Using WP-CLI (For Advanced Users)
If you have WP-CLI installed on your server, you can repair the database using a command line approach:

  • Open your server terminal and navigate to your WordPress root directory.
  • Run the following command:
wp db repair

WP-CLI will attempt to fix any database errors and provide a status update.

What to Do Next

After repairing the database, try loading your site again. If the issue is resolved, the error was likely caused by corrupt database tables.

However, if the error persists, the problem may be with WordPress core files or server settings. Proceed to the next step: Fixing Corrupted WordPress Files.

4. Fix Corrupted WordPress Files

If you’ve ruled out database issues and incorrect login credentials, the problem might be with WordPress itself.

Corrupted core files can break the connection between WordPress and your database, leading to the “Error Establishing a Database Connection” message.

This can happen due to a failed update, malware, or an incomplete file transfer. Even a single missing or damaged file—such as wp-config.php—can prevent WordPress from functioning properly.

Fortunately, replacing WordPress core files without touching your content can resolve the issue.

How to Restore WordPress Core Files

1: Back Up Your Site
Before making any changes, create a full backup of your site, including both files and the database.

If anything goes wrong, you’ll be able to restore your site quickly. You can use backup plugins like:

  • UpdraftPlus
  • BlogVault
  • Duplicator

If your hosting provider offers automatic backups, verify that you have a recent one available.

2. Download a Fresh Copy of WordPress

  1. Visit WordPress.org and download the latest version.
  2. Extract the ZIP file on your computer.
  3. Open the extracted folder, and you’ll see core directories like:
    • wp-admin
    • wp-includes
    • Some essential files like index.php and wp-config-sample.php
At this point, do not upload everything yet—some parts of your site should be preserved.

Step 3: Prepare the Files for Upload
To prevent overwriting your content:

  • Delete the wp-content folder from the new WordPress files.
    • This folder contains your themes, plugins, and uploads—deleting it from the new copy ensures you don’t lose any data.
  • Do not replace your existing wp-config.php file.
    • The one included in the new WordPress package is a sample file and does not contain your database credentials.

Step 4: Upload the Fresh WordPress Files

  1. Connect to your website using FTP (FileZilla) or your hosting provider’s file manager.
  2. Navigate to your WordPress root directory (where wp-admin, wp-includes, and wp-content are located).
  3. Upload all the new WordPress files, except for wp-content.
  4. When prompted, allow the new files to overwrite the old ones.
This process replaces any corrupted or missing core files without affecting your site’s content.

Step 5: Test Your Site
Once the upload is complete, reload your website. If the issue was caused by corrupted core files, WordPress should now be able to connect to the database without any errors.

Alternative Methods (Hosting-Specific Options)
If you’re using managed WordPress hosting (such as WP Engine, Kinsta, or Cloudways), your provider may offer a one-click reinstall option for WordPress core files. Check your hosting dashboard or contact support for details.

For advanced users comfortable with command-line tools, you can use WP-CLI to replace core files without touching content:

wp core download --skip-content --force
This downloads a clean copy of WordPress while preserving your themes, plugins, and uploads.

Next Steps

If your site is back online, the issue was likely caused by a corrupt core file.

However, if the error persists, the root cause might be server-related issues or database corruption beyond basic repairs. In that case, move on to the next troubleshooting steps.

5. Update the WordPress Site URL in the Database (If Applicable)

If you’ve recently moved your site to a new domain or changed its URL, an outdated database entry could be causing login issues, redirect loops, or site access errors.

While this issue doesn’t usually trigger the “Error Establishing a Database Connection” message directly, some users have found that fixing the site URL helped resolve database-related problems after a migration.

Updating the WordPress Site URL is a simple and risk-free step that ensures WordPress is looking for your site in the right place.

How to Update Your Site URL in the Database

Step 1: Open phpMyAdmin
To manually update the site URL, you’ll need access to phpMyAdmin, a database management tool available in most hosting control panels.

  1. Log into your hosting account (cPanel, Plesk, or another control panel).
  2. Look for phpMyAdmin under the Databases section.
  3. Select your WordPress database from the left-hand menu.
    If you’re unsure which database belongs to your site, check your wp-config.php file:
If you’re unsure which database belongs to your site, check your wp-config.php file: php Copy Edit
define('DB_NAME', 'your_database_name');
This value tells you which database to select in phpMyAdmin.

Step 2: Locate the Site URL in the Database

  1. In phpMyAdmin, find and click on the wp_options table.
    • If your database uses a custom prefix, the table name may look like wpabc_options.
  2. In the option_name column, look for two rows:
    • siteurl/code>
    • home/code>
  3.  These store your WordPress Site Address (URL) and Home URL, respectively.

Step 3: Update the Site URL
If these values still contain an old domain name (for example, after a migration), updating them will help prevent connection issues.

Method 1: Edit Directly in phpMyAdmin

  1. Click the Edit button next to siteurl.
  2. Replace the old URL with your new domain (including http:// or https://).
  3. Click Save (or Go).
  4. Repeat the process for the home row.

Method 2: Run an SQL Query (Advanced Users)

If you prefer using SQL commands, you can manually update the site URL:

1. Click on the SQL tab in phpMyAdmin.

2. Enter the following query (replace YOUR_SITE_URL with your actual domain):

UPDATE wp_options 
SET option_value = 'https://YOUR_SITE_URL' 
WHERE option_name = 'siteurl';

3. To update the home URL as well, run:

UPDATE wp_options 
SET option_value = 'https://YOUR_SITE_URL' 
WHERE option_name = 'home';

4. Click Go to execute the query.

Ensure the URL is formatted correctly (e.g., https://example.com instead of just example.com).

Step 4: Test Your Site
After updating the URLs, refresh your site and check if the issue is resolved.

  • If incorrect URLs were causing the problem, WordPress should now be pointing to the correct domain.
  • If the issue persists, the site URL was likely not the root cause, and you should move on to other troubleshooting steps.

Why This Step Matters

Even if your site wasn’t recently moved, verifying the database URL ensures that WordPress isn’t accidentally looking for an outdated domain.

If you’ve ruled this out and the error remains, proceed to server-related troubleshooting to further diagnose the issue.

6. Reboot, Backup, or Contact Your Hosting Provider (Last Resort)

If you’ve checked your credentials, database host, integrity, and core files but still see the error, it might be time to look at other possible causes or get expert help.

Reboot Your Server (For VPS/Dedicated Hosting)

If you’re on a self-managed server, restarting the entire machine can clear stuck processes or resource locks.

This forces all services, including MySQL, to restart fresh. For local setups like XAMPP or MAMP, restarting the services or app can also help.

Restore a Backup

If you have a working backup, restoring it can be the fastest way to fix the issue—especially if a recent change (like a plugin update) caused the problem.

Many hosting providers offer one-click restores. Before rolling back, back up your current state just in case.

Contact Hosting Support

If nothing works, reach out to your host. They can check server logs, MySQL access, and permission issues.

On shared hosting, they might also confirm if other users are affected. Managed WordPress hosts often diagnose these errors quickly.

Hire an Expert (If Needed)

If your site is critical and you’re still stuck, our team at Fidelity Creative can help.

We specialize in all things WordPress, from troubleshooting errors to optimizing performance and security.

Reach out to us for expert WordPress support and get your site back on track.

Conclusion & Next Steps

By following this guide, you should be able to fix the “Error establishing a database connection” in most cases.

We started with the most common issue—incorrect credentials—and worked through database repairs and file fixes, covering the main reasons WordPress might not connect.

We also shared extra tips like updating the site URL and checking with your host to address less common issues.

To make troubleshooting easier, apply one fix at a time and test your site after each step. This helps pinpoint what worked and prevents future issues—like remembering to update wp-config.php if you ever change your database password.

To avoid downtime in the future, keep regular backups and consider using monitoring tools that alert you if your site goes down.

If the error keeps coming back, it could signal a deeper issue, like unstable hosting or a recurring misconfiguration, so it’s worth investigating further with your host or a developer.

If this guide helped, bookmark it for future reference.

Knowing how to fix this error is valuable for any WordPress site owner, and with these steps, you’ll be prepared to handle it quickly.

Now that your site is back up, you can breathe easy—happy blogging!

Need Web & Digital Services?

Transform your online impact with web and SEO services!