How do I fix the ‘Error Establishing a Database Connection’ in WordPress?

Learn how to fix the “Error Establishing a Database Connection” in WordPress. Check database credentials, wp-config.php, MySQL, and common server issues.

How Do I Fix the “Error Establishing a Database Connection” in WordPress?

The “Error Establishing a Database Connection” message is one of the most frustrating errors a WordPress website owner can encounter.

Instead of loading your homepage, posts, or WordPress dashboard, the website may display only this message:

Error establishing a database connection

The good news is that this error usually does not mean your entire website has disappeared. In most cases, WordPress simply cannot communicate with the database that stores your site’s content and settings.

The problem may be caused by incorrect database credentials, a stopped database server, corrupted database tables, hosting resource limits, or damaged WordPress configuration files.

This guide explains what the error means, why it happens, and how to troubleshoot it safely.


Quick Answer

To fix the “Error Establishing a Database Connection” error in WordPress, first check whether your database server is working. Then verify the database name, username, password, and database host in the wp-config.php file.

If the credentials are correct, check whether the database is corrupted, review your hosting resource usage, restart the database service if you manage the server, and contact your hosting provider if necessary.

Before making major changes, create a backup whenever your hosting environment allows it.


What Does “Error Establishing a Database Connection” Mean?

WordPress is primarily built using PHP and a database system such as MySQL or MariaDB.

Your WordPress files contain the application code, themes, plugins, images, and configuration information. The database contains much of the site’s dynamic information, including posts, pages, comments, settings, users, and plugin data.

When someone opens a WordPress page, WordPress needs to connect to its database and retrieve the required information.

The basic process looks like this:

Visitor → Web Server → WordPress/PHP → Database → WordPress Page

If WordPress cannot successfully connect to the database, it cannot retrieve the information needed to construct the page.

It therefore displays:

Error establishing a database connection


Common Causes of the WordPress Database Connection Error

Several different problems can produce the same error message.

CauseWhat Happens
Incorrect database nameWordPress attempts to connect to the wrong database
Incorrect usernameThe database rejects the login
Incorrect passwordAuthentication fails
Incorrect database hostWordPress tries to reach the wrong database server
Database server unavailableMySQL or MariaDB is stopped or unreachable
Corrupted databaseWordPress may be unable to access required tables correctly
Hosting resource limitsThe server may be overloaded or unable to accept more connections
Migration problemsDatabase settings may not match the new hosting environment
Server configuration issuePHP and the database server cannot communicate correctly
Hosting outageThe database service may temporarily be unavailable

The correct solution depends on which of these problems is responsible.


Before You Start: Back Up Your Website

Database troubleshooting sometimes involves editing wp-config.php, repairing database tables, restarting services, or changing database users.

Create a backup before making significant changes whenever possible.

Ideally, back up:

  • WordPress files
  • The WordPress database
  • wp-config.php
  • Themes and plugins
  • The wp-content directory

If your website is already completely inaccessible, check whether your hosting provider offers automatic backups or snapshots.


1. Check Whether the Problem Is Temporary

Before editing anything, reload the website after a short period.

You can also test:

and:

Replace example.com with your domain.

If both the frontend and dashboard show database-related errors, the database server or database configuration is a likely cause.

If the error appeared suddenly without any changes to your website, check your hosting provider’s service status. A temporary MySQL or MariaDB outage can produce the same message.

Do not immediately change your database password if the website was working normally a few minutes earlier. First determine whether the server itself is having problems.


2. Check the WordPress Database Credentials

Incorrect database credentials are among the most common causes of this error.

WordPress stores its database connection information in:

wp-config.php

This file is normally located in the root directory of your WordPress installation.

You can access it using:

  • Your hosting file manager
  • FTP
  • SFTP
  • SSH

Look for lines similar to these:

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

Each value must match the database information configured on your hosting server.


Understanding the Four Database Settings

DB_NAME

DB_NAME tells WordPress which database it should use.

Example:

define( 'DB_NAME', 'mywordpressdb' );

A typo or outdated database name prevents WordPress from connecting to the correct database.


DB_USER

DB_USER contains the database account WordPress uses.

Example:

define( 'DB_USER', 'wpuser' );

The database user must exist and have the required permissions for the WordPress database.


DB_PASSWORD

DB_PASSWORD contains the password for the database user.

Example:

define( 'DB_PASSWORD', 'your-password' );

If the database user’s password has recently been changed through your hosting panel, the password in wp-config.php must also be updated.

Be careful when copying passwords. Special characters, spaces, and typing errors can cause authentication failures.

Never publish your actual database password in tutorials, screenshots, support forums, or public code repositories.


DB_HOST

DB_HOST specifies the database server WordPress should contact.

A common value is:

define( 'DB_HOST', 'localhost' );

However, localhost is not universal.

Some hosting companies use a separate database hostname, IP address, socket, or hostname containing a port.

For example:

define( 'DB_HOST', 'mysql.example-host.com' );

Use the exact database host supplied by your hosting provider.


3. Compare wp-config.php With Your Hosting Database Information

Open your hosting control panel and locate the database management section.

Depending on the hosting platform, it may be called:

  • MySQL Databases
  • Database Management
  • MariaDB
  • Database Users
  • MySQL Management

Compare the hosting information with the values in wp-config.php.

Check:

WordPress SettingWhat to Verify
DB_NAMECorrect database name
DB_USERCorrect database username
DB_PASSWORDCorrect password
DB_HOSTCorrect database hostname

Even a small difference can prevent the connection.


4. Check Whether the Database User Has Permission

Correct credentials are not enough if the database user does not have permission to access the WordPress database.

This commonly happens after:

  • Moving a website
  • Restoring a backup
  • Creating a new database
  • Changing hosting accounts
  • Recreating a database user

In hosting panels such as cPanel, check whether the appropriate database user is assigned to the WordPress database.

The user generally needs the privileges required by WordPress to read and modify its tables.

If the user is not associated with the database, add it and assign the necessary privileges.

Be careful not to alter unrelated databases or database users on shared hosting accounts.


5. Test Whether the Database Server Is Running

If your credentials are correct but WordPress still cannot connect, the database service itself may be unavailable.

On managed or shared WordPress hosting, you usually cannot restart MySQL yourself. Check your provider’s status page or contact its support team.

If you manage your own VPS or dedicated Linux server, check the appropriate database service.

For example:

sudo systemctl status mysql

Or on a MariaDB-based server:

sudo systemctl status mariadb

If the service is stopped, an administrator may start it with the appropriate command, such as:

sudo systemctl start mysql

or:

sudo systemctl start mariadb

Do not restart production database services casually. First check logs and understand why the service stopped, especially on a server hosting multiple websites.


6. Check for Database Corruption

Sometimes WordPress can reach the database server, but one or more database tables have become damaged.

WordPress includes a database repair feature.

Add the following line to wp-config.php:

define( 'WP_ALLOW_REPAIR', true );

Then visit:

You should see options for repairing the database.

WordPress may provide options such as:

Repair Database

or:

Repair and Optimize Database

Start with the repair option when your goal is simply to resolve suspected corruption.

Important Security Step

After finishing the repair, remove:

define( 'WP_ALLOW_REPAIR', true );

from wp-config.php.

The repair page does not require a normal logged-in WordPress session while this feature is enabled, so it should not remain publicly available unnecessarily.


7. Check Your Hosting Resource Limits

A website can show a database connection error even when its credentials are completely correct.

The underlying problem may be server resources.

Possible causes include:

  • Very high traffic
  • Too many concurrent database connections
  • Insufficient memory
  • CPU limits
  • Disk space exhaustion
  • Database connection limits
  • Heavy plugins
  • Slow database queries
  • Automated bot traffic
  • Server overload

Shared hosting plans are particularly dependent on provider-specific resource limits.

Open your hosting dashboard and look for information about CPU, RAM, disk space, processes, database usage, and account limits.

If the problem appears only during traffic spikes, server capacity or inefficient database activity deserves closer investigation.


8. Check Available Disk Space

A full server disk can create serious database problems.

MySQL or MariaDB needs storage for normal database operations, temporary data, logs, and table changes.

On a Linux server, administrators can check disk usage with:

df -h

If the filesystem is nearly or completely full, investigate what is consuming the space.

Common causes include:

  • Oversized log files
  • Old backups
  • Cache files
  • Temporary files
  • Large databases
  • Unused website copies

Do not randomly delete server files to free space. Identify what each file or directory does first.


9. Think About What Changed Before the Error Appeared

The timing of the problem can provide an important clue.

Ask yourself:

Did the error begin after migrating the website?

Check the database name, username, password, hostname, permissions, and imported database.

Did it start after changing a database password?

Update DB_PASSWORD in wp-config.php.

Did it start after moving to a new server?

The database hostname may have changed.

Did it appear without any website changes?

Check for a hosting outage, overloaded server, crashed database service, storage issue, or resource limit.

Troubleshooting based on recent changes is usually faster than changing settings randomly.


10. Check WordPress Site URLs After a Migration

A wrong WordPress URL normally causes redirects or access problems rather than the basic database-connection error. However, migration issues often involve several configuration problems at once.

Once database connectivity has been restored, verify that the site’s WordPress Address and Site Address are correct.

They can normally be managed under:

WordPress Dashboard → Settings → General

If the dashboard cannot be accessed, advanced users can inspect the corresponding values in the WordPress database.

Do not change database values unless you understand what they control and have a backup.


11. Verify the Database Table Prefix

The WordPress database table prefix is configured in wp-config.php.

For example:

$table_prefix = 'wp_';

Your database may contain tables such as:

wp_posts
wp_options
wp_users
wp_comments

Some websites use a custom prefix:

$table_prefix = 'site7_';

If the configured prefix does not match the actual WordPress tables, WordPress may behave as though the expected installation data is missing.

This issue is especially relevant after manual migrations or configuration-file replacements.

It is not usually the direct cause of a basic inability to connect to MySQL, so verify the connection credentials first.


12. Check for a Damaged wp-config.php File

The wp-config.php file is critical to WordPress.

A syntax error, accidental edit, incorrect quotation mark, or missing character can break the configuration.

For example, the following structure is valid:

define( 'DB_NAME', 'wordpress_db' );
define( 'DB_USER', 'wordpress_user' );
define( 'DB_PASSWORD', 'secure_password' );
define( 'DB_HOST', 'localhost' );

When editing the file, avoid unnecessary changes.

It is also wise to keep a backup copy before modifying it.


13. Can Plugins Cause a Database Connection Error?

Plugins are not the first thing to suspect when WordPress explicitly cannot establish a database connection.

However, poorly designed or resource-intensive plugins can indirectly contribute to database problems by generating excessive queries or consuming server resources.

If the error is intermittent and appears under heavy load, investigate plugins that perform intensive operations, such as:

  • Analytics
  • Search indexing
  • Backup processing
  • Security scanning
  • Related-post generation
  • Import/export jobs
  • WooCommerce extensions
  • Database optimization
  • Large scheduled tasks

Use server logs and performance monitoring rather than disabling plugins randomly on a production site.


14. Can a WordPress Theme Cause This Error?

A theme is also unlikely to be the primary cause of a genuine database connection failure.

WordPress needs database access before much of its normal theme-driven page generation can occur.

However, custom theme code can make inefficient queries or place additional load on the database.

If the problem began after deploying custom code and is intermittent rather than constant, review that code as part of your performance investigation.


15. Check the PHP Error Log and Database Logs

Logs can provide much more useful information than the generic message shown to visitors.

Depending on your hosting environment, useful logs may include:

  • PHP error logs
  • Web server logs
  • MySQL error logs
  • MariaDB logs
  • Hosting resource logs

Messages such as authentication failures, connection limits, unavailable sockets, disk errors, or crashed tables can point directly toward the cause.

Server administrators should check logs before repeatedly restarting services.


16. Use WordPress Debugging Carefully

WordPress debugging can help diagnose related PHP problems.

In wp-config.php, WordPress supports settings such as:

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

This configuration can log debugging information without intentionally displaying it to visitors.

Debugging should be used carefully on a production website because logs can contain technical or potentially sensitive information.

Disable unnecessary debugging after troubleshooting.

Also remember that WordPress debugging cannot solve a database server that is completely unavailable.


17. What If the Error Appears Only Sometimes?

An intermittent database connection error is an important clue.

If the website works normally and then occasionally displays the error, the credentials are less likely to be the problem.

Investigate:

  • Database connection limits
  • Traffic spikes
  • CPU or RAM exhaustion
  • Slow queries
  • Database server restarts
  • Hosting instability
  • Scheduled backup jobs
  • Resource-intensive cron jobs
  • Large WooCommerce activity
  • Automated bot traffic

Intermittent failures often indicate a performance, capacity, or infrastructure problem rather than a permanently incorrect password.


18. What If the Error Appeared After Migrating WordPress?

Migration is a common time for database connection problems.

Check the following information carefully:

  1. Was the old database successfully exported?
  2. Was it imported into the new database?
  3. Does DB_NAME contain the new database name?
  4. Does DB_USER contain the new database user?
  5. Is DB_PASSWORD correct?
  6. Is the database user assigned to the database?
  7. Does DB_HOST match the new hosting environment?
  8. Are the WordPress tables present?
  9. Does $table_prefix match the imported tables?

Do not assume that database credentials from the old hosting account will work on the new server.


19. What If the Error Appeared After Changing the Domain?

Changing a domain alone does not normally change the database username or password.

Therefore, an “Error Establishing a Database Connection” immediately after a domain change may indicate that the website was also migrated, restored, or reconfigured.

First establish a working database connection.

After that, troubleshoot domain-specific issues such as:

  • WordPress Address
  • Site Address
  • HTTPS configuration
  • DNS
  • Redirects
  • Serialized URLs
  • Caching

Keeping database troubleshooting separate from URL troubleshooting makes the process easier to understand.


20. What If phpMyAdmin Can Open the Database?

If you can successfully access the WordPress database through phpMyAdmin or another database-management interface, that is useful evidence.

It suggests the database itself is available.

Then focus on:

  • DB_NAME
  • DB_USER
  • DB_PASSWORD
  • DB_HOST
  • Database-user privileges
  • Server-specific connection configuration

However, remember that phpMyAdmin may use different credentials or connection methods from WordPress. Its ability to access the server does not automatically prove that the credentials in wp-config.php are correct.


21. Should You Reinstall WordPress?

Usually, no.

Reinstalling WordPress should not be the first response to a database connection error.

If the real problem is an incorrect password or unavailable database server, reinstalling WordPress will not solve the underlying issue and may complicate recovery.

Troubleshoot the database connection first.


Recommended Troubleshooting Order

For most WordPress websites, use this sequence:

  1. Confirm whether the error is temporary or hosting-wide.
  2. Check both the website and /wp-admin/.
  3. Back up accessible files and database data.
  4. Verify DB_NAME.
  5. Verify DB_USER.
  6. Verify DB_PASSWORD.
  7. Verify DB_HOST.
  8. Confirm that the database user has appropriate permissions.
  9. Check whether MySQL or MariaDB is running.
  10. Check hosting resource and disk-space limits.
  11. Investigate possible database corruption.
  12. Review server and PHP logs.
  13. Review recent migrations or configuration changes.
  14. Contact your hosting provider if the server-side cause remains unclear.

This order helps eliminate the most common causes before moving to advanced troubleshooting.


What to Ask Your Hosting Provider

If you cannot identify the problem yourself, contact your hosting provider and give them specific information.

You can ask:

My WordPress website is showing “Error Establishing a Database Connection.” Could you check whether the MySQL/MariaDB server is running, whether my database is reachable, whether the database user has the required permissions, and whether my hosting account has reached any database connection or resource limits?

This gives the support team useful areas to investigate without requiring you to share passwords.

Never send your database password through an insecure public support channel.


How to Prevent Database Connection Errors

Not every database failure can be prevented, but good website maintenance can reduce the risk.

Keep Regular Backups

Maintain backups of both your database and website files.

A backup is especially important before:

  • WordPress updates
  • Plugin updates
  • Server migrations
  • Database changes
  • PHP upgrades
  • Major configuration changes

Use Reliable Hosting

A stable hosting environment should provide adequate database resources, monitoring, backups, and technical support.

A growing website may eventually need more resources than a basic hosting plan provides.

Monitor Website Performance

Watch for:

  • Slow database queries
  • Sudden traffic increases
  • High CPU usage
  • Excessive memory consumption
  • Low disk space
  • Large database tables
  • Repeated server errors

Early detection can prevent a temporary performance issue from becoming a prolonged outage.

Keep WordPress Updated

Keep WordPress core, plugins, and themes updated when compatible updates are available.

Updates will not directly prevent every database outage, but maintaining supported software reduces the risk of bugs and security problems that can affect site stability.

Protect Database Credentials

Never publish your wp-config.php contents.

Database credentials should not appear in:

  • Public Git repositories
  • Screenshots
  • Blog tutorials
  • Social media posts
  • Public support forums

If credentials are accidentally exposed, change them promptly and update the website configuration.


Frequently Asked Questions

Why does WordPress say “Error Establishing a Database Connection”?

It means WordPress cannot successfully communicate with the database it needs. Common causes include incorrect credentials, an unavailable database server, resource limits, configuration problems, or database corruption.

Does this error mean my website has been deleted?

Usually not. Your files and database may still exist. WordPress simply cannot access the database correctly at that moment.

What is the most common thing I should check first?

Check whether the database service is available and verify the database information stored in wp-config.php, particularly DB_NAME, DB_USER, DB_PASSWORD, and DB_HOST.

Where is the WordPress database password stored?

WordPress normally stores the database password in the DB_PASSWORD setting inside wp-config.php.

Is DB_HOST always localhost?

No. Many servers use localhost, but hosting configurations vary. Always use the database hostname specified by your hosting provider.

Can a wrong database password cause this error?

Yes. If DB_PASSWORD does not match the password assigned to the database user, WordPress cannot authenticate successfully.

Can high traffic cause a database connection error?

Yes. A heavily loaded server may reach connection, memory, CPU, or other resource limits. This is particularly relevant when the error occurs intermittently during traffic spikes.

Can I repair the WordPress database?

WordPress provides a database repair facility that can be temporarily enabled using:

define( 'WP_ALLOW_REPAIR', true );

Remove the setting after the repair process is complete.

Should I reinstall WordPress to fix the error?

Usually not. First identify whether the problem involves credentials, the database service, corruption, permissions, hosting resources, or configuration.

Can I lose my website because of this error?

The error itself does not necessarily mean data has been lost. However, database corruption, server failure, or incorrect manual changes can potentially affect data. This is why reliable backups are important.


Final Thoughts

The “Error Establishing a Database Connection” message looks serious because it can make an entire WordPress website unavailable. However, it is often a connection or configuration problem rather than permanent data loss.

Start with the simplest checks. Confirm that the database server is available and verify the four important settings in wp-config.php:

DB_NAME
DB_USER
DB_PASSWORD
DB_HOST

If those values are correct, investigate database-user permissions, server status, database corruption, resource limits, available disk space, logs, and recent website migrations or configuration changes.

Avoid making many changes at once. Change or test one thing at a time so that you can identify the actual cause.

Most importantly, maintain regular backups. A current backup makes WordPress troubleshooting considerably safer and gives you a recovery option if a database or server problem becomes more serious.

Leave a Reply

Scroll to Top

Discover more from VastPedia

Subscribe now to keep reading and get access to the full archive.

Continue reading