How to fix "Error establishing a database connection" in WordPress
If your site shows a blank page with the message "Error establishing a database connection", PHP is running but your website cannot talk to its MySQL database. This guide shows you how to fix "Error establishing a database connection" on cPanel hosting, step by step. It is written for WordPress, but the same checks apply to any PHP site that uses MySQL.
Why this error happens
- Wrong login details in
wp-config.php(database name, user, password or host). This is the most common cause, especially right after moving a site. - The database user is not linked to the database, or it has no privileges.
- Corrupted tables after a crash or a failed update.
- The database was deleted or never imported.
- The account hit its resource limits, or the MySQL service is briefly unavailable.
Step 1: Check the details in wp-config.php
- Log in to cPanel (see how to log in to cPanel) and open File Manager.
- Go to your site folder (usually
public_html), right-clickwp-config.phpand choose Edit. - Find these four lines and note the values:
define( 'DB_NAME', 'cpuser_wp123' );
define( 'DB_USER', 'cpuser_wpuser' );
define( 'DB_PASSWORD', 'your-password' );
define( 'DB_HOST', 'localhost' );
On cPanel hosting, the database name and user always start with your cPanel username and an underscore, and DB_HOST is normally localhost. If you copied the file from another host, these values are often still the old ones.
Step 2: Compare them with cPanel
- In cPanel open MySQL Databases.
- Under Current Databases, make sure the exact name from
DB_NAMEexists and that the user fromDB_USERappears next to it under Privileged Users. - If the user is missing, use Add User To Database, choose the user and the database, then tick ALL PRIVILEGES and save.
- If you are not sure about the password, find the user under Current Users, click Change Password, set a new strong password and put the same password in
DB_PASSWORD.
wp-config.php is enough to cause this error. If the password contains a single quote ', choose a different one.For a full walkthrough of databases and users, see how to create a MySQL database and user in cPanel.
Step 3: Make sure the database has data
- Open phpMyAdmin from cPanel and click your database on the left.
- You should see the WordPress tables, such as
wp_optionsandwp_posts. If the database is empty, the import did not finish. Import your.sqlfile again, or restore it from a backup (see how to back up and restore your website). - Check that the table prefix matches
$table_prefixinwp-config.php. If the tables start withwpab_, the line must say$table_prefix = 'wpab_';.
Step 4: Repair corrupted tables
If the details are correct but the error remains, or the admin area says that a table "needs repair":
- In phpMyAdmin, select your database, tick Check all under the table list, then choose Repair table from the drop-down menu.
- Or add this line to
wp-config.phpjust above the line "That's all, stop editing!":
define( 'WP_ALLOW_REPAIR', true );
Then visit https://yourdomain.com/wp-admin/maint/repair.php and click Repair Database. Remove the line as soon as you finish, because this page works without logging in.
Step 5: Test the connection directly (optional)
To be sure the user and password work, create a temporary file called dbtest.php in public_html with your real values:
<?php
$c = @new mysqli('localhost', 'cpuser_wpuser', 'your-password', 'cpuser_wp123');
echo $c->connect_error ? 'Failed: ' . $c->connect_error : 'Connected OK';
Open https://yourdomain.com/dbtest.php in your browser. "Access denied" means the user or password is wrong. "Unknown database" means the database name is wrong. Delete the file right after the test, because it contains your password.
Common problems
- The error appears only at busy times: the account may be hitting its CPU or entry-process limits. Check Resource Usage in cPanel and contact us if you see faults.
- The site works but the admin area shows the error: this is usually table corruption. Repair the tables as in Step 4.
- It started after a migration: the new
wp-config.phpstill has the old host's database name or prefix.
Frequently asked questions
Will I lose my content?
Usually not. In most cases the data is still in the database and only the connection details are wrong. Take a backup before you repair anything.
Should DB_HOST be my domain name or an IP address?
On EgWeb shared hosting, use localhost unless our support team tells you otherwise.
The error is still there after all the steps. What now?
Open a ticket with your domain name and the time you saw the error. We can check the MySQL service and the server logs for you.