An in-depth troubleshooting guide for the WordPress database connection error, covering wp-config.php verification, password resets, database repair, MySQL service management, and preventive measures.
Troubleshooting the "Error Establishing a Database Connection" in WordPress
Few WordPress errors cause as much panic as this one. The whole site goes white, the admin panel is gone, and if you're running WooCommerce, every minute of downtime is costing money. The good news? It's almost always fixable, and usually faster than you'd expect.
Here's how the database connection works, what breaks it, and how to get things running again.
Understanding the WordPress Database Connection
WordPress stores its database credentials in wp-config.php. On every page load, it reads those credentials and connects to MySQL (or MariaDB) to pull posts, settings, user data -- everything. If the credentials are wrong, MySQL is down, the database is corrupted, or the server is out of resources, you get the dreaded error.
The four fields that matter in wp-config.php:
DB_NAME-- The database nameDB_USER-- The database usernameDB_PASSWORD-- The database passwordDB_HOST-- The database server (usuallylocalhost)
Common Causes
- Wrong credentials in
wp-config.php(typos happen more often than you'd think) - MySQL/MariaDB service crashed or was stopped
- Corrupted database tables
- Server ran out of memory, CPU, or max connections
- Hosting-level issues -- network blips, hardware problems
- A plugin or theme hammering the database connection pool
Quick Fixes and Basic Checks
4.1 Review and Fix the WordPress Config File
Open wp-config.php and double-check these values:
define('DB_NAME', 'your_database_name');
define('DB_USER', 'your_database_user');
define('DB_PASSWORD', 'your_database_password');
define('DB_HOST', 'localhost');
One wrong character and the connection fails. Compare what's in the file against what your hosting control panel shows as the actual database name, user, and password.
4.2 Reset the Database Password
Go into your hosting panel (cPanel, Plesk, etc.) and find the MySQL Databases section. Reset the password for the WordPress database user, then update wp-config.php to match.
4.3 Repair the WordPress Database
WordPress has a built-in repair tool that most people don't know about. Add this line to wp-config.php, right above /* That's all, stop editing! */:
define('WP_ALLOW_REPAIR', true);
Then visit https://yoursite.com/wp-admin/maint/repair.php in your browser. Pick "Repair Database" or "Repair and Optimize Database." Remove this line when you're done -- leaving it in is a security risk.
4.4 Test the Database Connection
Want to isolate whether it's a WordPress problem or a MySQL problem? Drop this file in the WordPress directory as test-db.php:
<?php
$link = mysqli_connect('localhost', 'your_db_user', 'your_db_password', 'your_db_name');
if (!$link) {
die('Connection failed: ' . mysqli_connect_error());
}
echo 'Connected successfully';
mysqli_close($link);
?>
Hit http://yoursite.com/test-db.php in the browser. If it connects, the problem is in WordPress (likely a plugin). If it doesn't, the credentials or MySQL service are the issue. Delete this file after testing.
4.5 Restart MySQL Using systemctl
If you have SSH access (VPS or dedicated server), a quick MySQL restart often clears up transient issues:
sudo systemctl restart mysql
Advanced Troubleshooting
5.1 Check Server Logs
Dig into the error logs -- via your hosting panel or SSH at /var/log/. You're looking for things like ERROR 1045 (28000) (access denied) or out-of-memory messages. The log usually tells you exactly what went wrong.
5.2 Review Hosting Resource Usage
Check CPU, memory, and process counts in your hosting panel. If you see regular spikes hitting the limits, you're probably on a plan that's too small for your traffic. Either optimize (caching, fewer plugins) or upgrade the hosting.
5.3 Disable Plugins Temporarily
A misbehaving plugin can exhaust database connections. The fastest way to test: rename the plugins directory via SSH or FTP.
mv wp-content/plugins wp-content/plugins_disabled
If the site comes back, re-enable plugins one by one to find the problem.
5.4 Reinstall WordPress Core Files
Grab a fresh copy of WordPress from wordpress.org. Replace wp-admin and wp-includes (leave wp-content alone). This fixes corrupted core files without touching your themes, plugins, or uploads.
5.5 Optimize the MySQL Server Configuration
On a VPS or dedicated server, bad MySQL tuning is a common root cause:
- Bump
innodb_buffer_pool_sizeif MySQL keeps running out of cache - Set
max_connectionsto something realistic for your traffic - Adjust
wait_timeoutandinteractive_timeoutto clean up stale connections
Preventing Future Issues
- Backups -- schedule regular backups of files and database. UpdraftPlus and BackupBuddy handle this well. Test restores occasionally.
- Security -- limit login attempts, use strong passwords, scan for malware. A compromised site can trash your database.
- Reliable hosting and ongoing maintenance -- cheap hosting leads to cheap uptime. For sites that matter, professional server management pays for itself.
- Keep everything updated -- WordPress core, themes, and plugins. Updates include security patches and performance fixes that prevent exactly these kinds of issues.
Final Thoughts
This error is scary but usually straightforward to fix. Start with wp-config.php credentials, check whether MySQL is running, try the repair tool, and work your way up to server-level troubleshooting if needed. For sites that can't afford downtime, proactive monitoring catches these problems before your customers do.
Or read how we handle it in WordPress Speed Optimization.
Related Articles
How to Troubleshoot and Fix Cloudflare Error 521 on Ubuntu
A practical Ubuntu-focused guide to diagnosing and resolving Cloudflare Error 521, covering origin server checks, web service restarts, firewall rules, resource monitoring, and prevention strategies.
Server & DevOpsHow to Write Alerts That Wake a Human Only When a Human Is Needed
An on-call rota fails long before anyone quits, at the moment the team stops reading the pages. This guide covers the three changes that keep that from happening: alerting on what a customer can feel rather than on a cause, replacing threshold pages with burn rate alerts against an objective, and grouping so that one incident produces one notification. It ends with a query that tells you which of your alerts nobody has acted on, and what to do with them.
Server & DevOpsHow to Migrate a Server to AWS Without a Big Bang Cutover
A big bang cutover is a plan with exactly one attempt in it. The incremental version costs a little more elapsed time and keeps a working rollback available until the very last step. This walks through the inventory that decides whether the cutover is clean, continuous replication that runs while the old server keeps serving, a dress rehearsal you can repeat, the DNS time to live arithmetic you have to do backwards from the cutover date, and the single action that ends the rollback window for good.