Seeing the “Error Establishing a Database Connection” message in WordPress means your website is unable to connect to its database. This is a common issue and can be fixed quickly if you follow the correct steps.
Video Tutorial
What Causes This Error?
This error usually happens due to:
- Incorrect database credentials
- Corrupted database
- Server/database downtime
- Corrupted WordPress files
Step 1: Check Your Database Credentials (Most Common Fix)
- Log in to your cPanel
- Open File Manager → go to
public_html - Find and open
wp-config.php
Verify these details:
define('DB_NAME', 'your_database_name');
define('DB_USER', 'your_database_user');
define('DB_PASSWORD', 'your_database_password');
define('DB_HOST', 'localhost');
Make sure they match your MySQL database details in cPanel.
Step 2: Repair the Database
- Open
wp-config.php - Add this line:
define('WP_ALLOW_REPAIR', true);
- Visit:
yourdomain.com/wp-admin/maint/repair.php
- Click Repair Database
⚠️ Remove the line after repair for security.
Step 3: Check if the Database Server is Down
- Try accessing phpMyAdmin from cPanel
- If it doesn’t open → server issue
In this case, contact your hosting provider.
Step 4: Check Database User Permissions
- Go to MySQL Databases in cPanel
- Ensure:
- The database user is assigned
- Has ALL PRIVILEGES
Step 5: Restore Backup (If Available)
If the database is corrupted:
- Restore from backup via cPanel
- Or use hosting backup tools
Step 6: Re-upload Core WordPress Files
If files are corrupted:
- Download fresh WordPress from wordpress.org
- Upload and replace:
wp-adminwp-includes
⚠️ Do NOT replace wp-content or wp-config.php
Common Mistakes to Avoid
- Editing the wrong database
- Forgetting to remove repair mode
- Using incorrect DB_HOST
- Overwriting important files
Pro Tips
- Always keep daily backups
- Use reliable hosting to avoid DB crashes
- Monitor uptime and database health
Want to Avoid This Error Completely?
Most database errors happen due to poor hosting environments.
👉 Choose optimized WordPress hosting with stable database servers and automatic backups
Conclusion
The “Error Establishing a Database Connection” can look scary, but in most cases, it’s just a configuration issue. By checking your credentials and repairing the database, you can fix it within minutes.