Need WordPress Malware Removal or Security Help? Get Help Now

How to Fix Error Establishing a Database Connection in WordPress (2026 Updated Guide)

Error establishing a database connection, WordPress database error, fix WordPress database, database connection error WordPress.

Seeing “Error Establishing a Database Connection” on your WordPress website?


Don't panic.

This error means WordPress is unable to connect to the database it needs to load your website's content, settings, users, posts, and pages.

The good news is that the problem is often fixable.

The difficult part is identifying why the connection failed.

A wrong database password, a database server problem, corrupted database tables, damaged WordPress files, hosting issues, or even a hacked website can cause similar symptoms.

In this guide, I'll walk you through 7 practical methods to diagnose and fix the WordPress database connection error, starting with the simplest checks and moving toward more advanced troubleshooting.

WordPress error establishing a database connection

What Does “Error Establishing a Database Connection” Mean?

WordPress uses a database to store most of the information that makes your website work.

This includes things such as
  • Posts and pages
  • WordPress settings
  • User accounts
  • Plugin settings
  • Theme settings
  • Website content
When WordPress cannot establish a connection to that database, it cannot properly load the website.

That's when you may see:

Error Establishing a Database Connection

The error itself doesn't tell you exactly what went wrong.

You need to investigate the cause.

Common Causes of This WordPress Error

Some common causes include:
  • Incorrect database name
  • Incorrect database username
  • Incorrect database password
  • Incorrect database host
  • Database server problems
  • Corrupted database tables
  • Problems after changing `wp-config.php`
  • Corrupted WordPress core files
  • Hosting configuration problems
  • Firewall or server restrictions
  • A plugin or theme causing a database-related problem
  • Malware or a compromised website
So, don't immediately assume that your database is corrupted.

First identify where the connection is failing.

Before You Start: Take a Backup

If you still have access to your files or database, create a backup before making major changes.

Ideally, keep a copy of:
  • WordPress files
  • Database
  • wp-config.php
This gives you a recovery point if a troubleshooting step makes the situation worse.

If the website is already down, don't make random changes just to see what happens.

Start with the least destructive checks first.

Method 1: Check Your wp-config.php File

One of the first things to check is the WordPress database configuration.

The wp-config.php file contains the database connection information WordPress uses.

The important settings include:

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

These values need to match the database details provided by your hosting environment. WordPress's official documentation also identifies DB_NAME, DB_USER, DB_PASSWORD, and DB_HOST as the key database connection settings in wp-config.php.

How to check

  1. Log in to your hosting control panel or connect through FTP/SFTP.
  2. Open your WordPress root directory.
  3. Find `wp-config.php`.
  4. Open the file carefully.
  5. Check the database settings.
  6. Compare them with the database information available in your hosting account.
Pay particular attention to:
  • Database name
  • Database username
  • Database password
  • Database host

What about DB_HOST?

Many WordPress installations use:

define( 'DB_HOST', 'localhost' );

But localhost is not guaranteed to be correct for every hosting environment.

If your hosting provider gives you a different database hostname, use the value they provide.

WordPress wp-config.php file showing database connection settings

Method 2: Check and Repair the WordPress Database

If your database tables are damaged or corrupted, WordPress may have trouble loading the website.

WordPress includes a built-in database repair feature that can be enabled through wp-config.php.

Add:
define( 'WP_ALLOW_REPAIR', true );
Then visit:
https://yourdomain.com/wp-admin/maint/repair.php

WordPress's official developer documentation confirms this repair path and notes that the repair setting should be disabled after the problem is resolved.

You can then use the available repair options.

Important Security Step

After the repair is complete, remove:

define( 'WP_ALLOW_REPAIR', true );

from wp-config.php.

This is important because the repair page is designed to remain accessible without a normal WordPress login when the repair feature is enabled.

Alternative: phpMyAdmin

Depending on your hosting environment, you may also be able to inspect or repair database tables through phpMyAdmin.
If you're not comfortable working directly with database tables, don't make random changes.
WordPress database repair process for fixing database errors

Method 3: Check Whether the Database Server Is Down

Sometimes the problem isn't inside WordPress at all.

The database server itself may be unavailable, or your hosting environment may be experiencing a temporary problem.

Try phpMyAdmin.

If your hosting control panel provides phpMyAdmin:
  1. Open your hosting control panel.
  2. Launch phpMyAdmin.
  3. Check whether you can access the database.
  4. Check whether your WordPress database is visible.
If the database service is unavailable, you may need your hosting provider to investigate the server side.

When should you contact hosting support?

Contact your host if:
  • phpMyAdmin isn't accessible.
  • The database server appears unavailable.
  • Other websites on the same hosting account have similar problems.
  • You recently experienced a hosting outage.
  • Database credentials appear correct, but the connection still fails.
Hosting support may be able to check server-side status and logs that you cannot access yourself.

phpMyAdmin interface used to check a WordPress database

Method 4: Restore wp-config.php From a Backup

Did the problem start immediately after you edited `wp-config.php`?

If yes, the configuration change may be responsible.

If you have a known-good backup of `wp-config.php`, you can compare the current file against the previous version.

Check for changes to:
  • DB_NAME
  • DB_USER
  • DB_PASSWORD
  • DB_HOST
  • Other custom configuration values
If the backup contains the correct configuration, restoring the appropriate version may help.

Be Careful

Don't overwrite the current file blindly.

First make a copy of the current wp-config.php.

That way, you can go back if necessary.

Method 5: Check for Corrupted WordPress Core Files

If the database credentials and database server appear to be working, damaged WordPress core files may be another possibility.

A clean copy of WordPress can help you replace damaged core files.

Basic approach

1. Download WordPress from the official WordPress website.
2. Extract the clean WordPress package.
3. Compare the core files with your existing installation.
4. Replace damaged core files carefully.

The original article recommends replacing the `wp-admin` and `wp-includes` directories while leaving `wp-content` untouched because `wp-content` contains your themes, plugins, and uploads.

If you follow this approach, make a backup first and take care not to overwrite legitimate custom files.

Why avoid touching wp-content?

Your website's:
  • Themes
  • Plugins
  • Uploads
are stored there.

Replacing it without understanding what's inside can remove legitimate website data or customizations.

WordPress fresh file download official website

Method 6: Check PHP Memory and Resource Limits

In some situations, limited server resources can contribute to WordPress failures.

Your existing article includes increasing the WordPress memory limit in `wp-config.php`, for example:

define( 'WP_MEMORY_LIMIT', '256M' );

However, don't treat increasing memory as a universal fix for a database connection error.

If the real problem is
  • Wrong database credentials
  • Database server failure
  • Corrupted tables
  • Hosting problems
Then increasing PHP memory won't solve the underlying issue.

Use this step when resource limitations are actually suspected.

Method 7: Check for Malware or a Hacked Website

A database connection error can sometimes appear alongside a compromised or damaged WordPress installation.

If your website was recently hacked, modified unexpectedly, or contains suspicious files, don't ignore the security side of the problem.

Look for signs such as
  • Unknown PHP files
  • Unexpected administrator accounts
  • Modified WordPress files
  • Suspicious plugin/theme files
  • Strange redirects
  • Unusual database activity
  • Unexpected website behavior
You can use a reputable security scanner such as Wordfence or Sucuri as part of the investigation.

But remember:

A security scan is one part of an investigation—not necessarily the entire investigation.

If malware is found, the goal should be to identify and remove the malicious code and then secure the website.

Also change relevant passwords, including:
  • WordPress administrator passwords
  • Hosting account
  • FTP/SFTP
  • Database credentials
WordPress security scan checking for malware and suspicious files

What If None of These Methods Work?

If you've checked the configuration, database, WordPress files, and security side and the website still shows the same error, the problem may be deeper.

Possible causes can include:
  • Hosting configuration problems
  • Database server failures
  • Firewall restrictions
  • Database permission issues
  • Server resource problems
  • MySQL/MariaDB problems
  • A damaged or incomplete migration
At this point, contacting your hosting provider is often the right next step.

Ask them to check:
  • Database server status
  • Database connectivity
  • Database user permissions
  • Server logs
  • Firewall restrictions
  • Recent server-side changes

A Special Case: After a WordPress Migration

If this error appears immediately after moving a WordPress website to another server or localhost, check the database connection settings first.

For example, after a migration, the database may have been imported successfully, but wp-config.php may still contain the old:
  • Database name
  • Username
  • Password
  • Host
This can make the website show the database connection error even though the database itself exists.

This is especially worth checking when you're moving a WordPress site between hosting environments or from a live server to a local development environment.

How to Prevent This Error in the Future

You can't prevent every possible database or hosting failure, but you can reduce the risk and make recovery easier.

1. Keep Regular Backups

Maintain reliable backups of both:
  • Website files
  • Database
A backup is much more useful when you've actually tested that it can be restored.

2. Protect Your Hosting Credentials

Use strong, unique passwords for:
  • Hosting
  • WordPress
  • FTP/SFTP
  • Database
3. Keep WordPress Updated

Keep WordPress core, plugins, and themes maintained and updated appropriately.

4. Remove Unused Plugins and Themes

Unused software adds unnecessary code to the website.

Remove plugins and themes you no longer need.

5. Monitor Website Security

If your website is business-critical, regular security monitoring can help you identify suspicious changes before they become a larger problem.

6. Keep a Known-Good Backup

A recent clean backup can make recovery much easier when a serious problem occurs.

How to Troubleshoot This Error Without Making Things Worse

When a WordPress website is down, it's tempting to start changing everything.

Don't.

A better approach is:

Backup → Identify the symptom → Check credentials → Check database → Check hosting → Check files → Check security → Contact support if necessary

This makes it easier to identify the real cause.

It also reduces the chance of creating a second problem while trying to solve the first one.

Frequently Asked Questions

Will I lose my website data if I repair the database?

A database repair operation is intended to repair database problems rather than remove your normal website content. However, you should still create a backup before performing database repairs whenever possible.

Can a wrong database password cause this error?

Yes. If the credentials in `wp-config.php` don't match the database account details, WordPress may not be able to connect to the database.

Can a plugin cause a database connection error?

Yes, a plugin can contribute to database-related problems. If you suspect a plugin, investigate recent changes and consider temporarily disabling plugins as part of a controlled troubleshooting process.

Should I change DB_HOST from localhost?

Not automatically.

Many WordPress installations use `localhost`, but some hosting environments require a different database hostname. Use the value provided by your hosting provider.

Is the database always corrupted when this error appears?

No.

A database connection error can have several causes, including incorrect credentials, server problems, permissions, or other hosting and WordPress issues.

Can a hacked WordPress website cause database problems?

A compromised website can contain malicious or modified files that interfere with normal website behavior. If the error appeared together with other signs of compromise, include malware investigation in your troubleshooting.

Should I leave WP_ALLOW_REPAIR enabled?

No.

If you enable the WordPress repair feature, remove the `WP_ALLOW_REPAIR` line from `wp-config.php` after you're finished. WordPress documents this feature as something to enable when needed and disable afterward.

Final Thoughts

The “Error Establishing a Database Connection” message can look serious, but the message itself doesn't tell you that your database is permanently damaged.

The problem could be something as simple as an incorrect database credential, or it could involve the database server, hosting environment, WordPress files, or a security compromise.

The best approach is to troubleshoot systematically.

Start with:
  1. Check wp-config.php
  2. Verify the database
  3. Check the database server
  4.  Check WordPress core files
  5. Investigate resource or hosting issues
  6.  Check for malware if there are signs of compromise
  7.  Contact your hosting provider when the problem is server-side
Most importantly, don't make random changes to a production website without a backup and a reason for each change.

Need Help Fixing a WordPress Website?

I provide WordPress services including:
  • WordPress Error Fixing
  • WordPress Malware Removal
  • WordPress Security Audit
  • WordPress Security Hardening
  • WordPress Migration
  • WordPress Maintenance
If your WordPress website is showing a database connection error, a hacked-site warning, unexpected redirects, or other serious problems, the first step is to identify the actual cause.

Need help with a broken or hacked WordPress website?

You can contact me through my freelance service channels.

Related WordPress Guides



WordPress Website Hacked in 2026? Here's How to Clean Malware Fast & Secure It Permanently

How to Fix White Screen of Death in WordPress (2026 Updated Guide)