What Is the “Error Establishing a Database Connection” Error?
This error means WordPress cannot communicate with its database (MySQL or MariaDB). You’ll see a message like:
- “Error establishing a database connection”
- “Unable to connect to the database server”
- “Can’t connect to MySQL server”
When this happens, your entire site goes down. WordPress can’t retrieve any content from the database, so nothing loads. Unlike other errors, this one isn’t caused by WordPress itself but by connectivity issues between WordPress and the database server.
This is one of the most common hosting-related errors and almost always has a straightforward fix.
Common Causes of Database Connection Errors
- Incorrect database credentials — Wrong username, password, or database name in wp-config.php
- Database server is down — MySQL service crashed or isn’t running
- Database user account issues — User permissions removed or account locked
- Hostname mismatch — Using wrong database server address
- Database exceeded resource limits — Too many connections or storage limit exceeded
- Network connectivity issue — Firewall blocking database connections
- Corrupted wp-config.php — File got damaged or edited incorrectly
- Hosting account limitations — Reached database limits on shared hosting
Solution 1: Verify Your Database Credentials
The most common cause is simply wrong login information.
Find your correct credentials:
- Log in to your hosting control panel (cPanel, Plesk, etc.)
- Find “MySQL Databases” or “Databases” section
- Look for your database name, username, and host
Update wp-config.php:
- Connect via FTP or File Manager
- Open
wp-config.phpin your root directory - Find these lines:
define('DB_NAME', 'database_name');
define('DB_USER', 'database_user');
define('DB_PASSWORD', 'database_password');
define('DB_HOST', 'localhost');
-
Replace with your actual credentials from the control panel
-
Common DB_HOST values:
localhost(most common on shared hosting)127.0.0.1mysql.yourdomain.com- Your specific server address (check your hosting docs)
-
Save the file
Solution 2: Check Database User Permissions
The database user might exist but lack proper permissions.
Via cPanel:
- Go to “MySQL Databases”
- Find your database user
- Check the “User Privileges” section
- Ensure all required permissions are checked (SELECT, INSERT, UPDATE, DELETE, etc.)
- If missing, add them back
Via phpMyAdmin (if accessible):
- Click on “User accounts” at the top
- Find your database user
- Click “Edit privileges”
- Under “Database-specific privileges,” select your database
- Check all global privileges
- Click “Go”
Solution 3: Reset the Database User Password
A corrupted or forgotten password causes connection failures.
Via cPanel:
- Go to “MySQL Databases”
- Under “MySQL Users,” find your database user
- Click the password reset icon (usually a lock or key)
- Set a new password (make it strong: mix uppercase, lowercase, numbers, symbols)
- Click “Change Password”
- Update
wp-config.phpwith the new password - Test your site
Via phpMyAdmin:
- Click “User accounts”
- Right-click your user → “Change password”
- Enter new password twice
- Click “Go”
Solution 4: Check if MySQL/Database Server Is Running
The database service might be down.
Via cPanel:
- Log in to cPanel
- Look for “MySQL Processes” or “Services”
- Check if MySQL is running
- If not, click the play/restart button
Via SSH (if you have access):
systemctl status mysql
If it says “inactive” or “stopped,” restart it:
sudo systemctl restart mysql
For MariaDB:
sudo systemctl restart mariadb
Contact your host if you can’t restart MySQL from your panel. They have access to server controls.
Solution 5: Check Database Host/Server Address
Using the wrong server address prevents connections.
Find the correct address:
- Log in to hosting control panel
- Look for “MySQL Databases” or “Database Info”
- Check the “Host” or “Server” field
Common variations:
- If you have multiple servers:
mysql1.yourhostname.com(not justlocalhost) - Some hosts require you to add your IP to whitelist
- Managed hosts sometimes use specific database server URLs
Update wp-config.php:
define('DB_HOST', 'mysql1.yourhostname.com');
Solution 6: Verify MySQL Port Settings
Non-standard port numbers can cause connection failures.
Check your hosting documentation for the MySQL port (usually 3306).
If your host uses a different port, update wp-config.php:
define('DB_HOST', 'localhost:3306');
Or for a remote server:
define('DB_HOST', 'mysql.yourdomain.com:3306');
Solution 7: Check Database Exists
The database itself might have been deleted or never created.
Via cPanel:
-
Go to “MySQL Databases”
-
Check if your database is listed
-
If not, create a new one:
- Click “Create New Database”
- Enter name (usually
username_dbname) - Click “Create Database”
-
Then create a user and assign them to the database with all privileges
Via phpMyAdmin:
- Click “Databases” tab
- Check if your database is listed
- If not, create one manually
Solution 8: Check Server Resource Limits
Shared hosting plans have connection limits.
- Log in to cPanel
- Check “Concurrent MySQL Connections” (usually around 5-10)
- If you’re hitting the limit, either reduce active connections or upgrade hosting
Also check:
- Database storage used vs. limit
- Number of databases allowed
If limits are exceeded, contact hosting support for an upgrade.
Solution 9: Disable WordPress Plugins
A misbehaving database plugin can interfere with normal connections.
- Via FTP, rename
/wp-content/plugins/to/wp-content/plugins-disabled/ - Test your site
- If it works, rename back to
plugins - Disable plugins one by one to identify the culprit
Some caching or database optimization plugins can cause issues if misconfigured.
Solution 10: Test Database Connection Manually
Create a test file to verify connectivity:
- Create a new file called
test-db.phpvia FTP - Add this code:
<?php
$host = 'localhost';
$username = 'your_db_user';
$password = 'your_db_password';
$database = 'your_db_name';
$connection = mysqli_connect($host, $username, $password, $database);
if (!$connection) {
echo "Connection failed: " . mysqli_connect_error();
} else {
echo "Connected successfully to database!";
}
mysqli_close($connection);
?>
- Visit
yourdomain.com/test-db.php - If it shows “Connection failed,” the error message tells you exactly what’s wrong
- Delete
test-db.phpafter testing
Solution 11: Restore from Backup
If credentials are lost and can’t be recovered:
- Access your hosting backup system
- Restore to a point before the error started
- The database will be restored with original credentials
- Check the backup’s wp-config.php for the correct credentials
Prevention Tips
- Save your database credentials in a secure location (password manager)
- Document your database host/server address separately
- Don’t manually edit
wp-config.phpunless you’re confident - Keep database user permissions consistent
- Monitor MySQL processes in your hosting panel
- Maintain regular backups (at least weekly)
- Test backups periodically to ensure they work
Real-world tip: I keep a backup of wp-config.php with database credentials stored securely outside my hosting account. This saves time if something goes wrong and I need to quickly verify correct settings.
Related: After fixing the database connection, make sure other critical systems are working. Check How to Fix WordPress 500 Internal Server Error or How to Fix WordPress White Screen of Death if you encounter other issues.
Conclusion
The “Error Establishing a Database Connection” looks scary but is usually fixable in minutes. Nine times out of ten, it’s incorrect credentials in wp-config.php or a MySQL user that lost permissions. Start by verifying your database information matches what’s in your hosting control panel, then work through the other solutions systematically. If your hosting provider is responsive, they can also help diagnose database server issues quickly.