Learn how to fix the “Error Establishing a Database Connection” in WordPress. Step-by-step guide to check wp-config.php, MySQL server, and user credentials.
What Does This Error Mean?
When you see “Error establishing a database connection”, it means WordPress cannot connect to your MySQL database.
This can happen if:
- Database credentials are incorrect
- MySQL server is down
- Database is corrupted
- User privileges are missing
Step-by-Step Fix
✅ Step 1: Check wp-config.php
Open wp-config.php
and confirm your database settings:
define( 'DB_NAME', 'your_database_name' );
define( 'DB_USER', 'your_username' );
define( 'DB_PASSWORD', 'your_password' );
define( 'DB_HOST', 'localhost' );
DB_NAME
→ must match your actual database nameDB_USER
andDB_PASSWORD
→ must match your MySQL user credentialsDB_HOST
→ usuallylocalhost
, but can differ on some hosts (127.0.0.1
or remote IP)
✅ Step 2: Test Database Connection Manually
Run this from the terminal:
mysql -u your_username -p your_database_name
If you can log in → credentials are fine.
If not → fix your MySQL user or password.
✅ Step 3: Check MySQL Server Status
Make sure MySQL is running:
sudo systemctl status mysql
If it’s stopped, restart it:
sudo systemctl restart mysql
✅ Step 4: Repair Database (Optional)
If the database is corrupted, add this line to wp-config.php
:
define('WP_ALLOW_REPAIR', true);
Then visit:
http://yourdomain.com/wp-admin/maint/repair.php
Repair and optimize the database.
Remove the line after repair.
✅ Step 5: Check User Privileges
Log in to MySQL and grant privileges:
GRANT ALL PRIVILEGES ON your_database_name.* TO 'your_username'@'localhost' IDENTIFIED BY 'your_password';
FLUSH PRIVILEGES;
✅ Step 6: Restore Backup (if needed)
If nothing works, restore your database from a backup.
⚡ Quick Recap
- This error means WordPress can’t talk to MySQL.
- Fix by:
- Checking
wp-config.php
credentials - Testing MySQL connection
- Restarting MySQL
- Repairing the database
- Restoring from backup
- Checking
✅ Following these steps should help you fix the Error Establishing a Database Connection and get your site back online.