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.
Frequently asked questions
What causes "Error Establishing a Database Connection" in WordPress?
WordPress shows the error when it cannot connect to MySQL or MariaDB using the credentials in wp-config.php. The usual suspects are wrong credentials (typos are common), a crashed or stopped database service, corrupted tables, and a server that has run out of memory, CPU or max connections. Hosting-level network or hardware problems and a plugin or theme hammering the connection pool can trigger it too.
Which wp-config.php values should I check when WordPress cannot connect to its database?
Four constants in wp-config.php control the connection, DB_NAME, DB_USER, DB_PASSWORD and DB_HOST, and DB_HOST is usually localhost. One wrong character in any of them breaks the connection. Compare each value with the database name, user and password shown in the hosting control panel. If the password is uncertain, reset it under MySQL Databases in cPanel or Plesk and update wp-config.php to match.
How do I run the built-in WordPress database repair tool?
Add define('WP_ALLOW_REPAIR', true); to wp-config.php just above the "That's all, stop editing!" comment, then open /wp-admin/maint/repair.php on the site in a browser. Choose Repair Database or Repair and Optimize Database. Remove the WP_ALLOW_REPAIR line as soon as the repair is done, because leaving it in place is a security risk.
How can I tell if the WordPress database connection error is a WordPress problem or a MySQL problem?
A small PHP test file settles the question. Save a test-db.php in the WordPress directory that calls mysqli_connect with the same host, user, password and database name and prints either the connection error or "Connected successfully", then load it in a browser. A successful connection points to WordPress itself, most likely a plugin, while a failure points to the credentials or the MySQL service. Delete test-db.php right after the test.
Will restarting MySQL fix "Error Establishing a Database Connection" in WordPress?
Often it will, when the cause is a transient problem. On a VPS or dedicated server with SSH access, run sudo systemctl restart mysql and reload the site. If the error comes back, the server logs under /var/log/ usually say why, for example ERROR 1045 (28000) for access denied, or out-of-memory messages.
How do I check if a WordPress plugin is causing the database connection error?
Rename the plugins folder over SSH or FTP with mv wp-content/plugins wp-content/plugins_disabled, which switches every plugin off at once. A misbehaving plugin can exhaust database connections, so if the site comes back after the rename, one of the plugins is to blame. Re-enable them one by one to find the culprit.
Does reinstalling WordPress core files delete themes, plugins or uploads?
No. Replacing only the wp-admin and wp-includes folders with the ones from a fresh copy of WordPress repairs corrupted core files and leaves wp-content alone. Themes, plugins and uploads stay untouched as long as wp-content is not overwritten.
Which MySQL settings should I tune on a VPS to stop WordPress database connection errors?
Bad MySQL tuning is a common root cause on a VPS or dedicated server. Raise innodb_buffer_pool_size if MySQL keeps running out of cache, set max_connections to a realistic figure for the site's traffic, and adjust wait_timeout and interactive_timeout so stale connections get cleaned up. When CPU, memory or process counts regularly spike to the plan's limits, the options are caching, fewer plugins or a bigger hosting plan.
Read how we handle it in DevOps for e-commerce.
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.