All posts

Blog

How to Fix "Error Establishing a Database Connection" in WordPress

Illustrated cover for the WordPress guide "How to Fix "Error Establishing a Database Connection" in WordPress"

"Error establishing a database connection" means WordPress can't reach its database. Your posts, pages, settings and users all live in that database, so without it WordPress can't build a single page.

The good news: your content is almost always still there. Something is stopping WordPress from connecting to it. Here's how to find out what.

How WordPress connects to its database

WordPress reads four values from the wp-config.php file in your site's root folder:

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

If any of these is wrong, or the database user doesn't have permission, or the database server can't respond, you get this error.

Step 1: Check whether the admin area shows the same error

Visit yourdomain.com/wp-admin.

  • The same error: it's a connection problem. Continue below.
  • A different message, like "One or more database tables are unavailable": the database is reachable but may need repairing. Skip to Step 5.

Step 2: Did anything just change?

This error often appears right after:

  • Moving the site to a new host or folder
  • Changing the database password in your control panel
  • Restoring a backup that used different database details
  • Renaming the database or user

If any of these happened, the details in wp-config.php probably no longer match. Go to Step 3.

Step 3: Check the details in wp-config.php

  1. Log into cPanel or DirectAdmin and open File Manager.
  2. Open your site's folder (usually public_html) and edit wp-config.php.
  3. Note the values of DB_NAME, DB_USER and DB_HOST.

Now compare them with your control panel:

  • cPanel: open MySQL Databases. Check the database name and the user exist exactly as written. On cPanel, both usually start with your account username and an underscore, for example account_wp123.
  • DirectAdmin: open MySQL Management and check the database and its user.

For DB_HOST, localhost is correct on most shared hosting.

Step 4: Reset the database user's password

If the names match, the password is the likely culprit. You can't view existing database passwords, but you can set a new one:

  1. In cPanel > MySQL Databases, scroll to Current Users, click Change Password next to the user, and set a strong new password. In DirectAdmin > MySQL Management, open the database and change the user's password.
  2. In wp-config.php, replace the DB_PASSWORD value with the new password, keeping it inside the quotes.
  3. Save and reload your site.

Also check the user is assigned to the database

In cPanel > MySQL Databases, under Current Databases, check that your user is listed next to the database. If not, use Add User to Database further down, and grant All Privileges. This step is easy to miss after a migration.

Step 5: Repair the database

If WordPress connects but reports damaged tables:

  1. Add this line to wp-config.php, just above "That's all, stop editing!":
define( 'WP_ALLOW_REPAIR', true );
  1. Visit yourdomain.com/wp-admin/maint/repair.php and click Repair Database.
  2. Remove the line from wp-config.php when you're done. Anyone can access the repair page while it's enabled.

You can also repair tables in phpMyAdmin: select the database, tick all tables, and choose Repair table.

Step 6: Check whether the database server is busy

If your details are correct and the error comes and goes, the database server may be refusing connections because your site is overloaded. Signs include:

  • The error appears during traffic spikes
  • Your control panel shows you hitting CPU, memory or process limits
  • A bot is hammering your login page or search

Fixes:

Step 7: Check for a wider problem

If several sites on your account show the error at the same time, and you haven't changed anything, the problem may be on the server. Contact your host with your domain and when the error started.

On a VPS or dedicated server

If you manage your own server, also check that the database service is running:

systemctl status mariadb

(or mysql, depending on your setup). If it has stopped, check the logs before restarting it. Out-of-memory events are a common cause on small servers, so check whether the system has been killing processes. Our guide to first steps on a new Linux VPS covers basic monitoring.

How to prevent it

  • Update wp-config.php whenever you change database passwords.
  • Use caching to cut database load.
  • Keep regular backups, including the database.
  • Get help with migrations. If your new host offers a migration service, use it, so the database is set up correctly.

The bottom line

In most cases, this error comes down to a mismatch between wp-config.php and your actual database name, user or password. Check those first, make sure the user is attached to the database, and look at server load if the error is intermittent. Your content is almost always safe, waiting for WordPress to reconnect.