Repair

How to Fix ‘Error Establishing a Database Connection’ in WordPress

Illustration of a concerned website owner troubleshooting a WordPress database connection error, with server, database and configuration checks.

Seeing "Error establishing a database connection" on your WordPress website can be alarming, especially if your site and admin dashboard have both stopped working.

This message means WordPress cannot establish the database connection it needs to load your website. The cause might be incorrect database credentials, a temporary hosting problem, an overloaded database server or, less commonly, database corruption.

The good news is that the error does not necessarily mean your content has been lost.

In this guide, we’ll explain what causes WordPress database connection errors and show you how to diagnose them safely, starting with the simplest checks before moving on to more technical fixes.

What does ‘Error Establishing a Database Connection’ mean?

WordPress uses PHP to run your website and a MySQL or MariaDB database to store information such as posts, pages, settings, user accounts and comments.

When someone visits your website, WordPress connects to the database to retrieve the information needed to display the requested page.

If WordPress cannot establish that connection, it may display:

Error establishing a database connection

You might see the message on your homepage, individual pages or inside /wp-admin/.

Sometimes the error affects the entire website. In other cases, it appears intermittently or only when the server is under heavy load.

The message tells you that WordPress has a database connectivity problem. It does not, by itself, tell you whether the database is unavailable, the credentials are incorrect or a database table is damaged.

That distinction matters because each problem requires a different solution.

What causes a WordPress database connection error?

The most common causes include:

  • Incorrect database credentials: The database name, username, password or hostname in wp-config.php does not match your hosting configuration.
  • Database server downtime: MySQL or MariaDB is stopped, unavailable or experiencing a hosting-related problem.
  • Resource limits: The database server has reached a connection limit, run out of available resources or encountered a storage issue.
  • Database user permissions: The configured user no longer has appropriate access to the database.
  • Database corruption: One or more tables are damaged, preventing WordPress from accessing the information it needs.

Database connection problems can also occur after migrating a website, changing hosting providers or updating database credentials.

Although the error might appear shortly after installing a plugin or updating WordPress, that timing alone does not prove the update caused it.

The safest approach is to identify what has actually failed before changing any files or database settings.

First checks before changing anything

Before attempting a repair, establish whether the error is temporary or persistent.

Check which parts of your website are affected

Open your website in a private browser window and check:

  1. Your homepage.
  2. Another page or blog post.
  3. Your WordPress admin login at /wp-admin/.

If the same error appears everywhere, the database connection is probably failing at a site-wide level.

If the homepage works but the dashboard does not, bear in mind that your homepage might be served from a cache. A cached page can continue to display even when WordPress cannot reach its database.

Check for a wider hosting problem

Look at your hosting provider’s status page or dashboard.

If several websites on the same hosting account are affected, the problem may be with the database service rather than WordPress itself.

For an intermittent error, record when it occurs and whether it coincides with high traffic, scheduled tasks or other server activity.

Think about recent changes

Consider whether you’ve recently:

  • Migrated your website or changed hosting providers.
  • Changed the database password or database user.
  • Edited wp-config.php.
  • Restored a backup.
  • Experienced a hosting outage or storage warning.

These details can help narrow down the cause.

Before making changes: Check that you have a recent, usable backup of both your database and website files. Avoid repeatedly editing settings, reinstalling WordPress or restoring backups without first identifying the likely cause.

1. Check your hosting provider and database server

If your database connection suddenly fails and you haven’t changed anything, checking your hosting environment is a sensible first step.

Sign in to your hosting dashboard and look for:

  • Any reported database or server outages.
  • Whether the MySQL or MariaDB service is running.
  • Database storage or disk space limits.
  • Resource usage warnings.
  • Recent hosting maintenance or account changes.

Some managed WordPress hosts handle these checks for you, while VPS and dedicated server users may have access to additional monitoring and logs.

If you’re unsure, contact your host and explain that WordPress is displaying an error establishing a database connection.

Ask them to confirm whether the database server is available, accepting connections and operating within its resource limits.

If the database server is down, changing WordPress plugins, themes or core files will not resolve the underlying problem. The service needs to recover or be restored by whoever manages it.

Once hosting availability has been confirmed, continue with the WordPress configuration checks.

2. Verify your database settings in wp-config.php

WordPress stores its database connection settings in a file named wp-config.php.

You can normally access this file through your hosting provider’s file manager or a secure SFTP or SSH connection.

It is generally located in your WordPress installation directory, although some configurations place it one directory above.

Find the database credentials

Open wp-config.php and look for settings similar to these:

define( 'DB_NAME', 'example_database' );
define( 'DB_USER', 'example_user' );
define( 'DB_PASSWORD', 'example_password' );
define( 'DB_HOST', 'localhost' );

The example values above are placeholders. Your website will have its own settings.

Each value has a specific purpose:

  • DB_NAME identifies the database WordPress should use.
  • DB_USER identifies the database account.
  • DB_PASSWORD contains that account’s password.
  • DB_HOST identifies the database server.

Check these settings against the details supplied by your hosting provider.

Check the database hostname carefully

A common mistake is assuming that DB_HOST must be set to localhost.

That works on many hosting environments, but others use a separate database hostname, a custom port or a Unix socket.

For example, your host might supply a hostname such as mysql.example.com, or a hostname with an additional port number.

Use the exact connection details provided by your host rather than guessing.

What if the settings don’t match?

If you’ve found a confirmed mismatch, create a backup of wp-config.php before correcting it.

Change only the incorrect value, save the file and test your website again.

Do not replace the configuration with credentials from another installation or reset database passwords without understanding which applications use the same account.

Security note: Never post your database password in a public support forum, screenshot or troubleshooting log. The wp-config.php file contains sensitive information and should remain protected.

For further details, see the official WordPress guide to editing wp-config.php.

3. Check database availability and user permissions

If the settings in wp-config.php appear correct, the next step is to confirm that the configured database and user account are available.

Your hosting control panel might provide a section called Databases, MySQL Databases or something similar.

Look for the database name shown in DB_NAME and check whether the user from DB_USER has access to it.

After a migration or account change, it’s possible for the database to exist while the WordPress user no longer has the required permissions.

Check for common database errors

Your hosting provider may be able to identify the cause from the database logs.

For example:

Access denied for user

This usually points towards incorrect credentials, an account restriction or a database permission issue.

Unknown database

The configured database name might be wrong, or the database may no longer exist on the specified server.

Connection refused

The database service may be stopped, unavailable at the specified address or blocked by a network configuration.

These messages offer more specific information than the generic WordPress error.

Can you check the database using phpMyAdmin?

If your host provides phpMyAdmin, you may be able to confirm that the expected database and WordPress tables exist.

However, being able to open phpMyAdmin does not prove WordPress can connect using the credentials in wp-config.php. Your hosting dashboard might use a different database account.

Avoid changing tables, deleting records or creating replacement users simply because you can access phpMyAdmin.

If the database user is missing or its permissions are incorrect, ask your hosting provider to restore the appropriate access.

4. Investigate database server limits and connection failures

A WordPress database connection error isn’t always caused by incorrect credentials.

Sometimes the database is running, but it cannot accept or maintain connections reliably.

This is particularly relevant when errors appear intermittently rather than continuously.

Too many database connections

Database servers have limits on how many connections they can handle simultaneously.

A sudden traffic spike, inefficient database queries or excessive background processes can contribute to connection exhaustion.

When a server reaches its limit, WordPress may be unable to establish a new connection.

Your hosting provider can check whether the database is reporting messages such as Too many connections.

Insufficient memory or storage

A database server may also become unavailable if it runs out of memory or disk space.

These problems can affect the database service even when your website files appear intact.

Check the hosting dashboard for storage warnings, memory pressure and service restarts.

Slow queries and recurring server load

If the problem repeatedly occurs during busy periods, investigate the underlying workload.

Potential contributors include poorly optimised queries, large database tables and resource-intensive scheduled tasks.

An inefficient plugin can contribute to database overload, but disabling every WordPress plugin is not the appropriate first response to a database connection error.

Start with server metrics and logs. Only isolate plugins or other application code when the evidence points towards a particular process.

If your hosting resources are consistently exhausted, resolving the issue may require query optimisation, workload changes or more suitable hosting.

5. Check for database corruption and repair only when necessary

WordPress includes a database repair facility, but it should not be the first troubleshooting step.

A connection error does not automatically mean the database is corrupted.

Database repair is more appropriate when a hosting diagnostic, database check or specific error message indicates that tables are damaged.

Before attempting a repair, confirm you have a recoverable database backup. Some repairs can fail, and the available repair methods depend on the database storage engine.

How to enable WordPress database repair

If corruption has been identified and a repair is appropriate, open wp-config.php and add:

define( 'WP_ALLOW_REPAIR', true );

Save the file, then visit:

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

Replace yourwebsite.com with your own domain.

WordPress may offer options to repair the database or repair and optimise it.

Start with the repair option if it is appropriate for the identified problem. Database optimisation is a separate operation and is not necessary simply because your website has displayed a connection error.

Follow the results carefully. Not all database engines or forms of corruption can be repaired using this facility.

Disable repair mode immediately afterwards

Once the repair attempt is complete, remove:

define( 'WP_ALLOW_REPAIR', true );

from wp-config.php.

This is an essential step because WordPress allows access to its repair facility without requiring a logged-in administrator while that setting is enabled.

If the repair fails, don’t repeatedly run it or attempt more invasive database operations without understanding the error.

Your hosting provider or WordPress support specialist may need to investigate the database engine, inspect the affected tables or recover the database from a verified backup.

6. Advanced troubleshooting with WP-CLI and server logs

If you’re comfortable using the command line and have suitable hosting access, WP-CLI can help you investigate the database without relying on the WordPress dashboard.

These steps are intended for technically confident users.

Test database connectivity using WP-CLI

From the WordPress installation directory, run:

wp db query 'SELECT 1;'

This sends a simple read-only query using the database credentials defined in wp-config.php.

If it succeeds, the connection is working at the time of the test.

If it fails, the error may help distinguish between incorrect credentials, an unavailable server and other connection problems.

WP-CLI relies on the appropriate database command-line tools being installed and available on the server.

Check database table status

Once database connectivity is available, you can run:

wp db check

This checks the status of database tables using the configured credentials.

It does not repair damaged tables or solve an unavailable database server.

A failed check should be investigated before attempting any repair operation.

See the official WP-CLI database check documentation for details.

Review server error logs

When the database connection fails, server logs can reveal information that WordPress cannot display in the browser.

Depending on your hosting setup, useful sources include:

  • MySQL or MariaDB service logs.
  • PHP error logs.
  • Web server error logs.
  • Hosting resource and monitoring logs.

Look for errors that occur at the same time as the website outage.

Repeated connection failures, authentication errors, database service restarts or messages about exhausted connections can help identify the actual cause.

WordPress also provides debugging tools, but they may not capture every problem that occurs before the database connection is established.

If you enable WordPress debug logging, keep errors hidden from public visitors, protect the log file from unauthorised access and disable temporary debugging when the investigation is complete.

The official WordPress debugging documentation explains the available settings.

7. What to do if the database connection error persists

If you’ve checked the hosting environment, verified the credentials and reviewed the available logs without finding the cause, avoid making further speculative changes.

At this stage, gather the information a hosting provider or WordPress specialist needs:

  • The exact error message displayed.
  • When the problem first occurred.
  • Whether it affects the entire site or happens intermittently.
  • Any recent migrations, configuration changes or outages.
  • Relevant database and server error messages.
  • Which checks you’ve already completed.

This can significantly reduce the time spent repeating basic troubleshooting.

Should you restore a backup?

Restoring a backup may be appropriate if database damage or data loss has been confirmed and you have a known-good recovery point.

However, restoring an older database will usually overwrite changes made since that backup.

For a WooCommerce website, this could include recent orders, customer information and other transactions.

Do not restore the database simply to see whether the error disappears. Identify the likely cause and understand what data would be affected before proceeding.

Check the website after recovery

Once the database connection is working again, verify more than the homepage.

Check that:

  • The WordPress dashboard loads correctly.
  • Pages and posts display as expected.
  • Contact forms and other essential functions work.
  • WooCommerce checkout and orders work, if applicable.
  • Error logs are no longer reporting recurring database failures.

If caching is enabled, check an uncached page or authenticated admin area to ensure the website is genuinely functioning rather than showing an older cached response.

Continue monitoring the website for recurring errors, especially if the original problem was intermittent.

8. How to prevent WordPress database connection errors

Not every database outage can be prevented, but good maintenance and monitoring can reduce the risk of recurring problems.

Maintain reliable backups

Keep automated backups of your website files and database, store suitable copies away from the production server and periodically test your recovery process.

The appropriate backup frequency depends on how often your website changes.

A busy WooCommerce store will generally need a more frequent backup schedule than a relatively static brochure website.

Monitor uptime and server resources

Uptime monitoring can alert you when a website becomes unavailable.

Server monitoring is equally important because a database might experience resource problems before the entire website goes offline.

Track database availability, storage capacity and recurring server errors where your hosting platform permits it.

Keep WordPress and its components maintained

Regular WordPress, plugin and theme updates help keep your website secure and compatible.

Use a staging environment for significant changes where practical, and take appropriate backups before updates or migrations.

Maintenance alone cannot prevent every hosting outage, but it can help you identify emerging problems before they become more serious.

For more information, read our guide to why ongoing WordPress maintenance is needed.

Need help fixing a WordPress database connection error?

If your website is still displaying "Error establishing a database connection", the next step is to identify whether the problem lies with your hosting, database configuration or database itself.

Avoid repeatedly changing credentials, repairing tables or restoring backups without a confirmed diagnosis.

At Newt Labs, we help website owners investigate and resolve WordPress problems, including database connection failures.

Our one-off WordPress website repair service can help you get to the root of the issue and restore your website safely.

You can also explore our guide to common WordPress errors and how to fix them for help with other website problems.

About the author

Steven Watts

Steven is the founder of Newt Labs and a WordPress specialist with more than 15 years of experience. Since 2010, he has been helping businesses keep their WordPress websites secure, fast, reliable and well supported, with a focus on practical advice and long-term website care.

Related advice

More Repair articles

Need a clearer next step?

Get practical help with your WordPress website

You can start with one issue, a free audit, or an ongoing care plan depending on what your website needs.

WordPress support illustration.