Fix error establishing a database connection
Work through the three checks that clear a WordPress database connection error: the wp-config.php values, the cPanel database user, and a table repair.
When a WordPress site shows "Error establishing a database connection", WordPress itself is running but cannot reach its database. The cause is usually one of two things: the database details saved in wp-config.php no longer match the database user in cPanel, or a table needs repairing. This guide works through the checks in order, starting with the least disruptive.
You will need to log in to cPanel for all three sections. Take a backup of the site's files and database first, so you can put a value back if you change the wrong one.
Read the database details in wp-config.php
- In cPanel, open File Manager.
- Open the folder that holds the site. For your main domain that is usually
public_html. For an addon domain or a subdomain it is the folder you chose when you added it, which you can confirm in the Domains area of cPanel. - Select
wp-config.php, then choose Edit from the toolbar. If an encoding prompt appears, continue past it. - Note the four values near the top of the file:
DB_NAME,DB_USER,DB_PASSWORDandDB_HOST.
On a cPanel server, database and user names normally begin with your cPanel username followed by an underscore. DB_HOST is usually localhost. If yours is set to something else, leave it alone for now, since some setups do use a different host. WordPress documents this file at Editing wp-config.php.
Check the database and user in cPanel
- Return to the cPanel home screen and open MySQL Databases.
- Under Current Databases, confirm a database exists with exactly the name in
DB_NAME. A missing database, or one letter out of place, produces this error on its own. - Under Current Users, confirm the user from
DB_USERis listed. - Still under Current Databases, check that the user appears beside that database in the privileged users column. If it does not, scroll to Add a User to a Database, pair the two, and grant ALL PRIVILEGES.
- If you cannot confirm the password, set a new one under Current Users, then edit
wp-config.phpagain and paste the same password intoDB_PASSWORD. Save the file.
Load the site again. If the error has cleared, you are finished.
Repair the tables in phpMyAdmin
- From the cPanel home screen, open phpMyAdmin.
- Select the site's database in the left-hand list.
- At the foot of the table list, select Check All.
- Open the With Selected menu, choose Repair Table, and wait for the result screen.
- Repeat with Check Table if you want to see which tables reported a problem.
Load the site again.
If it didn't work
- You edited the wrong copy. Staging folders and older installs keep their own
wp-config.php. Confirm the document root for the domain in the Domains area of cPanel, then check the file in that folder instead. - The error comes and goes rather than appearing every time. That pattern points at the server or at an account hitting a resource limit, not at your credentials. Stop changing settings and send us a ticket.
- Something else still holds the old details, such as an object cache drop-in file in
wp-contentor a caching plugin's configuration left behind by a move from your previous host.
If the site still shows the error after these checks, open a ticket at /support with the domain name and roughly when the error started, and we will check the server side for you.
More in WordPress
Stuck partway through? Support is answered by the engineers who run the servers, around the clock.