How to Fix “Error Establishing a Database Connection” in WordPress

Geschätzte Lektüre: 5 Minuten

The “Error Establishing a Database Connection” message indicates that WordPress cannot communicate with its database. Since WordPress stores your website’s content, settings, users, and configuration data in a database, the website cannot function until the connection is restored.

This guide explains the most common causes of the error and provides step-by-step troubleshooting methods.

Symptoms

You may notice one or more of the following:

  • Your website displays “Error Establishing a Database Connection.”
  • The WordPress admin dashboard is inaccessible.
  • Visitors cannot access your website.
  • The error appears immediately after updating WordPress, migrating your website, or changing hosting providers.

Common Causes

The most common reasons include:

  • Incorrect database credentials
  • Corrupted database
  • Database server is unavailable
  • Corrupted wp-config.php
  • Hosting server issues
  • Exceeded database limits
  • Corrupted WordPress files

Before You Begin

Before making changes:

  • Create a backup if possible.
  • Verify you have access to your hosting control panel.
  • Confirm you can access your website files.
  • Have your database credentials available.

Step 1: Check Your Database Credentials

WordPress stores its database connection details in the wp-config.php file.

Verify the following values:

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

Ensure that:

  • Database name is correct.
  • Database username is correct.
  • Password matches the database user.
  • Database host is correct.

Incorrect credentials are one of the most common causes of this error.

Step 2: Verify the Database Exists

Using your hosting control panel:

  • Open the MySQL Databases section.
  • Confirm the database still exists.
  • Verify the database user is assigned to the database.
  • Ensure the user has All Privileges.

If the database was accidentally deleted, restore it from a recent backup.

Step 3: Check the Database Server

Sometimes the database server is temporarily unavailable.

Possible causes include:

  • Server maintenance
  • Database service restart
  • High server load
  • Hosting outage

If multiple websites on the same hosting account experience the same issue, contact your hosting provider.

Step 4: Repair the WordPress Database

If the database has become corrupted, WordPress includes a built-in repair feature.

Enable Database Repair

Open wp-config.php and add:

define('WP_ALLOW_REPAIR', true);

Save the file.

Then visit:

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

Choose:

  • Repair Database
  • Repair and Optimize Database

After the repair completes, remove the following line from wp-config.php:

define('WP_ALLOW_REPAIR', true);

Leaving it enabled may expose the repair page to unauthorized visitors.

Step 5: Verify Database Host

Most hosting providers use:

localhost

However, some providers use a different hostname.

If you’re unsure, check your hosting documentation or contact your hosting provider.

Step 6: Check Database User Permissions

The database user should have permission to:

  • SELECT
  • INSERT
  • UPDATE
  • DELETE
  • CREATE
  • ALTER
  • INDEX
  • DROP

If permissions are missing, reassign the user and grant All Privileges.

Step 7: Check Server Resources

Insufficient server resources can prevent database connections.

Review your hosting account for:

  • Memory usage
  • CPU usage
  • Disk space
  • Database limits

Resource exhaustion is more common on shared hosting environments.

Step 8: Restore a Recent Backup

If the error occurred after:

  • Updating WordPress
  • Installing a plugin
  • Editing configuration files
  • Migrating your website

Restore your most recent working backup.

Step 9: Verify WordPress Core Files

Corrupted core files can sometimes contribute to connection issues.

If necessary:

  • Download the latest version of WordPress.
  • Replace only the core files.
  • Do not overwrite the wp-content directory or wp-config.php.

Step 10: Contact Your Hosting Provider

If all previous steps fail, your hosting provider can help determine whether the issue is related to:

  • Database service failures
  • Server outages
  • Corrupted databases
  • Account restrictions
  • Resource limitations

Provide them with the exact error message and any troubleshooting steps you have already completed.

Verify the Fix

After resolving the issue:

  • Reload your website.
  • Log in to the WordPress dashboard.
  • Confirm posts and pages load correctly.
  • Verify plugins and themes function normally.
  • Review your website for any additional errors.

Common Scenarios

Website Works but Admin Does Not

Possible causes:

  • Corrupted plugin
  • Database table corruption
  • User table issues

Both Website and Admin Are Offline

Usually caused by:

  • Incorrect database credentials
  • Database server outage
  • Corrupted database

Error Appeared After Migration

Verify:

  • Database import completed successfully.
  • wp-config.php contains the new database credentials.
  • Database hostname matches your new hosting environment.

Error Appeared After Updating WordPress

Try:

  • Restoring your latest backup.
  • Checking database integrity.
  • Verifying plugin compatibility.

Best Practices

  • Create backups before making major changes.
  • Keep WordPress updated.
  • Use reliable hosting.
  • Monitor server resources.
  • Store database credentials securely.
  • Test backups regularly.
  • Review database permissions periodically.

Frequently Asked Questions

What causes “Error Establishing a Database Connection”?

The error usually occurs because WordPress cannot connect to its database due to incorrect credentials, database corruption, server outages, or configuration issues.

Will I lose my website data?

Not necessarily. In most cases, your data still exists in the database, but WordPress is temporarily unable to access it.

Can plugins cause this error?

Plugins rarely cause the error directly, but an incompatible plugin or failed update can contribute to database issues or configuration changes.

Should I reinstall WordPress?

Reinstalling WordPress should only be considered after verifying that your database credentials, database server, and configuration files are correct.

Launchpad