"Error establishing a database connection" – causes
Four causes come into question, and they can be ruled out in this order – from the credentials to an overloaded database server.
This message does not mean your data is gone. It means WordPress cannot reach the database server right now. Check in this order: have the credentials changed, is the database server running, is the database damaged, or is the server overloaded. The first two cases together account for the large majority.
1. Credentials in wp-config.php
Four values live there: DB_NAME, DB_USER, DB_PASSWORD and DB_HOST. If one is wrong you get exactly this message. Typical triggers are a move to another host, a backup restored from a different environment, or a database password changed in the customer area.
DB_HOST is the most common stumbling block: it is not localhost everywhere. Some providers use a dedicated host name or a different port. The correct value is in the host's customer area.
2. Is the database server running at all?
On shared hosting you notice it because other sites at the same provider are down too – a glance at the host's status page settles it in seconds. On your own server the service may have crashed, often after a reboot or because memory ran out.
A reliable test without WordPress: log in to phpMyAdmin or Adminer with the same credentials. If that works, the server is not the problem, the configuration is. If it does not, the host is responsible.
3. and 4. Damaged tables, overloaded server
If the error appears only in the admin area while the site runs, or the other way round, that points to damaged tables. WordPress ships a tool for it: set WP_ALLOW_REPAIR to true in wp-config.php and open /wp-admin/maint/repair.php. Remove the line afterwards – that page is reachable without logging in.
If the error only shows at peak times and disappears by itself, the database server is overloaded. No change to WordPress helps there, only finding the cause: a plugin with expensive queries, missing caching, or simply a hosting package that is too small.
Sources and further documentation
Frequently asked questions
Is my content lost?
Very probably not. The message concerns the connection, not the content. As soon as the connection works, everything is back. Only with damaged tables can something be missing in places – and usually not much even then.
Can I leave WP_ALLOW_REPAIR switched on?
No. While the line is active, **anyone** can open the repair page without being logged in. That is deliberate, so you can reach it when the database is broken – and exactly why the line has to come out again right after the repair.
More guides
Update failed: site stuck in maintenance mode
Read article →WordPress shows a white screen – what to do?
Read article →Have a project in mind?
Get in touchAI assistant — may make mistakes. Not legally binding.