How to Fix WordPress ‘Error Establishing a Database Connection’
You open your site and instead of your homepage you see a plain white page that says error establishing a database connection. Nothing loads, and in most cases not even the admin area works, so the message gives you almost no clue about what went wrong. The good news is that this error almost always comes down to a short list of causes, and you can work through them one by one without touching your content. This guide walks through each fix in order, from the quickest checks to the deeper repairs, so you can get your WordPress site back online.
What the WordPress Error Establishing a Database Connection Means
WordPress stores nearly everything in a MySQL or MariaDB database: your posts, pages, comments, settings, user accounts, and plugin data. Every time someone visits a page, WordPress asks the database for that content. When the WordPress database connection failed, the site has nothing to show, so it prints the error and stops.
The error itself does not tell you which part broke. It could be wrong login details in your config file, a database server that stopped running, a corrupted table, or a server that ran out of disk space or memory. The fix depends on the cause, so the fastest path is to test each one in turn instead of guessing.
Check Your wp-config.php Database Credentials
The most common cause is a mismatch between the login details in wp-config.php and the actual database. Your wp-config.php database credentials live in four lines near the top of that file:
DB_NAME: the name of the database WordPress should useDB_USER: the MySQL user WordPress logs in asDB_PASSWORD: that user’s passwordDB_HOST: usuallylocalhoston a VPS, or a separate host on managed setups
Open the file in your site root and compare each value against the database user you created in your hosting panel or with the MySQL command line. A typical scenario: you reset the database password in your panel last week and forgot to update wp-config.php. Another one: a migration copied the files but the database name on the new server is different. Fix the wrong value, save the file, and reload the site. If the error is gone, you found it.
You can also test the credentials directly from the command line with mysql -u your_user -p your_database. If MySQL rejects the login, the credentials are wrong. If it logs you in, the credentials are fine and the problem sits somewhere else.
Test Whether MySQL Is Actually Running
If the credentials check out, the database server itself may be down. On a VPS, check the service with systemctl status mysql or systemctl status mariadb, depending on which one you installed. If it shows as inactive or failed, restart it with systemctl restart mysql and reload your site.
On a small VPS this failure has a familiar pattern. A traffic spike or a heavy backup job eats the available RAM, the system kills MySQL to protect itself, and the site goes down with the database error. Check the MySQL error log, usually at /var/log/mysql/error.log, for lines about being killed or running out of memory. If that is what happened, the restart is only a bandage: you need to lower MySQL memory use or add RAM before the next spike does the same thing.
Disk space is another quiet killer. MySQL refuses new connections when the disk is full, and a full disk can also corrupt tables. Run df -h and look at the partition that holds /var/lib/mysql. If it shows 100 percent used, free space by clearing old logs, old backups, or oversized files before you restart the service. A related failure is the connection limit: if too many requests hit MySQL at once, new ones get refused. Our guide to troubleshooting the MySQL too many connections error walks through raising the limit and finding the queries that fill it.
Repair a Corrupted WordPress Database
If MySQL runs and the credentials are right but the error stays, one or more tables may be damaged. A sudden power cut, a crashed server, or a disk that filled up mid-write can leave a table unreadable, and WordPress fails the moment it tries to read it.
Before you repair anything, make a copy of the database as it is, even in its broken state. A repair that goes wrong can make things worse, and a backup gives you a way back. If you have no backup routine yet, our guide on backing up MySQL databases automatically with mysqldump and cron sets one up in a few minutes.
To repair WordPress database tables, start with the built-in tool WordPress ships with. Add define('WP_ALLOW_REPAIR', true); to wp-config.php, then visit yoursite.com/wp-admin/maint/repair.php and click Repair Database. WordPress checks every table and fixes the ones it can. When it finishes, remove that line from wp-config.php right away, because anyone can open the repair page while it is enabled.
You can also repair from the command line with mysqlcheck -r your_database -u your_user -p, or from phpMyAdmin by selecting the tables and choosing Repair table. The command line version is useful when the admin area itself will not load. After the repair, reload the site and check a few different pages, not just the homepage, since the damaged table might belong to a plugin or a specific post type.
Corruption sometimes comes back because the database has grown messy over years of revisions, transients, and leftover plugin tables. Our walkthrough on shrinking a bloated WordPress database without a plugin shows how to clear that dead weight safely, which also makes future repairs faster.
Fix It When Only the Admin Area Is Broken
Sometimes the front of the site loads fine but /wp-admin shows the database error, or the other way around. That split points at a specific table rather than the whole database. The admin area leans on tables like wp_options and wp_usermeta, while a single post page leans on wp_posts and wp_postmeta. A MySQL database connection error that only hits one side usually means one of those tables is the damaged one.
Run the repair step above, then check the result table by table. In phpMyAdmin or with mysqlcheck, look for any table still marked as crashed after the repair. If one table refuses to repair, you may need to restore just that table from a backup, or drop and rebuild it if it only holds disposable data such as transients.
Plugins can cause the same split. A plugin that runs a bad query on every admin page can lock a table or flood MySQL with connections until the admin area fails while the front page, which never runs that query, keeps working. Rename the wp-content/plugins folder briefly to disable every plugin at once. If the admin area comes back, rename the folder back and reactivate plugins one at a time until the error returns. That last plugin is the one to replace or update.
Prevent It From Happening Again
Once the site is back, spend an hour making sure the error does not return next month. Set up automatic daily backups and test a restore at least once, because a backup you have never restored is a guess, not a safety net. Keep an eye on disk space and memory with a simple monitor or a weekly manual check, since full disks and killed database processes cause a large share of these outages.
Keep WordPress core, themes, and plugins updated, and remove plugins you no longer use. Old plugins are a common source of bad queries and corrupted tables. Finally, keep the database lean: regular cleanup of post revisions, expired transients, and orphaned metadata keeps tables small and repairs quick. Our guide to cleaning up wp_postmeta bloat covers the tables that grow fastest and how to trim them without breaking anything.
Most cases of this error end with one of the fixes above: corrected credentials, a restarted database, a repaired table, or a bad plugin removed. Work through them in order, check the site after each step, and you will usually find the cause within half an hour.