What Is This Error?
You visit your WordPress site and suddenly see: Error establishing a database connection. This means WordPress cannot talk to your MySQL/MariaDB database. Without the database, your site is essentially dead — no content, no posts, nothing.
Root Causes
- Wrong credentials in wp-config.php (DB_NAME, DB_USER, DB_PASSWORD, DB_HOST)
- Database server is down — MySQL/MariaDB crashed or is not running
- Database corruption — tables crashed due to server crash or disk issues
- Too many connections — MySQL hit max_connections limit
- Server out of memory — Apache/PHP consuming all RAM
- wp-config.php corruption — file was accidentally modified
Step-by-Step Fixes
1. Check wp-config.php Credentials
Open your wp-config.php and verify these values:
define( "DB_NAME", "your_database_name" );
define( "DB_USER", "your_username" );
define( "DB_PASSWORD", "your_password" );
define( "DB_HOST", "localhost" );
2. Check MySQL Status
sudo systemctl status mysql
# or
sudo systemctl status mariadb
If not running, restart it:
sudo systemctl restart mysql
3. Repair the Database
Add this to wp-config.php temporarily:
define( "WP_ALLOW_REPAIR", true );
Then visit: https://yourdomain.com/wp-admin/maint/repair.php
4. Fix Database Tables via phpMyAdmin
Select all tables and click Repair Table
5. Check Memory Usage
free -h
top -bn1 | head -20
If memory is exhausted, consider adding to wp-config.php:
define( "WP_MEMORY_LIMIT", "256M" );
6. Increase Max Connections (MySQL)
Edit my.cnf:
max_connections = 200
7. Fix Corrupted wp-config.php
Download fresh WordPress and replace wp-config.php (keep your DB credentials!)
Prevention Tips
- Use a reliable host with proper resource limits
- Monitor server resources regularly
- Keep WordPress, themes, and plugins updated
- Schedule regular database backups
- Use database optimization plugins
- Implement proper caching to reduce DB load
Quick Diagnostic Checklist
| Check | Command/Tool |
|---|---|
| MySQL running? | systemctl status mysql |
| DB credentials correct? | wp-config.php |
| DB corrupted? | wp-admin/maint/repair.php |
| Memory OK? | free -h |
| Max connections? | SHOW VARIABLES LIKE max_connections; |
Conclusion
The Error establishing a database connection is scary but usually fixable. Start with checking your wp-config.php credentials, then verify MySQL is running, and finally repair corrupted tables if needed. For long-term stability, choose quality hosting and monitor your server resources.