“Error establishing a database connection” is one of the most alarming WordPress messages, because the whole site disappears behind it, front end and admin alike. It means WordPress loaded, but it could not log in to its MySQL or MariaDB database. Your posts and pages are usually still there; WordPress simply cannot reach them.
WordPress’s own common errors guide says the cause is usually a wrong setting in wp-config.php, and otherwise a problem on the database server. Below are the checks in the order we would make them on a cPanel account, from the most likely cause to the least.
Why it happens
- Wrong details in
wp-config.php: the database name, username, password or host does not match what exists on the server. This is the most common cause, especially right after a migration or a password change. - The user is not attached to the database: the user exists but has no privileges on that database.
- A damaged database table: WordPress connects, but a corrupted table stops it working.
- The database server is down or overloaded, or the account has hit a limit. This is one for your host.
- A hacked site whose
wp-config.phpwas changed. WordPress lists this as a possibility once the other causes are ruled out.
Step 1: Read the database settings in wp-config.php
In cPanel, open File Manager, go to the site’s folder (usually public_html) and open wp-config.php with the editor. Find these four lines:
define( 'DB_NAME', 'cpuser_wp123' );
define( 'DB_USER', 'cpuser_wpuser' );
define( 'DB_PASSWORD', 'your-database-password' );
define( 'DB_HOST', 'localhost' );
Write down the database name and the username. Do not change anything yet. On cPanel, DB_HOST is normally localhost, unless your host runs databases on a separate server.
Step 2: Compare them with Manage My Databases
Open Databases » Manage My Databases in cPanel (called MySQL Databases in older versions). cPanel’s documentation describes each section.
- In Current Databases, find the database named in
DB_NAME. Check the spelling exactly, including the prefix. - In the same row, check that the user from
DB_USERis listed under Privileged Users. - If the user is missing, go to Add a User to a Database, choose the user and the database, click Add, then tick ALL PRIVILEGES and click Make Changes.
- If the database or user does not exist at all, the site’s data may be in a database with a different name. Look through Current Databases before you create anything new.
The prefix trap after a migration
With database prefixing enabled, cPanel adds the account username and an underscore to every database and user name, such as cpuser_wp123. If a site moves to a cPanel account with a different username, the names change with it, and wp-config.php still points at the old ones. Update DB_NAME and DB_USER to the names shown in Manage My Databases.
Step 3: Reset the database user’s password
You cannot read an existing database password in cPanel, but you can set a new one:
- In Manage My Databases, under Current Users, click Change Password next to the user from
DB_USER. - Generate a strong password, copy it, and save it.
- Paste the same password into
DB_PASSWORDinwp-config.php, keeping the quotes, and save the file.
Reload the site. If the password was the problem, it comes straight back. If other apps use the same database user, update their settings too.
Step 4: Check and repair the database
If the details are right and the error remains, or WordPress says that one or more database tables are unavailable, a table may be damaged.
- In Manage My Databases, choose the database under Check Database and click Check Database. cPanel names any corrupt table.
- If it finds a problem, choose the database under Repair Database and click Repair Database.
WordPress also has its own repair page. Add this line to wp-config.php, above the line that says “That’s all, stop editing”:
define( 'WP_ALLOW_REPAIR', true );
Then open https://yourdomain.com/wp-admin/maint/repair.php and choose Repair Database. According to the wp-config.php documentation, this page works without logging in, because a damaged database often stops you logging in. That also means anyone can open it, so remove the line again as soon as you are done.
Step 5: Rule out the server
If all of the above checks out, the problem is probably not your site:
- If other sites on the same account also show the error, the database server itself may be down.
- If the error comes and goes, often at busy times, the database server may be overloaded, or the account may be reaching a resource limit.
- If
wp-config.phpcontains code you did not add, or new admin users have appeared, treat the site as hacked: change passwords and restore clean files from a backup taken before the change.
In each case, open a ticket with your host and include the domain and the time the error started.
Restore from a backup if nothing else works
If the database was damaged beyond repair, restore it from a backup taken before the problem began. Restore the database together with its database user, so the login matches. Our guide to restoring a backup with JetBackup 5 shows how to do that from cPanel.
Frequently asked questions
Did I lose my WordPress content?
Usually not. The message means WordPress cannot connect to the database. In most cases the content is still there and returns once the connection details are fixed.
Why did the error appear right after I moved my site?
The database name, user or password in wp-config.php usually does not match the new server, often because cPanel’s prefix changed with the new account username. Compare the names in Manage My Databases and update the file.
What should DB_HOST be on cPanel?
Normally localhost. If your host uses a separate database server, Manage My Databases shows a Remote Database Host section with the address to use.
Is the WordPress repair page safe to leave on?
No. With WP_ALLOW_REPAIR enabled, anyone can open the repair page without logging in. Remove the line from wp-config.php once the repair is done.
Can I create the database user in phpMyAdmin instead?
cPanel advises against it. Databases and users created in phpMyAdmin are not mapped to your account, which stops backups and restores working. Use Manage My Databases or the Database Wizard.
WordPress hosting that keeps a backup ready
Our WordPress hosting runs on LiteSpeed with daily JetBackup copies, so a damaged database can be restored from cPanel without waiting on a ticket. If your site is down with this error right now, open a ticket with the domain and we will look at it with you. Moving from another host? We will copy the site and set up the database for you with a free migration.
Tags:
Related articles
How to move a cPanel or WordPress site to a new host without downtime
Lower the DNS TTL, copy the site, test it on the new server, then change nameservers.
Read More →How to install WordPress on cPanel
The one-click installer in Softaculous, or a manual install with a database you create yourself.
Read More →



