Ultimate Database Connection Error Fix for WordPress

The “Error establishing a database connection” message blocks your WordPress site and usually stops you from logging in to the admin panel. This guide shows you how to check your host, confirm your database details, update wp-config.php, and safely test whether your site can reach its database again.

Quick Answer

The “Error establishing a database connection” message means WordPress cannot connect to the MySQL database that stores your posts, pages, users, and settings. Start by asking your host about server issues, then check DB_NAME, DB_USER, DB_PASSWORD, and DB_HOST in your wp-config.php file.

Key Takeaways

  • The error usually comes from wrong database credentials, a database server issue, corrupted tables, or a migration mismatch.
  • Contact your hosting provider first so you do not edit files when the real problem is server-side.
  • The most important wp-config.php values to check are DB_NAME, DB_USER, DB_PASSWORD, and DB_HOST.
  • Resetting the database user password in cPanel often fixes credential-related connection failures.
  • Back up your files and database before using repair tools, renaming folders, or restoring a backup.

At a Glance

Time Required 10 to 30 minutes for credential checks; longer if the host must repair a server issue.
Difficulty Beginner to intermediate, depending on whether you need to edit wp-config.php.
Tools Needed Hosting control panel, File Manager or FTP access, MySQL Databases area, and your WordPress site URL.
Cost Usually free if you fix it yourself or through your hosting support team.

Website showing error establishing a database connection message

Why Does This Error Happen?

WordPress relies on a database to store your posts, pages, comments, theme settings, plugin settings, and user information. When WordPress cannot reach that database, it cannot build the page, so it shows the database connection error instead.

Common triggers for this error include:

  • Incorrect database credentials within the wp-config.php file.
  • A changed or reset database password that was not updated in WordPress.
  • An incorrect DB_HOST value, especially after moving hosts.
  • Database corruption resulting from plugin conflicts, failed updates, or sudden crashes.
  • Temporary downtime or maintenance on your hosting provider’s database server.
  • Exceeded resource limits on shared hosting plans.
  • Configuration mismatches during a recent site migration.

Warning: Do not randomly change database names, users, or passwords without saving a backup copy of wp-config.php first. One wrong value can keep the site offline.

Step 1: Check With Your Hosting Provider

Before you modify any files, contact your hosting support team. Server-side issues, such as database outages, high load, account suspension, or resource limits, can cause this error and require host-level intervention.

Ask your host these questions:

  • Is the MySQL or MariaDB database server running normally?
  • Has my hosting account hit any CPU, memory, process, or database connection limit?
  • Do you see database connection errors in the server logs?
  • Was my database user password changed recently?
  • Did a migration, restore, or server move change my database host value?

This step prevents you from making unnecessary changes to your files if the problem originates outside your control.

Step 2: Access Your Hosting Control Panel

If the hosting support team confirms their servers are healthy, use your cPanel or hosting control panel to investigate further. Log in to your hosting account and navigate to the file management section.

Most hosts provide File Manager, FTP, or SFTP access. File Manager is the easiest option for beginners because you can open wp-config.php directly from the browser.

Logging into cPanel interface

Step 3: Audit Your wp-config.php File

The wp-config.php file acts as the bridge between your site and the database. You must ensure the credentials listed there match the ones stored on your server.

  1. Open the File Manager in your cPanel.
  2. Navigate to the public_html folder or your site’s root directory.
  3. Right-click the wp-config.php file and select Edit.
  4. Locate the lines defining DB_NAME, DB_USER, DB_PASSWORD, and DB_HOST.
  5. Copy the current values into a safe note before changing anything.

The main lines usually look similar to this:

define( 'DB_NAME', 'database_name_here' );
define( 'DB_USER', 'username_here' );
define( 'DB_PASSWORD', 'password_here' );
define( 'DB_HOST', 'localhost' );

Check these values carefully. DB_NAME must match the database name. DB_USER must match the database user assigned to that database. DB_PASSWORD must match the current password for that user. DB_HOST is often localhost, but some hosting providers use a custom hostname.

Note: Database passwords often contain symbols. Copy and paste the full password exactly, and do not add extra spaces before or after it.

Editing wp-config.php file in cPanel file manager

Step 4: Reset Your Database Password

If your credentials in the configuration file do not match your current database settings, update them by resetting the user password. This is often faster than trying to guess the old password.

  1. Return to the cPanel dashboard.
  2. Select the MySQL Databases or Databases option.
  3. Find the database user associated with your WordPress site.
  4. Select the option to change the password and create a new one.
  5. Copy the new password and update the DB_PASSWORD line in your wp-config.php file.
  6. Save your changes to the file.

After saving the new password, make sure the database user still has permission to access the correct database. In cPanel, this is usually shown under the section that connects users to databases.

Pro Tip: If your host offers a password generator, use it. Strong generated passwords reduce the risk of unauthorized database access.

Changing database user password in cPanel

Updating password in wp-config.php and saving changes

Step 5: Test Your Site

Refresh your website in your browser to verify the connection. If the credentials are correct and the database server is working, your content should load normally.

Next, test your admin panel by visiting yourwebsite.com/wp-admin. If the front end loads but the admin area still shows a database repair message, your database may need a repair step.

Clear your browser cache or open the site in a private window if you still see the error after making the fix. Some caching layers can briefly show an old error page.

Website successfully loading after fixing database connection error

Additional Troubleshooting Steps

If the issue persists, try these professional methods to isolate the problem:

  • Verify the hostname: Confirm that the DB_HOST value in your configuration file is set to localhost, unless your host specifies a custom address.
  • Check database permissions: Make sure the database user has full permission to access the WordPress database.
  • Repair the database: Add the code define('WP_ALLOW_REPAIR', true); to your wp-config.php file, then visit yoursite.com/wp-admin/maint/repair.php.
  • Remove the repair line afterward: Delete define('WP_ALLOW_REPAIR', true); after the repair is complete because the repair page can be accessed without logging in.
  • Check for conflicts: Rename your plugins folder via FTP to temporarily deactivate all plugins and rule out a software conflict.
  • Review recent changes: Think about recent plugin updates, theme updates, PHP version changes, server moves, or backup restores.
  • Use a backup: If corruption remains, restore your site from a recent, known-good backup.

Always consult your hosting documentation or a professional developer before performing major database repairs or file changes.

How to Check DB_HOST Correctly

The DB_HOST value tells WordPress where the database server lives. On many shared hosting accounts, this value is localhost. On some managed, cloud, or migrated sites, it may be a custom hostname, IP address, or socket path.

If your host provides a database details screen, compare that screen with the DB_HOST line in wp-config.php. If they do not match, update wp-config.php with the value your host provides.

define( 'DB_HOST', 'localhost' );

Only change this value when you have a confirmed hostname from your hosting provider or control panel.

How to Repair a WordPress Database Safely

If the credentials are correct but WordPress still cannot read the database properly, a table may be corrupted. WordPress includes a built-in repair tool, but you should use it carefully.

  1. Back up your site files and database first.
  2. Open wp-config.php.
  3. Add this line above the final comment in the file: define('WP_ALLOW_REPAIR', true);
  4. Visit yoursite.com/wp-admin/maint/repair.php.
  5. Choose the repair option.
  6. Remove the repair line from wp-config.php immediately after finishing.

Warning: Do not leave the WordPress repair tool enabled. Remove the repair code after use so visitors cannot access that repair page.

Why Migration Triggers Errors

Moving a site to a new server often causes this error because the database environment changes. The new server might require a different database name, a new user, a new password, or a specific hostname. Ensure every value in your configuration file aligns with the settings on your new server provider.

This problem also happens when a site is restored from backup but the database was not imported correctly. If the files came from one backup and the database came from another, WordPress may point to a database that does not contain the expected tables.

What to Do If You Still Cannot Fix It

If the error remains after checking credentials, password, hostname, permissions, plugins, and database repair, send your host a clear support request. Include the exact error message, the time it started, what changed recently, and the steps you already tried.

You can also ask the host to check:

  • MySQL service status.
  • Database table corruption.
  • User permission errors.
  • Resource limit logs.
  • Recent server migrations or maintenance.
  • Firewall or security rules blocking database access.

This gives the support team enough context to investigate faster.

Summary Checklist

  • Ask your host to confirm server status.
  • Check your wp-config.php file for matching credentials.
  • Confirm DB_NAME, DB_USER, DB_PASSWORD, and DB_HOST.
  • Reset the database user password in cPanel if needed.
  • Make sure the database user has permission to access the database.
  • Update your configuration file with the correct credentials.
  • Run the WordPress repair tool only after backing up your site.
  • Remove the repair line from wp-config.php when finished.
  • Restore from a recent backup if corruption cannot be repaired.

Fixing this error restores your site access and helps protect your content. Use these steps carefully, test after each change, and contact your hosting support team if the problem continues.

Sources

  1. WordPress Developer Resources: Editing wp-config.php — supports the explanation of database configuration values.
  2. WordPress Documentation: Debugging in WordPress — supports safe troubleshooting practices for WordPress errors.
  3. WordPress Developer Resources: Backups — supports the recommendation to back up before repairs or restores.

Last Updated on June 27, 2026 by Logan Carter

Releted Post