Diagnosing 'Error Establishing a Database Connection' in WordPress
A systematic diagnostic guide to resolving 'Error Establishing a Database Connection' in WordPress, covering credential validation, service crashes, and table corruption.
16 May 2026, 03:53 UTC

The Connection Failure Problem
The "Error Establishing a Database Connection" message is a generic failure state. It indicates that the WordPress PHP application cannot communicate with the MySQL or MariaDB database server. Because this error masks several distinct failure points—ranging from simple typos in a config file to server-level resource exhaustion—you cannot fix it without first isolating where the handshake is failing.
Diagnostic Matrix: Identifying the Root Cause
| Symptom | Likely Cause | Diagnostic Focus |
|---|---|---|
| Error appears on front-end AND admin dashboard | Server down or wrong credentials | Service status & wp-config.php |
| Error appears only on front-end; admin shows "One or more database tables are unavailable" | Database corruption | Table integrity checks |
| Intermittent connection drops under high traffic | Resource exhaustion (OOM) | RAM usage & MySQL logs |
| Connection timeout (long hang before error) | Network/Firewall block | Port 3306 connectivity |
Step 1: Validate Application Credentials
The most common cause is a mismatch between the wp-config.php file and the database user permissions. Before changing any code, verify the credentials manually via the command line.
Run this command from the server terminal (replace placeholders with values found in your wp-config.php):
# Run as a user with shell access to the server
mysql -u [db_user] -p -h [db_host] [db_name]
- Expected Result: You are prompted for a password and successfully enter the MySQL monitor.
- Failure: If you receive "Access denied," the username, password, or host permissions in
wp-config.phpare incorrect. - Risk: Do not edit
wp-config.phpwithout a backup. A single missing semicolon or quote will trigger a White Screen of Death (WSOD).
Step 2: Verify Database Service Status
If the manual login fails with "Can't connect to MySQL server," the service itself may be stopped or crashed due to an Out-of-Memory (OOM) event.
Check the service status using the system manager (requires sudo/root permissions):
# For systemd-based systems (Ubuntu, CentOS 7+)
systemctl status mysql
# OR
systemctl status mariadb
If the service is inactive or failed, attempt a restart: sudo systemctl restart mysql. If it crashes again immediately, check the system logs (/var/log/syslog or dmesg) for "Out of memory: Kill process" to determine if the server requires more RAM.
Step 3: Test Network and Port Connectivity
In decoupled environments where the database lives on a separate server, a firewall may be blocking port 3306 (the default MySQL port).
Test the connection from the web server to the database server:
# Run from the web server terminal
telnet [db_host] 3306
If the connection times out or is refused, verify that the database server's security group or firewall allows incoming traffic from the web server's IP address on port 3306.
Step 4: Repair Corrupted Tables
If you can access /wp-admin/ but see a database repair notice, the connection is working, but the data is unreadable. This often happens after an unclean server shutdown.
Enable the built-in WordPress repair tool by adding this line to wp-config.php, just before the "That's all, stop editing!" line:
define('WP_ALLOW_REPAIR', true);
Navigate to http://yourdomain.com/wp-admin/maint/repair.php and select "Repair and Optimize Database." Crucial: Remove the WP_ALLOW_REPAIR line immediately after finishing to prevent unauthorized users from triggering repairs.
Summary of Fixes
- Wrong Credentials: Update
DB_USERandDB_PASSWORDinwp-config.phpto match the MySQL user table. - Service Crash: Restart the service and increase server RAM or optimize
my.cnfto prevent OOM kills. - Host Mismatch: Change
DB_HOSTfromlocalhostto127.0.0.1if the MySQL socket is misconfigured. - Corruption: Use the
mysqlcheckutility or theWP_ALLOW_REPAIRconstant to fix tables.
Escalation Criteria
If the following conditions persist, escalate to a database administrator or hosting provider:
- The MySQL service fails to start even after clearing the
/tmpdirectory and checking disk space. - Manual CLI login works, but WordPress continues to report a connection error (indicates a PHP-MySQL driver mismatch).
- The database logs show "InnoDB: Database page corruption on disk," which requires binary log recovery.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.