How to Fix "Error Establishing a Database Connection" in WordPress
Step-by-step fixes for WordPress's "Error establishing a database connection": check wp-config.php credentials, the database host, user privileges, corrupted tables and server limits.

Table of Contents
- Before you start: is it the whole site?
- Step 1: Check the credentials in wp-config.php
- Step 2: Check that the user has access to the database
- Step 3: Check whether the database server is running
- Step 4: Repair corrupted tables
- Step 5: Check the site URL and table prefix
- Step 6: Look for resource or disk problems
- Step 7: Restore from a backup if needed
- Quick reference
- Frequently Asked Questions
- Related reading
- Sources
"Error establishing a database connection" means WordPress could not connect to its MySQL or MariaDB database. The cause is almost always one of four things: wrong database details in wp-config.php, a database user without access, a database server that is down or overloaded, or corrupted database tables.
Work through the checks below in order. Take a backup of wp-config.php before editing it.
Before you start: is it the whole site?
- If both the front end and
/wp-admin/show the error, the connection itself is failing. Continue with step 1. - If
/wp-admin/shows a different message, such as "One or more database tables are unavailable", tables may be corrupted. Jump to step 4. - If the error appeared right after migrating the site, step 1 is almost certainly the cause.
Step 1: Check the credentials in wp-config.php
Open wp-config.php in the site's root folder (with File Manager or FTP) and find:
define( 'DB_NAME', 'example_wp' );
define( 'DB_USER', 'example_wpuser' );
define( 'DB_PASSWORD', '...' );
define( 'DB_HOST', 'localhost' );
Compare each value with what exists on the server:
- In cPanel, open MySQL Databases. Check the exact database name and user name, including the account prefix (for example
cpaneluser_). - If you are unsure of the password, set a new password for the database user in cPanel and paste the same password into
DB_PASSWORD. DB_HOSTis usuallylocalhoston shared hosting. Some hosts use a different host name or a port such aslocalhost:3306; check your host's documentation.
Watch for invisible problems: extra spaces, quotes copied from a word processor, or a password containing a ' character that breaks the PHP string.
Step 2: Check that the user has access to the database
A user can exist without being linked to the database. In cPanel → MySQL Databases, under "Current Databases", check that the user is listed against the database. If not, use Add User to Database and grant All Privileges.
A detailed walkthrough is in how to create a MySQL database and user in cPanel.
Step 3: Check whether the database server is running
If the credentials are correct but the error persists:
- Open phpMyAdmin from cPanel. If it also fails to connect, the database server may be down or overloaded.
- Check whether other sites on the same hosting account also fail.
- On a VPS, check the service:
systemctl status mariadb(ormysql) and the database error log.
If the database server is down on shared hosting, contact your host. If the error comes and goes under traffic, the server may be hitting connection or resource limits; see hosting resource limits explained.
Step 4: Repair corrupted tables
Crashed tables can stop WordPress from loading. To use WordPress's built-in repair tool:
- Add this line to
wp-config.php:
define( 'WP_ALLOW_REPAIR', true );
- Visit
https://yourdomain.com/wp-admin/maint/repair.php. - Choose Repair Database.
- Remove the line from
wp-config.phpafterwards. The repair page does not require a login while it is enabled.
You can also use Check and Repair in phpMyAdmin or cPanel's MySQL Databases → Repair Database.
Step 5: Check the site URL and table prefix
After a migration, check that $table_prefix in wp-config.php matches the prefix of the tables in the database (for example wp_ or wpx7_). If they differ, WordPress connects but cannot find its tables and may show an installation screen or errors.
Step 6: Look for resource or disk problems
- Disk full: a full hosting account can stop the database from writing. Check disk usage in cPanel.
- Too many connections: traffic spikes or a slow query can exhaust database connections.
- Memory limits on a VPS: the database server may have been stopped by the out-of-memory killer. See troubleshooting a slow or unresponsive server.
Step 7: Restore from a backup if needed
If tables are damaged beyond repair, restore the database from your most recent backup. See WordPress backups: what to back up and how to restore.
Quick reference
| Symptom | Most likely cause | Fix |
|---|---|---|
| Error immediately after migration | Wrong DB name, user, password or host | Correct wp-config.php |
| phpMyAdmin works, site does not | Credentials or user privileges | Recheck user and privileges |
| phpMyAdmin also fails | Database server down | Contact host or restart service |
| Error only under traffic | Connection or resource limits | Caching, fix slow queries, more resources |
| "Tables unavailable" in admin | Corrupted tables | Repair database |
Frequently Asked Questions
Can a plugin cause this error?
Rarely directly. A plugin causing slow queries can contribute to overload, but the message itself means the connection failed.
Will I lose my content?
Usually not. The content is still in the database; WordPress just cannot reach it. Corrupted tables are the exception, which is why backups matter.
Why did it happen when I changed hosting?
The new server's database name, user or password usually differ, and the old values are still in wp-config.php.
Related reading
For other common WordPress errors, see how to fix the WordPress white screen of death, and for the bigger picture, WordPress hosting: what it is and how to choose. If you are on ServerNeed hosting and the database server seems down, contact support.
Sources
Last updated 7 October 2026



