preloader

How to Fix “Error Establishing a Database Connection” in WordPress

You see: Error establishing a database connection. Work through these in order – the first check catches 80% of cases. Wrong credentials in wp-config.php are by far the usual cause, especially after a host migration or a password reset you forgot about. If credentials are correct and the error persists, move to the next check.

Step 1: Check wp-config.php Credentials

Open wp-config.php via FTP. Verify these four values exactly match what your hosting panel shows for your database:

define( 'DB_NAME', 'your_database_name' );
define( 'DB_USER', 'your_database_user' );
define( 'DB_PASSWORD', 'your_database_password' );
define( 'DB_HOST', 'localhost' );  // may differ on your host

Database passwords are case-sensitive. A single wrong character causes this error. Copy-paste credentials from your hosting panel rather than typing them. Common DB_HOST values that are not “localhost”: some hosts use 127.0.0.1, a specific hostname like mysql.yourhost.com, or a port-specific value like localhost:3306. Check your hosting documentation for the correct DB_HOST.

Step 2: Test If the Database Server Is Running

Your credentials might be correct but the MySQL server might be down. Try to access phpMyAdmin through your hosting control panel. If phpMyAdmin also cannot connect, the MySQL service is down – this is a hosting infrastructure problem. Contact your host’s support immediately with the exact error and time it started. On a VPS or dedicated server you manage yourself, SSH in and check: systemctl status mysql or service mysqld status.

Problem not solved? Describe the issue and get a free estimate.

Step 3: Check If the Database Exists

Log into phpMyAdmin and verify your WordPress database exists in the left sidebar. If it does not exist, it was deleted or the credentials are pointing to the wrong database. If you recently migrated hosts, the database import may have failed silently. Re-import the database backup and retry.

Step 4: Repair a Corrupted Database

Database tables become corrupted during server crashes, interrupted writes, or disk failures. Corrupted tables cause the database connection error even with correct credentials. WordPress has a built-in repair tool. Add to wp-config.php:

define( 'WP_ALLOW_REPAIR', true );

Then visit: https://yoursite.com/wp-admin/maint/repair.php

Click “Repair and Optimize Database”. After repair completes, remove the WP_ALLOW_REPAIR line from wp-config.php immediately – leaving it active lets anyone access the repair page without authentication.

You can also repair tables directly in phpMyAdmin: select all tables, choose “Repair table” from the With Selected dropdown. This is equivalent to the WordPress repair tool but does not require WordPress to be functional.

Step 5: Too Many Connections

MySQL has a connection limit. On shared hosting, if too many sites on the same server are making database connections simultaneously, new connections are rejected. This produces the database connection error intermittently (not consistently). Contact your host – they need to either increase the connection limit or investigate what is causing the spike. Consider using WP-Optimize to keep your database healthy. On your own VPS, also check SHOW STATUS LIKE 'Max_used_connections'; in MySQL to see if you are hitting the limit.

Keep Reading

Previous Post WordPress Frontend Is Slow: Diagnosing and Fixing Page Load Speed Next Post WooCommerce Products Not Showing in Shop: How to Fix

Need Help With Your WordPress Site?

If you need help with WordPress fixes, plugin issues, theme customization, or development work, feel free to get in touch.

Get a Free Estimate