The “Error Establishing a Database Connection” message is one of the most serious WordPress errors. When it appears, your website usually becomes completely inaccessible.
This error means WordPress cannot communicate with its database. Since WordPress stores posts, pages, users, settings, and plugins in the database, the site cannot load anything without it.
The good news is that this error is usually fixable. So, why it happens and which checks to run in the correct order?
What Does “Error Establishing a Database Connection” Actually Mean?
WordPress connects to a MySQL (or MariaDB) database every time a page loads.
The connection requires:
- database name
- database user
- database password
- database host
If any part of this connection fails, WordPress stops and shows the error.
This is not a browser issue. This is not a theme issue. This is a server-level or configuration problem.
Most Common Causes of the “Error Establishing a Database Connection” Error
Before fixing anything, it helps to know the real causes.
1. Incorrect Database Credentials
The most common reason is incorrect login details in wp-config.php.
Even one wrong character in the password is enough to break the connection and show Error Establishing a Database Connection
2. Corrupted Database Tables
Database corruption can happen because of:
- plugin or theme bugs
- failed updates
- server crashes
- disk issues
When WordPress cannot read required tables, the connection fails.
3. Database Server Is Down
Sometimes the database server itself is offline.
This often happens on:
- shared hosting
- overloaded servers
- during hosting maintenance
In this case, credentials are correct, but the server is unreachable.
Stuck with a Database Connection Error?
If basic checks didn’t fix the issue, the problem is often related to hosting,
database corruption, or server configuration.
A WordPress expert can diagnose and fix the error before it causes prolonged downtime.
4. Hosting Resource Limits
High traffic or poorly optimized plugins can overload the database.
When the database reaches connection limits, new connections are refused.
5. Recent Migration or Hosting Change
After moving a site to a new host, database details often change.
If wp-config.php is not updated, WordPress will still try to connect to the old database.
Step 1: Check Database Credentials in wp-config.php
Start here!
Open the wp-config.php file in your WordPress root directory.
You should see something like this:
define( 'DB_NAME', 'database_name' );
define( 'DB_USER', 'database_user' );
define( 'DB_PASSWORD', 'database_password' );
define( 'DB_HOST', 'localhost' );
What to Verify
- DB_NAME – the database actually exists
- DB_USER – the user exists and has access
- DB_PASSWORD – correct password
- DB_HOST – matches your hosting provider
Some hosts use custom hosts, for example:
define( 'DB_HOST', '127.0.0.1' );
or
define( 'DB_HOST', 'mysql.yourhost.com' );
If you are unsure, your hosting provider can confirm the correct values.
Step 2: Test Database Connection Manually
If credentials look correct, test the connection directly.
Create a temporary file called db-test.php in the WordPress root:
<?php
$connection = new mysqli(
'localhost',
'database_user',
'database_password',
'database_name'
);
if ( $connection->connect_error ) {
die( 'Connection failed: ' . $connection->connect_error );
}
echo 'Database connection successful';
Open this file in the browser.
- If it fails → credentials or server problem
- If it works → WordPress configuration or database corruption
Delete this file after testing.
Step 3: Repair a Corrupted Database
WordPress includes a built-in database repair tool.
Enable Repair Mode
Add this line to wp-config.php:
define( 'WP_ALLOW_REPAIR', true );
Then visit:
https://yourwebsite.com/wp-admin/maint/repair.php
Click Repair Database or Repair and Optimize Database.
After the repair completes, remove the line from wp-config.php.
Step 4: Check Database Tables Directly
If repair does not help, inspect tables manually using phpMyAdmin.
Look for:
- missing tables
- tables marked as “corrupt”
- errors during queries
Common WordPress tables include:
wp_postswp_optionswp_userswp_usermeta
If critical tables are missing, restoring a backup may be required.
Step 5: Check If the Database Server Is Down
If credentials and tables are fine, the database server may be unavailable.
Signs of server issues:
- other sites on the same host are down, and have Error Establishing a Database Connection issue
- database connection works intermittently
- errors appear only during peak traffic
Contact your hosting provider and ask:
- is the database server running?
- are there connection limits?
- was there recent maintenance?
- are there Error Establishing a Database Connection issues on other sites they host?
On shared hosting, this is a very common cause.
Step 6: Increase Database Connection Limits
On some servers, database connections are limited, and this may cause an Error Establishing a Database Connection issue
You can try adding this to wp-config.php:
define( 'WP_USE_EXT_MYSQL', true );
Or increase PHP memory:
define( 'WP_MEMORY_LIMIT', '256M' );
This does not fix bad hosting, but it can reduce errors temporarily.
Step 7: Disable Plugins to Reduce Load
If the site loads intermittently, a plugin may be overloading the database.
Rename the plugins folder via FTP:
/wp-content/plugins → /wp-content/plugins-disabled
If the site loads, reactivate plugins one by one to find the culprit.
Heavy plugins often include:
- poorly optimized page builders
- analytics plugins
- backup plugins running too often
Step 8: Check for Broken Object Cache
Some sites use Redis or Memcached.
If object cache is misconfigured, database queries may fail and Error Establishing a Database Connection may appear
Temporarily disable object cache by renaming:
wp-content/object-cache.php
Then reload the site.
Step 9: Fix Database Errors After Migration
After migrating a site, database details often change, and the Error Establishing a Database Connection issue may arise
Common mistake:
- old credentials left in
wp-config.php
Make sure:
- database name matches the new server
- user has permissions
- host is correct
If the site worked before migration and broke after, this is usually the cause.
Step 10: Restore a Backup (Last Resort)
If the database is badly corrupted, restoring a backup may be the fastest fix.
Best practice:
- restore database only
- keep files unchanged if possible
Always restore on a staging site first if available.
How to Prevent This Error in the Future
Prevention is easier than recovery.
Use Reliable Hosting
Cheap hosting often causes database issues.
Managed WordPress hosting usually includes:
- optimized databases
- automatic backups
- monitoring
Schedule Regular Backups
Use daily backups for:
- database
- files
Test restores occasionally.
Optimize Database Regularly
You can schedule cleanup tasks:
wp_schedule_event( time(), 'weekly', 'db_optimization_event' );
Or use a trusted optimization plugin.
Monitor Server Health
Monitoring tools can alert you before failures happen.
Track:
- uptime
- CPU usage
- database response time
When to Get Professional Help
If:
- the Error Establishing a Database Connection error keeps returning
- you manage a business website
- downtime costs money
Professional help is often cheaper than repeated failures. Experienced WordPress developers can:
- diagnose server issues
- repair databases safely
- migrate sites to stable hosting
- optimize performance long-term
The “Error Establishing a Database Connection” message looks scary, but it is rarely permanent.
In most cases, it is caused by:
- incorrect credentials
- database corruption
- hosting limitations
By following the steps above in order, the issue can usually be fixed quickly and safely.
When the problem keeps returning, the real issue is often hosting quality or long-term maintenance.
Need a Permanent Fix for Database Errors?
Recurring database connection issues usually point to deeper hosting or configuration problems.
Get professional help to fix the root cause and keep your WordPress site stable and secure.

