What Causes "Error Establishing a Database Connection"?
"Error establishing a database connection" means WordPress's PHP code could not reach the MySQL or MariaDB database it needs to load the site. The usual causes are wrong database credentials in wp-config.php, a database server that is down, overloaded or hit its connection limit, or a corrupted database table, and the fix depends on which of those it turns out to be.
What this error actually means
Every WordPress page load runs PHP code that opens a connection to a MySQL or MariaDB database to fetch posts, settings and user data. This error appears the moment that connection attempt fails, before WordPress has loaded enough to show its own styled error page. It can show on every page, including wp-admin, or only on the front end while wp-admin still loads, which is itself a useful clue about the cause.
The most common causes
| Cause | Typical sign |
|---|---|
| Wrong DB_NAME, DB_USER, DB_PASSWORD or DB_HOST in wp-config.php | Error appears everywhere, right after a migration or host change |
| Database server down or restarting | Error appears suddenly on a site that worked minutes earlier |
| Too many simultaneous connections for the plan | Error comes and goes, often during traffic spikes |
| Corrupted database table | Front end broken, but wp-admin shows a more specific repair message |
| Database server hostname changed after a migration | Error starts right after moving to new hosting |
Checking wp-config.php first
Open wp-config.php in the site's root folder and confirm these four lines match the credentials in your hosting control panel exactly, including capitalization:
define( 'DB_NAME', 'your_database_name' );
define( 'DB_USER', 'your_database_user' );
define( 'DB_PASSWORD', 'your_database_password' );
define( 'DB_HOST', 'localhost' );
DB_HOST is localhost on most shared hosting, but managed platforms and some VPS setups use a different hostname or a port suffix such as localhost:3306. If you recently migrated hosts, this is the value most likely to be stale.
If credentials are correct: server and repair steps
- Confirm the database user still exists and is still assigned to that specific database, since some control panels can silently detach a user during a restore.
- Ask your host, or check your monitoring, whether the database service is running; on a VPS you control, check with
systemctl status mysqlorsystemctl status mariadb. - Check whether your plan has hit a maximum simultaneous connections limit, common on busy shared hosting during traffic spikes.
- If wp-admin shows a message about a corrupted table, add
define( 'WP_ALLOW_REPAIR', true );to wp-config.php, visityoursite.com/wp-admin/maint/repair.php, run the repair, then remove that line again since the page is otherwise publicly reachable.
When it is on the host, not on you
If multiple unrelated sites on the same server show this error at the same time, or it resolves itself within minutes without any change on your end, the database server itself likely restarted or was briefly overloaded. In that case contact hosting support rather than editing files, since there is nothing in wp-config.php for you to fix.
Frequently asked questions
What does "Error establishing a database connection" mean?
It means WordPress's PHP code could not connect to the MySQL or MariaDB database that stores the site's content, usually because of wrong credentials, a down database server, or a connection limit being reached.
How do I fix a database connection error in WordPress?
Start by checking DB_NAME, DB_USER, DB_PASSWORD and DB_HOST in wp-config.php against your hosting control panel. If those are correct, check whether the database server itself is running and not over its connection limit.
Why did this error appear right after I migrated my site?
Migrations commonly change the database hostname, username or password, so wp-config.php on the new server still points to the old values. Update those four constants to match the new environment.
Does this error mean I lost my data?
Not necessarily. The error means WordPress cannot currently reach the database, not that the database or its data is gone. Table corruption can sometimes be repaired without data loss using WordPress's built-in repair tool.
Why does the error only appear on the front end but not wp-admin?
wp-admin sometimes detects a corrupted table and shows a specific repair notice instead of the generic connection error, which is a sign to run the WordPress database repair tool rather than check your credentials.
Can too much website traffic cause this error?
Yes. If traffic pushes your plan past its maximum simultaneous database connections, new connection attempts fail with this same error until load drops or the limit is raised.
Related topics
- What Is a Database? Databases
- What Is WordPress? WordPress Hosting
- MySQL vs. MariaDB Databases
- What Is wp-config.php? WordPress Hosting
- What Is a Database Table? Databases