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

The WordPress message “Error Establishing a Database Connection” appears when WordPress cannot communicate with the MySQL or MariaDB database that stores your posts, pages, settings, users, and orders. The website files may still be intact, but WordPress cannot build the page without database access.

The most common causes are incorrect database credentials, an unavailable database server, damaged tables, or a hosting resource limit. Follow the checks below in order. If possible, create a backup before changing configuration or repairing the database.

First, Confirm the Scope of the Problem

Visit the homepage, /wp-admin/, and a few other URLs. If every page displays the same message, the problem is likely connected to the database server, credentials, or hosting account. If only one page or feature fails, investigate plugins, themes, custom queries, and server resources as well.

Think about what changed shortly before the error appeared. A host migration, database password change, backup restoration, edit to wp-config.php, plugin update, or hosting resource limit can provide an important clue.

Check the Database Settings in wp-config.php

WordPress reads its database connection details from wp-config.php, usually located in the site’s document root. Open the file through cPanel File Manager, SFTP, or SSH and review these settings:

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

Compare the database name, username, and password with the values shown in your hosting control panel. They must match exactly. Check for accidental spaces, missing characters, incorrect capitalization, and altered quotation marks.

DB_HOST is often localhost, but that is not universal. Some hosts require a specific database hostname or port. Use the value provided by your hosting company, particularly if the account was recently migrated.

Keep wp-config.php private. It contains database credentials that could provide access to the site’s data.

Verify the Database User and Permissions

Open MySQL Databases in cPanel, or the equivalent tool supplied by your host. Confirm all of the following:

  • The database named in DB_NAME exists.
  • The user named in DB_USER exists.
  • The user is assigned to the correct database.
  • The user has the privileges WordPress needs, normally including SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER, INDEX, and DROP.

Shared hosts often add an account prefix to database names and usernames, such as accountname_wp1. If that prefix is missing from wp-config.php, WordPress may be trying to connect to a database or user that does not exist.

If you reset the database user’s password, replace DB_PASSWORD with the new value, save the file, and test the site. Change one setting at a time so you can identify which correction resolves the problem.

Check Whether the Database Server Is Available

When the credentials look correct, the database service may be down or unreachable. Check your hosting provider’s status page and ask support whether the MySQL or MariaDB server is operating normally. Also ask whether your account has reached a connection, CPU, memory, or disk-space limit.

On a VPS or dedicated server, an administrator can check the database service with commands such as:

sudo systemctl status mysql
sudo systemctl status mariadb

Only run service commands if you manage the server and understand its configuration. Restarting a database service can affect other websites and applications on the same machine.

The connection failure may be part of a broader hosting problem. If you are also seeing gateway or upstream errors, review this guide to fixing WordPress 502 Bad Gateway errors.

Repair Damaged Database Tables

If the database exists and the server is online, one or more tables may be damaged. Create a backup of the WordPress files and database before attempting repairs. This guide explains how to create and restore a WordPress backup using cPanel.

WordPress includes a temporary repair tool. Add this line to wp-config.php, above the line that says “That’s all, stop editing”:

define( 'WP_ALLOW_REPAIR', true );

Then visit https://example.com/wp-admin/maint/repair.php, replacing the example domain with your own. Choose Repair Database or Repair and Optimize Database.

Remove the WP_ALLOW_REPAIR line immediately after the repair finishes. The page does not require a normal WordPress login, so leaving it enabled exposes an unnecessary maintenance endpoint.

You can also inspect tables in phpMyAdmin. Select the correct database, review the table status, and use its repair option only after making a backup. If tables are missing or badly damaged, restore a known-good database backup or ask your host to investigate its available backups.

Review Hosting Resources and Recent Changes

Full disk space, too many database connections, memory pressure, or a sudden traffic increase can stop WordPress from connecting. Review resource graphs and error logs in your hosting panel. WooCommerce sites can generate substantial database activity from orders, sessions, scheduled actions, and product data.

If the problem began after a plugin installation or update, deactivate plugins without the dashboard by renaming wp-content/plugins to something such as plugins.disabled. If the site comes back, restore the original folder name and deactivate plugins individually from the dashboard to find the conflict.

You can use a similar file-based approach to switch themes by temporarily moving or renaming the active theme folder, provided a compatible default WordPress theme is available. For recurring resource errors, read the guide to fixing WordPress memory exhaustion. Increasing PHP memory will not fix incorrect credentials or a stopped database service, but it may help separate a memory problem from a connection problem.

Use Logs to Identify the Cause

Enable WordPress debugging temporarily, but keep errors hidden from visitors. Add or update these settings in wp-config.php:

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

Review wp-content/debug.log and the hosting error logs, then disable debugging after troubleshooting. Messages such as these point in different directions:

  • Access denied: check the username, password, and privileges.
  • Unknown database: check the database name and account prefix.
  • Too many connections: investigate hosting limits, traffic, and plugins.
  • Connection refused: check whether the database service or host is reachable.

When to Contact Your Hosting Provider

Contact support when the credentials are correct but the connection still fails, the database service is unavailable, tables have disappeared, or the account was recently migrated. Include your domain, the approximate time the problem began, recent changes, and the exact error message.

Do not send your database password through ordinary email or a public support ticket. Use your provider’s secure credential-sharing process if support needs temporary access.

FAQ

Will changing the WordPress URL fix this error?

Usually not. This message concerns database connectivity, not the site URL. Changing unrelated settings can make recovery more difficult.

Can I delete and recreate the database?

Do not do this unless you have a verified backup and a tested recovery plan. Deleting the database removes posts, users, settings, and potentially WooCommerce order data.

Why does the error appear and disappear?

Intermittent failures often indicate server overload, connection limits, an unstable database service, or a plugin generating excessive queries. Hosting logs and resource reports are particularly useful in this situation.

Conclusion

Start with the least destructive checks: confirm the values in wp-config.php, verify the database user and privileges, check server availability, and make a backup before repairing tables. Logs can help distinguish a credential mistake from a hosting outage, resource limit, or damaged database. After restoring the site, maintain reliable backups and monitor hosting resources so future failures are easier to diagnose.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top