Fixing "Error Establishing a Database Connection"

Updated: 4 December 20264 min read11 viewsMarkdown

"Error establishing a database connection" means WordPress can't reach the MySQL database that holds your content. Most of the time the cause is a single wrong value in wp-config.php, and it takes a few minutes to fix.

A broken link between a browser window showing a warning sign and a database cylinder, with a config file card and a gear below
The config file connects the site to its database, and that's usually where the problem is.

Common causes

  • Wrong connection details: wp-config.php wasn't updated after a migration or password change.
  • User not linked to the database: the database user exists but was never added to the database with privileges.
  • Corrupted tables: an interrupted operation or a plugin bug damaged the tables.
  • Resource limits: during a traffic spike the account hits its limits and can't open new connections.
  • Server-side issue: rarely, a problem with the MySQL service itself.

Before you start

  • Your cPanel login (in the activation e-mail or under Client Area > My Services in the service details).
  • Download a copy of wp-config.php before you change anything.

Step-by-step fix

  1. Check the connection details. In cPanel open wp-config.php in your site folder with File Manager and look at these four lines:
define( 'DB_NAME', 'cpuser_wp' );
define( 'DB_USER', 'cpuser_wpuser' );
define( 'DB_PASSWORD', '********' );
define( 'DB_HOST', 'localhost' );
  1. Compare the names with cPanel. On the MySQL Databases page, check the database and user names match exactly. In cPanel they carry a prefix starting with your account username (e.g. cpuser_). Forgetting the prefix is the most common mistake.
  2. Check privileges. Under Add User To Database on the same page, add the user to the database and select ALL PRIVILEGES. Skip this if it's already linked.
  3. Reset the password. If you aren't sure it's right, use Change Password for the user under Current Users and put the same password in the DB_PASSWORD line.
  4. Check DB_HOST. On our hosting accounts the database is on the same server, so the value should be localhost. Replace any custom host left over from your previous provider.
  5. Save the file and reload your site.
Three-step flow: check the lines in the config file, link the user to the database in the panel, the site loads again
1. Check wp-config, 2. confirm the user and privileges, 3. reload the site.

If the details are correct: repair the tables

If the details are right but the error persists, or the admin area says one or more tables are unavailable, the tables may be corrupted.

  • In cPanel, use Repair Database on the MySQL Databases page.
  • Or temporarily add define( 'WP_ALLOW_REPAIR', true ); to wp-config.php and open https://example.com/wp-admin/maint/repair.php. Remove the line as soon as you're done, otherwise the repair page stays open to everyone.
Careful

Export a database backup from phpMyAdmin before repairing. For serious corruption, restoring the last good backup is the safest option.

If the error comes and goes

If it only appears at busy times, you're most likely hitting resource limits. cPanel's Resource Usage screen shows when limits were reached. Enabling a cache plugin, removing heavy plugins and limiting bot traffic is usually enough. If the load is constant, consider upgrading your plan.

Troubleshooting summary

SymptomLikely causeFix
Error right after a migrationwp-config has old detailsEnter the new database name, user and password.
Error after a password changeDB_PASSWORD not updatedWrite the new password into wp-config.
Table warning only in the admin areaCorrupted tableRepair with Repair Database or WP_ALLOW_REPAIR.
Intermittent errorResource limitsUse caching and check Resource Usage.
Every site on the account fails at oncePossibly a server-side issueOpen a support ticket.

Frequently asked questions

Does this error mean my data is gone?

No. It means WordPress can't connect; the data is usually still there. Once the connection is fixed, the site loads as before.

Where do I find my database name?

It's on the DB_NAME line in wp-config.php. Compare it with the list on cPanel's MySQL Databases page.

What if the error doesn't go away?

Open a support ticket from the Client Area and list the steps you've already checked. Our team will look at the server side.

See our cPanel guides for related issues or contact us. Plans built for WordPress are on the WordPress Hosting page.

Was this article helpful?
Thank you for your feedback.