How to Fix “Error Establishing a Database Connection” in WordPress
Fix the WordPress “Error establishing a database connection” step by step: check wp-config.php, database user privileges, the database server and table repair.

Quick answer: “Error establishing a database connection” means WordPress cannot log in to its MySQL database. In most cases the database name, username, password or host in wp-config.php no longer match what is set in your hosting panel. Check those four values first, then check that the database user still has privileges, and finally repair the database or contact your host.
When this message appears, the whole site, including wp-admin, shows a blank page with that one line. Your content is almost never lost: it is still in the database, WordPress just cannot reach it. Work through the causes below in order, starting with the most common.
What causes the error
| Cause | How common | Typical trigger |
|---|---|---|
Wrong details in wp-config.php |
Very common | Moving the site, changing the database password, restoring a backup |
| Database user lost its privileges or was deleted | Common | Cleaning up users in cPanel, migration |
| Database server is down or overloaded | Occasional | Traffic spike, host maintenance, shared server problems |
| Corrupted database tables | Less common | Server crash during a write, disk full |
Step 1: Check whether wp-admin shows a different message
Open yourdomain.com/wp-admin. If the front end shows the error but wp-admin says something like “One or more database tables are unavailable”, the tables are probably damaged and you can jump to Step 5. If both show the same error, continue with Step 2.
Step 2: Check the database details in wp-config.php
In cPanel, open File Manager, go to your site’s folder (often public_html) and edit wp-config.php. Find these four lines:
define( 'DB_NAME', 'example_wp123' );
define( 'DB_USER', 'example_wpuser' );
define( 'DB_PASSWORD', 'your-password' );
define( 'DB_HOST', 'localhost' );
Now open MySQL Databases in cPanel and compare:
- DB_NAME must exactly match a database listed under “Current Databases”, including the account prefix (for example
example_). - DB_USER must exactly match a user listed under “Current Users”.
- DB_HOST is
localhoston most cPanel hosts. Some hosts use a separate database server name; their help pages will show it. - DB_PASSWORD cannot be viewed in cPanel. If you are not sure it is right, set a new password for the user in MySQL Databases and paste the same password into
wp-config.php.
Watch for invisible mistakes: an extra space inside the quotes, a curly quote (’) instead of a straight quote (‘), or a missing semicolon at the end of the line will all stop the connection.
Step 3: Check the user is added to the database with all privileges
In MySQL Databases, scroll to the “Current Databases” table and look at the Privileged Users column for your database. If your user is not listed:
Add the user
Under “Add User to Database”, choose your user and your database, then click Add.
Grant privileges
On the next screen tick ALL PRIVILEGES and click Make Changes.
Reload the site
Open your site in a private window. If the details were right, it should load now.
Step 4: Check whether the database server is running
Open phpMyAdmin from cPanel. If phpMyAdmin itself cannot connect, or other sites on the same hosting account show the same error, the problem is on the server side. Check your host’s status page and open a support ticket; there is nothing to fix inside WordPress. If your site suddenly gets much more traffic than usual, ask the host whether you hit a connection limit on your plan.
Step 5: Repair the database
WordPress has a built-in repair tool. To use it, add this line to wp-config.php, above “That’s all, stop editing!”:
define( 'WP_ALLOW_REPAIR', true );
Then visit yourdomain.com/wp-admin/maint/repair.php and click Repair Database. When it finishes, remove the line again. While it is there, anyone can open the repair page without logging in.
You can also repair from phpMyAdmin: select the database, tick all tables, and choose Repair table from the “With selected” menu.
Step 6: After a migration, check the site address
If the error appeared right after moving the site to a new host or domain, also check in phpMyAdmin that the siteurl and home rows in the wp_options table contain the correct address. The table prefix may be different from wp_; it is set by $table_prefix in wp-config.php, and it must match the tables that actually exist.
How to prevent it next time
- Keep automatic backups of the database, stored off the server.
- After changing a database password in cPanel, update
wp-config.phpat the same time. - Do not delete MySQL users in cPanel unless you have checked which site uses them.
- Keep an eye on disk space; a full disk can corrupt tables during writes.
Frequently asked questions
Have I lost my posts?
Almost certainly not. The error means WordPress cannot connect, not that the data is gone. You can confirm by opening phpMyAdmin and browsing the wp_posts table.
Why did the error appear without me changing anything?
Usually the database server was briefly overloaded or restarted by the host. If it fixes itself after a few minutes and keeps coming back, ask your host to check the server and your plan’s limits.
Is it safe to edit wp-config.php?
Yes, if you download a copy first. A typo in this file can take the site down, but putting the backup copy back fixes it immediately.
Sources
- Editing wp-config.php (WordPress Developer Resources)
- Debugging in WordPress (WordPress Developer Resources)