How to Fix 'Error Establishing a Database Connection' in WordPress

A technical walkthrough to diagnosing and fixing the dreaded 'Error Establishing a Database Connection' in WordPress. Covering wp-config credentials, MySQL server crashes, and database corruption.

How to Fix 'Error Establishing a Database Connection' in WordPress

Seeing a blank white screen with the bold text “Error establishing a database connection” is enough to cause panic for any WordPress site owner. Because WordPress relies entirely on PHP and a MySQL/MariaDB database to render content dynamically, an inability to connect to the database means your entire site goes offline.

Fortunately, this error usually boils down to three primary culprits: incorrect credentials, a crashed database server, or corrupted database tables. Here is how you troubleshoot them.

1. Verify Your wp-config.php Credentials

The most common reason for this error, especially after migrating a website, is mismatched database credentials in your wp-config.php file.

Connect to your server via SSH or FTP, locate wp-config.php in your root directory, and verify the following four lines:

// ** MySQL settings - You can get this info from your web host ** //
define( 'DB_NAME', 'database_name_here' );
define( 'DB_USER', 'username_here' );
define( 'DB_PASSWORD', 'password_here' );
define( 'DB_HOST', 'localhost' );

If you are hosted on NVMe-powered WordPress hosting in Pakistan, ensure these match the MySQL Databases section in your control panel. Note that DB_HOST is almost always localhost unless your database is hosted on a separate external server.

2. Check if the MySQL Server is Down

If you haven’t changed your wp-config.php file and the error suddenly appeared, your MySQL server might have crashed. This often happens on constrained shared hosting environments or unoptimized VPS servers when traffic spikes cause the server to run out of RAM, forcing the operating system to kill the MySQL process to survive.

If you have root SSH access, you can check the status of your database service:

systemctl status mysql
# or for MariaDB:
systemctl status mariadb

If the service is down, attempt to restart it:

systemctl restart mariadb

If the service restarts but crashes again shortly after, you need to investigate your server’s RAM utilization and potentially upgrade your infrastructure. If you’re having trouble connecting to SSH to check this, refer to our SSH Troubleshooting Guide.

3. Repair a Corrupted Database

If your credentials are correct and the MySQL server is running, the database tables themselves might be corrupted. A dead giveaway is if you see the “Error establishing a database connection” on the front-end, but a different error like “One or more database tables are unavailable” when you try to access /wp-admin/.

WordPress has a built-in database repair tool. To enable it, add this line to your wp-config.php file (just above the /* That's all, stop editing! */ line):

define( 'WP_ALLOW_REPAIR', true );

Once added, navigate to https://yourwebsite.com/wp-admin/maint/repair.php in your browser. Click “Repair and Optimize Database.”

Crucial Step: Once the repair is complete, immediately remove the WP_ALLOW_REPAIR line from your wp-config.php file to prevent unauthorized users from accessing the repair page.

Conclusion

The “Error establishing a database connection” is highly disruptive, but by methodically checking your configuration credentials, ensuring the MySQL service is active, and running a database repair, you can usually restore service in a matter of minutes. If you find your database crashing frequently, it is a strong indicator that you need to transition to a more robust hosting architecture.