Error Establishing a Database Connection in WordPress

Table of Contents

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

Seeing “Error Establishing a Database Connection” when you visit your WordPress website can be alarming.

Instead of your homepage, WordPress displays an error message, and your website may become completely inaccessible. In some cases, the WordPress dashboard may also stop working.

The good news is that this error is usually fixable.

The message means that WordPress cannot successfully communicate with the database where your website’s content and configuration are stored. The problem could be caused by incorrect database credentials, a database server outage, corrupted database tables, hosting problems, insufficient resources, or a configuration issue.

In this guide, you’ll learn what the WordPress database connection error means, what causes it, and how to troubleshoot and fix it step by step.

You’ll also learn how to prevent the problem from happening again.

What Does “Error Establishing a Database Connection” Mean?

WordPress relies on two major components to work:

  • WordPress files
  • A database

The WordPress files contain the application itself, including the WordPress core, themes, plugins, and uploaded files.

The database stores important information such as:

  • Posts
  • Pages
  • Users
  • Settings
  • Comments
  • Menus
  • Plugin data
  • Theme settings
  • WooCommerce data
  • Custom post types
  • Website configuration

When someone visits your website, WordPress needs to communicate with the database to retrieve the information required to generate the page.

If WordPress cannot connect to the database, it cannot retrieve that information.

That’s when you may see:

Error Establishing a Database Connection

This doesn’t necessarily mean that your entire database has been deleted or destroyed.

It simply means that WordPress cannot establish a successful connection to it.

What Causes a WordPress Database Connection Error?

There isn’t a single cause.

The most common causes include:

  • Incorrect database username
  • Incorrect database password
  • Incorrect database name
  • Incorrect database host
  • Corrupted database
  • Database server outage
  • Hosting server problems
  • Database user permission problems
  • Too many database connections
  • Insufficient hosting resources
  • Corrupted WordPress files
  • Incorrect wp-config.php configuration
  • Server migration problems
  • Plugin or theme issues
  • Malware or unauthorized changes

The important part is identifying which one is affecting your website.

Before Fixing the Error: Create a Backup

If you still have access to your hosting control panel or database management tools, create a backup before making changes.

Ideally, you should have:

  • A full website backup
  • A database backup
  • A copy of wp-config.php
  • A copy of the WordPress files

If you have a recent working backup, troubleshooting becomes much safer.

This is also why maintaining regular WordPress backups is so important, especially for websites that generate leads, publish content, or process orders.

Step 1: Check Whether Your Hosting Server Is Working

Before changing WordPress files, check your hosting account.

The problem may not actually be WordPress.

Your database server could be:

  • Down
  • Restarting
  • Overloaded
  • Experiencing an outage
  • Running out of resources

Check your hosting provider’s status page or server monitoring tools.

If your hosting provider is experiencing a database outage, changing your WordPress configuration won’t fix the problem.

You simply need to wait for the database service to become available again or contact hosting support.

Step 2: Check Your WordPress Database Credentials

One of the most common causes is incorrect database information.

WordPress stores its database connection details in:

wp-config.php

Look for these settings:

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

Your actual values will be different.

The important settings are:

  • DB_NAME
  • DB_USER
  • DB_PASSWORD
  • DB_HOST

If any of these values are incorrect, WordPress may not be able to connect to the database.

Step 3: Verify the Database Name

Log in to your hosting control panel and open your database management section.

Depending on your hosting provider, you may see tools such as:

  • phpMyAdmin
  • MySQL Databases
  • Database Manager
  • Database Wizard

Find the database associated with your WordPress website.

Then compare its name with:

define( 'DB_NAME', 'database_name' );

The values need to match.

A common issue after migration is that the database was imported under a different name, while wp-config.php still contains the old database name.

Step 4: Verify the Database Username

Next, check the database user.

Your hosting panel should show which users have access to the database.

Compare the username with:

define( 'DB_USER', 'database_user' );

If the username is incorrect, WordPress won’t be able to authenticate with the database.

This can happen after:

  • Hosting migrations
  • Manual database imports
  • Server changes
  • Website cloning
  • Account restructuring

Step 5: Verify the Database Password

The database password must also match.

If you’re unsure what the current database password is, you may need to reset it through your hosting control panel.

After changing the password, update:

define( 'DB_PASSWORD', 'new_password' );

in wp-config.php.

Be extremely careful when editing this file.

A syntax error can cause a different WordPress error.

Step 6: Check the Database Host

The database host is often:

define( 'DB_HOST', 'localhost' );

However, localhost is not universal.

Some hosting environments use a different database hostname.

If your hosting provider specifies a particular database host, use that value.

This is especially important when moving WordPress between hosting environments.

A configuration that worked on one server may not work on another.

Step 7: Check Whether the Database User Has the Correct Permissions

Your database user needs appropriate permissions to access the WordPress database.

Even if:

  • The database exists
  • The username is correct
  • The password is correct

the connection can still fail if the user isn’t properly assigned to the database.

In your hosting control panel, verify that the correct database user is associated with the correct database.

If necessary, reassign the user and ensure it has the required privileges.

Step 8: Test the Database in phpMyAdmin

If your hosting provider offers phpMyAdmin, open it and try to access the WordPress database.

If the database appears normally, that’s useful information.

It suggests that the database server is responding and the problem may be related to WordPress configuration or permissions.

If phpMyAdmin cannot access the database either, the problem may be at the server or database level.

Step 9: Repair the WordPress Database

Sometimes WordPress database tables become corrupted.

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

Add:

define( 'WP_ALLOW_REPAIR', true );

Then visit:

/wp-admin/maint/repair.php

WordPress provides database repair and optimization options there.

Important

This page should not remain publicly accessible after you’re finished.

Remove:

define( 'WP_ALLOW_REPAIR', true );

from wp-config.php once the repair process is complete.

Database repair should also be performed carefully and preferably after creating a backup.

Step 10: Check for Corrupted Database Tables

If your database tool provides a repair function, you can inspect individual tables.

WordPress databases commonly contain tables such as:

  • wp_posts
  • wp_postmeta
  • wp_options
  • wp_users
  • wp_usermeta
  • wp_terms
  • wp_term_relationships

Your table prefix may not be wp_.

If one or more tables are marked as corrupted, repairing them may resolve the connection problem.

However, if the database is severely damaged, restoring a known-good backup may be safer than attempting extensive manual repairs.

Step 11: Check Whether Your Hosting Account Has Run Out of Resources

Resource limitations can cause database problems.

Depending on your hosting environment, you may be limited by:

  • CPU
  • RAM
  • PHP workers
  • Database connections
  • Disk space
  • I/O
  • Processes

If the server is heavily overloaded, database requests may fail or time out.

This is especially common on websites with:

  • High traffic
  • Poorly optimized plugins
  • Large WooCommerce stores
  • Heavy database queries
  • Inefficient themes
  • Large amounts of background processing

Check your hosting dashboard for resource usage.

If your account is constantly reaching its limits, upgrading hosting or optimizing the website may be necessary.

For more information about improving overall WordPress performance, see our guide on How to Speed Up a WordPress Website: Complete Optimization Guide.

Step 12: Check for a Hosting Database Outage

Sometimes the problem is entirely outside your website.

Your hosting provider may have:

  • Database maintenance
  • Server downtime
  • Network problems
  • Hardware issues
  • MySQL service problems

If multiple websites on the same server are experiencing similar problems, that’s a strong indication that the problem is infrastructure-related.

In this situation, contact hosting support and ask them to check the database server.

Step 13: Disable WordPress Plugins

A plugin can sometimes contribute to database-related problems.

If you can access the WordPress dashboard, temporarily deactivate your plugins.

If you cannot access the dashboard, you can rename the plugins directory using your hosting file manager or FTP.

The directory is normally:

wp-content/plugins

Rename it temporarily to something like:

plugins-disabled

Then test your website.

If the website starts working, a plugin may be responsible.

You can then restore the original directory name and identify the problematic plugin by activating plugins individually.

For a systematic approach to this type of troubleshooting, see:

How to Diagnose and Resolve WordPress Plugin and Theme Conflicts.

Step 14: Check Your WordPress Theme

Themes can also cause unexpected database queries or PHP errors.

If the problem appeared immediately after a theme change or update, the theme should be investigated.

If you have access to the WordPress dashboard, temporarily switch to a default WordPress theme.

If you don’t have dashboard access, you can rename the active theme’s directory so WordPress attempts to fall back to another installed theme.

Always make a backup before making filesystem changes.

Step 15: Check Your WordPress Files

If database credentials are correct and the database is functioning, corrupted WordPress core files may be another possibility.

You can replace the WordPress core files with a fresh copy of the same WordPress version while preserving:

  • wp-content
  • wp-config.php

Do not blindly overwrite files without understanding what you’re replacing.

If the problem started immediately after a failed update, corrupted core files become a more likely explanation.

Step 16: Check Your PHP Version

An incompatible PHP version can cause serious WordPress problems.

Check the PHP version configured by your hosting provider.

Make sure your WordPress version, theme, and plugins are compatible with the PHP version you’re using.

If the database connection problem appeared immediately after changing PHP versions, consider whether the change is related.

For production websites, avoid making major server configuration changes without testing them first.

Step 17: Check the Error Logs

If you still can’t identify the cause, check your server and WordPress logs.

Useful sources can include:

  • Hosting error logs
  • PHP error logs
  • WordPress debug logs
  • Web server logs
  • Database server logs

WordPress debugging can be enabled in wp-config.php with settings such as:

define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );

This can help record PHP errors in:

/wp-content/debug.log

Important

Don’t leave debugging enabled unnecessarily on a production website.

After troubleshooting, return the settings to an appropriate production configuration.

Step 18: If the Website Was Recently Migrated, Recheck Everything

If the database error appeared after moving your website to another host, migration-related configuration is one of the first things you should investigate.

Check:

  • Database name
  • Database username
  • Database password
  • Database host
  • Database user permissions
  • Database import
  • wp-config.php
  • PHP version
  • Server configuration
  • DNS
  • SSL

A database connection problem after migration is often caused by a mismatch between the new hosting environment and the old database configuration.

If you’re planning a migration, our guide on How to Migrate a WordPress Website Without Losing SEO covers the SEO side of moving a WordPress website.

Step 19: Check Your DNS After a Hosting Change

If you’ve recently changed hosting, DNS can also be part of the problem.

Verify that your domain points to the correct server.

Check:

  • A record
  • CNAME records
  • Nameservers
  • DNS propagation
  • SSL certificate
  • Hosting account configuration

However, remember that DNS problems typically produce different symptoms than a database connection error.

If the domain is correctly reaching the new WordPress server but WordPress itself cannot connect to its database, focus on the database and server configuration.

Step 20: Restore a Known-Good Backup if Necessary

If you’ve tried the standard troubleshooting steps and the database is severely corrupted, restoring a recent working backup may be the safest solution.

Before restoring:

  • Preserve the current files and database.
  • Identify the most recent known-good backup.
  • Confirm that the backup belongs to the correct website.
  • Restore carefully.
  • Test the website.
  • Identify what caused the original failure.

Don’t immediately overwrite everything without preserving the current state.

The current version may contain information you need to recover.

How to Prevent the WordPress Database Error in the Future

Fixing the error is important, but preventing it is even better.

Keep Regular Backups

Maintain automated backups of:

  • Database
  • WordPress files
  • Uploads
  • Configuration

Ideally, store backups separately from your primary hosting account.

Keep WordPress Updated

Regular updates can improve security, compatibility, and stability.

However, always test major changes before applying them to production.

Our guide on How to Safely Update & Test WordPress Websites Using Staging Environments explains how to use staging to test updates safely.

Monitor Hosting Resources

Don’t wait until your website stops working.

Monitor:

  • CPU usage
  • RAM
  • Disk space
  • I/O
  • PHP workers
  • Database usage

If your hosting environment is consistently reaching its limits, investigate before the problem becomes an outage.

Avoid Poorly Optimized Plugins

Every plugin adds functionality, but poorly developed or unnecessary plugins can increase database queries and server load.

Periodically review your plugin list and remove plugins you no longer need.

Optimize Your Database

Over time, WordPress databases can accumulate unnecessary data.

Depending on your website, this can include:

  • Post revisions
  • Transients
  • Spam comments
  • Trashed content
  • Plugin leftovers
  • Expired temporary data

Database optimization can help keep your installation manageable, although it should always be performed carefully and after creating a backup.

When Should You Contact Your Hosting Provider?

Contact your hosting provider if:

  • The database server appears to be down
  • Multiple sites on your account are affected
  • You cannot access the database through your hosting tools
  • Database connections are repeatedly timing out
  • Your server is hitting resource limits
  • You suspect a MySQL or MariaDB problem
  • The problem started after a server migration
  • You don’t have access to the required database configuration

Hosting support can often identify server-side problems that aren’t visible from WordPress.

Quick Troubleshooting Checklist

If you’re facing the error right now, work through these steps in order:

  • Check whether your hosting provider has an outage
  • Create a backup if possible
  • Verify DB_NAME
  • Verify DB_USER
  • Verify DB_PASSWORD
  • Verify DB_HOST
  • Check database user permissions
  • Open the database in phpMyAdmin
  • Check database tables for corruption
  • Check hosting resource usage
  • Temporarily disable plugins
  • Check the active theme
  • Review PHP and server error logs
  • Check WordPress debug logs
  • Verify recent migration changes
  • Restore a known-good backup if necessary
  • Contact your hosting provider if the database server is unavailable

Final Thoughts

The “Error Establishing a Database Connection” message looks serious, but it doesn’t automatically mean that your WordPress website or database is permanently damaged.

In many cases, the cause is relatively simple:

  • Incorrect database credentials
  • Incorrect database host
  • Database permissions
  • Hosting problems
  • Resource limitations
  • Database corruption
  • A recent migration or configuration change

The best approach is to troubleshoot systematically rather than immediately reinstalling WordPress or restoring an old backup.

Start with the simplest possibilities: hosting status, database credentials, database availability, and permissions. Then move toward database repair, resource analysis, plugin conflicts, server configuration, and backups.

Most importantly, use this problem as a reminder that a reliable WordPress website needs more than just good content. Backups, secure hosting, performance optimization, staging, and regular maintenance all work together to keep your website stable.

Take Your WordPress Skills Further

If you’re serious about learning how to build, optimize, secure, and maintain professional WordPress websites, the WP Flow Mastery WordPress eBook brings these essential skills together in one practical resource.

Get the WP Flow Mastery WordPress eBook →

Written by Nemanja Stosic

WordPress Developer & Website Optimization Specialist

Nemanja helps businesses and freelancers build, optimize, and maintain professional WordPress websites focused on performance, SEO, security, and usability.

Before You Go…

Get our free WordPress guide and learn the essential strategies for building, optimizing and growing your website.

Free guide · No spam · Unsubscribe anytime

Get the Free WordPress Guide

Learn how to build, optimize and grow a professional WordPress website with practical strategies you can actually use.

No spam. Unsubscribe anytime.