
Nothing ruins a website owner’s morning faster than opening their site and seeing one stark sentence: “Error establishing a database connection.” No homepage, no dashboard, no content โ just that message. The good news is that this error establishing a database connection problem is one of the most common WordPress issues, and you can usually fix it in under half an hour once you know which of the four usual causes is behind it.
What Does “Error Establishing a Database Connection” Mean?
Every WordPress page is built on the fly from a MySQL or MariaDB database, where your posts, pages, settings and user accounts live. When WordPress cannot log in to that database, it cannot build a single page, so it stops and prints this message instead.
WordPress reads four values from the wp-config.php file in your site’s root folder: DB_NAME, DB_USER, DB_PASSWORD and DB_HOST (usually localhost). If any of those is wrong, or the database server is unreachable, the connection fails. Every cause shows the exact same message, so diagnose first and fix second.
Why Does This Error Happen? The 4 Main Causes
- Wrong database credentials in wp-config.php โ the most common cause, especially after a migration or password reset.
- The database server is down โ MySQL has stopped, crashed or is refusing connections.
- A crashed or corrupted database table โ usually after a failed update or sudden server restart.
- Hosting connection limits โ too many simultaneous connections on shared hosting, often under a traffic spike.
Before You Start: Back Up First
Before changing anything, take a backup of your site files and database through your hosting panel. Also check your host’s status page โ if their database servers are down, no fix on your side will help.
Fix 1: Check the Credentials in wp-config.php
Wrong credentials cause roughly four out of five cases, so start here. In your hosting control panel, open the databases section and note the exact database name, username and host value. Then open wp-config.php in your site’s root folder with your host’s file manager or an SFTP client, and compare the four lines:
define( 'DB_NAME', 'your_database_name' ); define( 'DB_USER', 'your_database_username' ); define( 'DB_PASSWORD', 'your_database_password' ); define( 'DB_HOST', 'localhost' );
Every character must match exactly โ one wrong letter or a changed password breaks the connection. If you recently reset the database password, update DB_PASSWORD here. If your host uses a remote database server instead of localhost, use the server name your host provides. Save the file and reload your site.
Still stuck? Add define( 'WP_DEBUG', true ); define( 'WP_DEBUG_LOG', true ); to wp-config.php and read the real MySQL error in wp-content/debug.log. Error 1045 means wrong credentials, “Connection refused” means MySQL is down, and error 1040 means too many connections. Switch debug mode off when you are done.
Fix 2: Check Whether the Database Server Is Down
If the credentials are correct, the database server itself may be the problem. Log in to your hosting panel and try opening phpMyAdmin. If phpMyAdmin also fails to connect, the MySQL server is down or overloaded โ that is your host’s problem, not your site’s. Contact support with the exact error, or restart MySQL yourself if you run a VPS.

Fix 3: Repair a Crashed Database Table
Sometimes the front of your site shows the connection error while /wp-admin/ shows a different message: “One or more database tables are unavailable.” That means a table has crashed. WordPress has a built-in repair tool: add this line to wp-config.php, just before “That’s all, stop editing!”:
define( 'WP_ALLOW_REPAIR', true );
Then visit yourdomain.com/wp-admin/maint/repair.php and click “Repair Database”. Remove the WP_ALLOW_REPAIR line immediately afterwards โ leaving it in place lets anyone run the repair tool on your site.
Fix 4: Fix Hosting Connection Limits
If the error comes and goes โ the site works, then fails under traffic, then works again โ your site is probably hitting the maximum number of simultaneous database connections your plan allows. Short-term, deactivate heavy plugins and enable caching so fewer requests hit the database. Long-term, move to a plan with a higher limit, or to a VPS where you control it. If your site keeps outgrowing shared hosting, read our guide on the signs you have outgrown your VPS.
How to Prevent This Error in the Future
- Keep automatic backups โ daily database backups turn a disaster into a ten-minute restore.
- Update wp-config.php whenever you change a database password โ do both in the same session.
- Keep WordPress, themes and plugins updated โ failed updates are a common cause of crashed tables.
- Use a caching plugin โ fewer database queries means fewer chances to hit connection limits.
FAQs: Error Establishing a Database Connection
Does this error mean my website is hacked?
Not usually โ most cases are a wrong password, a down server or a crashed table. But if your wp-config.php credentials changed and nobody on your team changed them, treat it as a security incident and scan your site for malware.
Will I lose my content?
Almost certainly not. The error means WordPress cannot reach the database, not that the database is gone. Once the connection is restored, everything comes back exactly as it was.
Can a plugin cause this error?
Rarely directly, but a badly coded plugin can open too many database connections or run heavy queries that exhaust the limit. If the error started right after installing a plugin, deactivate it and see if the site returns.
Conclusion
The “error establishing a database connection” message looks frightening, but it has only four usual causes, and you now know how to check each one. Start with the credentials in wp-config.php, check whether MySQL is running, repair any crashed tables, and look at hosting limits if the error keeps returning. Work through the fixes in order, keep a backup before you change anything, and your site will be back online with all its content intact.
Need more help with your site? Explore our free online tools and WordPress guides on KamiWeb.
