Have you ever opened your WordPress website only to see the message:

“Error Establishing a Database Connection”

This is one of the most common and frustrating WordPress errors. When it occurs, visitors cannot access your website, and you may lose traffic, sales, and search engine rankings if the issue is not fixed quickly.

The good news is that this error is usually fixable. In most cases, it happens because WordPress cannot communicate with your database due to incorrect settings, corrupted files, or server-related issues.

In this guide, you’ll learn what causes this error and how to fix it step by step.

What “Error Establishing a Database Connection” Actually Means

WordPress stores all your content, settings, posts, pages, user accounts, and plugin data in a MySQL database.

When ever someone visits your website, WordPress retrieves information from the database and displays it on the screen.

If WordPress cannot connect to the database, it will display the message:

Error Establishing a Database Connection

This means the website files are working, but WordPress cannot access the information stored in the database.

Common Causes of the Error

Before fixing the problem, it’s important to understand what may have caused it.

The most common causes include:

  • Incorrect database credentials
  • Wrong database host
  • Corrupted database tables
  • Corrupted WordPress core files
  • Database server downtime
  • Website migration issues
  • DNS configuration problems
  • Missing database user permissions
  • Failed WordPress updates
  • Overloaded hosting server

Now let’s look at the solutions.


1. Check Your Database Credentials

The most common cause of this error is incorrect database credentials.

Open your WordPress installation and locate:

wp-config.php

Look for these lines:

define('DB_NAME', 'database_name');
define('DB_USER', 'database_user');
define('DB_PASSWORD', 'database_password');
define('DB_HOST', 'localhost');

Make sure the values match the database information provided by your hosting account.

Even a small typo can prevent WordPress from connecting to the database.


2. Verify the Database Host

Many hosting providers use:

localhost

However, some hosts use a custom database hostname.

If your host uses a different database server address and your DB_HOST value is incorrect, WordPress will fail to connect.

Check your hosting documentation or contact support to confirm the correct database host.


3. Confirm Database User Permissions

Even if your username and password are correct, the database user may not have permission to access the database.

Verify that:

  • The database user exists
  • The user is assigned to the database
  • The user has all required privileges

Without proper permissions, WordPress cannot read or write data.


4. Test the Database Connection Manually

You can create a simple PHP test file to check whether the database credentials work.

If the connection succeeds, your credentials are likely correct.

If it fails, the problem is probably related to:

  • Database username
  • Database password
  • Database host
  • Database server availability

This test helps narrow down the cause quickly.


5. Repair a Corrupted Database

Database corruption is another common cause.

Open your wp-config.php file and add:

define('WP_ALLOW_REPAIR', true);

Save the file and visit:

yourdomain.com/wp-admin/maint/repair.php

Click:

  • Repair Database
  • Repair and Optimize Database

Once the process is complete, remove the code from wp-config.php.

Leaving it enabled creates a security risk.


6. Repair Database Tables Using phpMyAdmin

If the WordPress repair tool does not work, try repairing tables manually.

Steps:

  1. Open phpMyAdmin.
  2. Select your WordPress database.
  3. Check all tables.
  4. Choose Repair Table from the dropdown menu.

This can fix corrupted database tables causing connection problems.


7. Use WP-CLI to Repair the Database

If your hosting account supports SSH access, run:

wp db repair

WP-CLI can repair damaged database tables directly from the command line.

This method is often faster than using phpMyAdmin.


8. Replace Corrupted WordPress Core Files

Sometimes WordPress files become damaged after:

  • Failed updates
  • Malware attacks
  • Incomplete uploads
  • Server crashes

Download a fresh copy of WordPress from WordPress.org.

Keep these files:

  • wp-content
  • wp-config.php

Replace all other core files with clean copies.

This often resolves file-related connection issues.


9. Disable Recently Installed Plugins

A poorly coded plugin can interfere with database operations.

If the error appeared after installing or updating a plugin:

  1. Access your website via File Manager or FTP.
  2. Rename the plugins folder temporarily.

Example:

plugins → plugins_old

Check if the website loads.

If it does, reactivate plugins one at a time to identify the culprit.


10. Check for Theme Issues

Although rare, a corrupted theme can trigger database-related errors.

Switch to a default WordPress theme such as:

  • Twenty Twenty-Four
  • Twenty Twenty-Five

Then test the website again.


11. Verify WordPress Site URLs

If you recently migrated your website, incorrect URLs can create problems.

Open phpMyAdmin and locate:

wp_options

Check these entries:

  • siteurl
  • home

Ensure they contain your current website address.

Incorrect URLs can break WordPress functionality after migration.


12. Check DNS and Domain Configuration

If you recently changed hosting providers, verify that:

  • Nameservers are correct
  • DNS records point to the correct server
  • Domain propagation has completed

Incorrect DNS settings may make it appear that WordPress cannot connect properly.


13. Check Whether the Database Server Is Down

Sometimes the issue is not with WordPress at all.

The database server may be:

  • Overloaded
  • Under maintenance
  • Experiencing hardware problems
  • Temporarily offline

Check your hosting provider’s status page or contact support to confirm.


How to Prevent This Error in the Future

Here are some best practices to reduce the chances of seeing this error again:

  • Keep regular backups.
  • Update WordPress carefully.
  • Use trusted plugins and themes.
  • Monitor server health.
  • Optimize your database regularly.
  • Install a security plugin.
  • Use reliable hosting.
  • Avoid editing critical files without a backup.

Conclusion

The “Error Establishing a Database Connection” message may look scary, but it is usually caused by a handful of common issues.

Start by checking your database credentials and database host. If those are correct, move on to database repair, user permissions, WordPress file integrity, and server availability.

By following the solutions in this guide, you should be able to identify the root cause and get your WordPress website back online quickly