How to fix "Error establishing a database connection" in WordPress
Help centreOn 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
- Log in to cPanel and open File Manager.
- Go to your WordPress folder, usually
public_html. - Right-click
wp-config.phpand choose View or Edit. - Find these four lines and note the values:
DB_NAME, the database nameDB_USER, the database usernameDB_PASSWORD, the password for that userDB_HOST, which should belocalhoston our hosting
- 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
- In cPanel, open MySQL Databases.
- Under Current Databases, check the database named in
DB_NAMEexists, spelled exactly the same. - Under Current Users, check the user named in
DB_USERexists. - 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.
- In MySQL Databases, under Current Users, click Change Password next to the user.
- Set a new password and click Change Password.
- Edit
wp-config.phpand put the same password between the quote marks on theDB_PASSWORDline. - 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.
- In MySQL Databases, find Modify Databases.
- Under Repair Database, choose your database and click Repair Database.
- 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.phpare 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.
WP Toolkit, staging, backups every six hours and free migration of your site.