A client's WordPress site says "Error establishing a database connection"
WordPress keeps every post, page and setting in a database, and it could not reach it. Nothing is usually lost: the website and the database are both still there, they just cannot talk. Here is the order we would work in.
1. What the message means
Every page on the site shows the same short message, "Error establishing a database connection", and answers with HTTP 500, a server error. It happens before WordPress loads any plugin or theme, so it is almost never a plugin's fault. There are four usual causes:
- The database server is down or overloaded. The most common one on shared hosting. If other sites on the same host fail at the same moment, it is this.
- The login details changed. Someone changed the database password in the hosting panel, or the site was moved to a new host, and
wp-config.phpstill has the old ones. - The database is damaged. WordPress then says "One or more database tables are unavailable. The database may need to be repaired."
- The server ran out of room or connections. A full disk, or a burst of traffic that used up every connection the host allows.
2. Get the longer message
Visitors usually get the short version. (If the site has WP_DEBUG and WP_DEBUG_DISPLAY switched on, everyone sees the long one, plus the database's own error. Switch display off again once it is fixed.) Open /wp-admin on the same site: on admin pages WordPress shows the longer message, which says "This either means that the username and password information in your wp-config.php file is incorrect or that contact with the database server at" the host name it tried. If the site is badly damaged instead, this is where you see the "may need to be repaired" message.
3. Check the login details
Open wp-config.php in the site's main folder, over SFTP or the host's file manager. Four lines matter:
define( 'DB_NAME', '...' );
define( 'DB_USER', '...' );
define( 'DB_PASSWORD', '...' );
define( 'DB_HOST', '...' );Compare them with the database section of the hosting panel. The name and user must match exactly, and DB_HOST is often localhost but not on every host: many managed hosts give a separate address. If you are not sure of the password, set a new one for that database user in the panel and put the same one in DB_PASSWORD. Before you do, check nothing else uses that database user, such as a staging copy of the site or a backup script: they would stop connecting until you update them too. Keep a copy of the file before you edit it.
4. If the details are right, ask the host
With correct details and the error still there, the database server itself is the problem, and only the host can fix that. Check their status page first. Then ask them plainly:
Since [time], WordPress on [domain] says "Error establishing a database connection". The details in wp-config.php match the database in the panel. Is the database server for this account up, and is the account hitting a connection or disk limit?
If it keeps happening at busy times, the account is too small for the traffic. A caching plugin cuts how often WordPress needs the database; a bigger plan fixes it.
5. If WordPress says the database needs repair
Add this line to wp-config.php, above the one that says "That's all, stop editing!":
define( 'WP_ALLOW_REPAIR', true );Then open /wp-admin/maint/repair.php and choose Repair Database. That page works without logging in, so anyone can open it while the line is there. Remove the line as soon as the repair finishes. Take a backup from the hosting panel first if you can.
6. Check the fix
Ask from a terminal rather than a browser that may show an old copy. This follows any redirects and prints the code the page finally answers with:
curl -sL -o /dev/null -w "%{http_code}\n" https://example.comOn Windows, type curl.exe and use NUL instead of /dev/null. It should print 200, not 500. If the host or a plugin caches pages, a page that looks fine may be an old stored copy, and so can the one you just tested. Log in to the dashboard: page caches skip logged-in users, so if the dashboard loads, the database is back.
7. Next time, hear about it first
WordPress sends no email for this error: it cannot even read the site's settings to find out where to send one. Bionic Uptime reads the status code on every check, and this page's HTTP 500 is a failed check. When both of our locations, on two different companies' networks, and a retry agree within 45 seconds, it is confirmed as an outage and each person on that website's alert list gets one email. For this error it says "Both checking locations received a server error (HTTP 500)." See the exact outage email.
Two cases a plain check cannot see. Some sites have a custom error page, a file called wp-content/db-error.php, which WordPress shows instead of its own message. It sends whatever code that file sets, and a normal 200 if it sets none. For that, turn on Also check what the page says when you add the website, choose Must say, and enter a phrase the real page always shows, such as text in the footer. The custom error page does not have it, so it fails the check. The check reads the first 256 KB of the page, and the form tests your phrase against the live page before it saves it. The other case is a page cache. While it still holds a stored copy of a page, it serves that copy to visitors and to every outside check alike, and nothing outside the server can tell the difference. We see the error once the cache stops covering it. More on monitoring WordPress sites.
3 websites are free, forever, with no card. Looking after several client sites?