Diagnosing and Fixing "Error Establishing a Database Connection" in WordPress
Few error messages are as alarming to website owners as "Error Establishing a Database Connection". When this occurs, WordPress cannot communicate with the MySQL or MariaDB database server to fetch page content, user accounts, or theme options. On shared hosting and VPS servers throughout Nepal, this error stems from four primary causes: incorrect credentials in wp-config.php, corrupted database tables, exhausted database connection pools, or an out-of-memory MySQL service crash.
Strategic Executive Summary
- Core Insight: Does the error show only on the frontend, or does navigating to /wp-admin/ display "One or more database tables are unavailable"? If WP-Admin shows table errors, your database needs table repair rather than credential fixes.
- Production Quality: Battle-tested engineering techniques designed specifically for Nepal's network infrastructure and business environment.
- Direct Implementation: Copy-paste ready code architectures with security safeguards against race conditions, data corruption, and unauthorized access.
Table of Contents
1. Verifying Database Credentials in wp-config.php
If your hosting provider recently migrated servers or updated database passwords, verify the four core constants in wp-config.php:
define( 'DB_NAME', 'cpaneluser_wpdb' );
define( 'DB_USER', 'cpaneluser_wpuser' );
define( 'DB_PASSWORD', 'Your_Strong_Password_Here' );
define( 'DB_HOST', 'localhost' );
To test these credentials independently without loading the entire WordPress core, create a temporary script named db-test.php in public_html/:
<?php
$conn = new mysqli('localhost', 'cpaneluser_wpuser', 'Your_Strong_Password_Here', 'cpaneluser_wpdb');
if ($conn->connect_error) {
die("Connection Failed: " . $conn->connect_error);
}
echo "Database Connected Successfully!";
$conn->close();
// IMPORTANT: Delete this file immediately after testing!
If connection fails with "Access denied", open cPanel MySQL Databases, ensure the user has "ALL PRIVILEGES" assigned to the database, and reset the password.
2. Repairing Corrupted MySQL Tables
Sudden server reboots, improper shutdowns, or heavy concurrent writes to wp_posts or wp_options can corrupt MyISAM or InnoDB tables.
Add this directive to wp-config.php right above "That's all, stop editing!":
define('WP_ALLOW_REPAIR', true);
Navigate to https://yourdomain.com/wp-admin/maint/repair.php in your browser and click "Repair and Optimize Database". Once completed, remove the WP_ALLOW_REPAIR line immediately to prevent unauthorized access.
3. Checking MySQL Service Crashes and Out of Memory (OOM)
On cloud VPS instances (such as 1GB or 2GB RAM instances), traffic spikes from Facebook or Google can exhaust physical RAM. The Linux kernel OOM (Out of Memory) Killer terminates the MySQL/MariaDB daemon to preserve system stability.
Log into your VPS via SSH and check service status:
# Check if MySQL daemon is running
sudo systemctl status mysql
# Inspect server memory and disk space
free -m
df -h
# Check if MySQL was killed by OOM
grep -i -E 'killed process|oom' /var/log/syslog /var/log/messages
If killed by OOM, restart the service with sudo systemctl restart mysql, and immediately configure a 2GB swap file (/swapfile) to absorb future memory spikes.