Free migrationUK web hosting from £2.99 a month, with backups every six hours and a 60-day money-back guarantee.UK web hosting from £2.99 a month.See hosting plans

How to fix "Error establishing a database connection" in WordPress

On this page

This guide explains how to fix the “Error establishing a database connection” message in WordPress. It means WordPress can’t log in to its MySQL database, usually because a detail in wp-config.php is wrong.

Check the details in wp-config.php

  1. Log in to cPanel and open File Manager.
  2. Go to your WordPress folder, usually public_html.
  3. Right-click wp-config.php and choose View or Edit.
  4. Find these four lines and note the values:
    • DB_NAME, the database name
    • DB_USER, the database username
    • DB_PASSWORD, the password for that user
    • DB_HOST, which should be localhost on our hosting
  5. On cPanel, database names and usernames start with your cPanel username and an underscore, for example abcd_wp123. Check the values include this part.

Compare them with cPanel

  1. In cPanel, open MySQL Databases.
  2. Under Current Databases, check the database named in DB_NAME exists, spelled exactly the same.
  3. Under Current Users, check the user named in DB_USER exists.
  4. In the Current Databases table, check that user is listed next to the database under Privileged Users.

If the user isn’t linked to the database, scroll to Add User To Database, pick the user and the database, click Add, then tick ALL PRIVILEGES and click Make Changes.

Reset the database password

If everything matches but it still won’t connect, the password is probably wrong.

  1. In MySQL Databases, under Current Users, click Change Password next to the user.
  2. Set a new password and click Change Password.
  3. Edit wp-config.php and put the same password between the quote marks on the DB_PASSWORD line.
  4. Save the file and reload your site.

For more on databases and users in cPanel, see how to create a MySQL database.

Repair the database tables

If the error only appears on some pages, or the dashboard says tables need repairing, a table may be damaged.

  1. In MySQL Databases, find Modify Databases.
  2. Under Repair Database, choose your database and click Repair Database.
  3. Reload your site.

You can also repair tables in phpMyAdmin. Select the database, tick the tables, and choose Repair table from the drop-down at the bottom.

If it doesn’t work

  • The database isn’t in the list at all. It may have been deleted. Restore it from a backup. See how to restore a backup with JetBackup.
  • You’ve just moved the site to us. The details in wp-config.php are probably still the old host’s. Update them to match the database on your new account.
  • The error comes and goes. Note the times it happens and send them to us in a ticket.

Still stuck? Open a support ticket and we’ll help.

Still stuck? Open a support ticket from your client area. Tell us your domain and which step went wrong, and we'll take it from there.
WordPress hosting from £2.99 a month

WP Toolkit, staging, backups every six hours and free migration of your site.

See WordPress hosting
Open in new tab