WordPress database connection error, how to fix it
Instead of your site, a short message appears: "Error establishing a database connection". The whole site disappears, the admin panel stops working too, and you do not know whether it is a minor thing or you have just lost your data. Let us reassure you right away: in most cases the data is intact, and the problem lies in the connection between WordPress and the database, not in the content itself. Below we explain what this error means, where it starts, and how to fix it step by step.
What "Error establishing a database connection" means
WordPress keeps all its content (posts, pages, settings, users) in a MySQL database. On every visit it connects to it using the credentials saved in the wp-config.php file. This message means the connection failed: either the database credentials are wrong, or the database server is not responding, or the database itself is corrupted. The site has nowhere to pull content from, so it shows a bare error.
The most common causes
- Wrong credentials in wp-config.php. A changed database password, name, or user, while
wp-config.phpstill has the old ones. Very common, especially after migrating the site to a different server. - The database server is overloaded or briefly down. During a traffic spike or on shared hosting, the database can refuse further connections. Then the error flickers: sometimes it is there, sometimes not.
- The database connection limit is exceeded. Cheap hosting limits the number of simultaneous connections. Once it runs out, every further visit gets this error.
- A corrupted database. A sudden server shutdown or a write error can corrupt tables. WordPress connects but cannot read the data.
- A sign of a hack or bot overload. Mass malicious queries can clog the database so it looks like a failure.
A quick 30-second diagnosis
Check one thing: whether the admin panel at /wp-admin shows the same error or a different one. If in the panel you see a message like "One or more database tables are unavailable. The database may need to be repaired", it is a sign the database is corrupted (go to repairing the tables). If both the site and the panel show an identical connection error, the problem is most often in the credentials or on the database server side.
How to fix it step by step
- Check the credentials in wp-config.php. In
wp-config.php, compareDB_NAME,DB_USER,DB_PASSWORD, andDB_HOSTwith what your hosting panel shows in the databases section. After a migration,DB_HOSTis often something other thanlocalhost, the host gives you the correct value. This is the most common and fastest fix. - Ask whether the MySQL server is running. If the credentials match, check the database status in your hosting panel or ask support directly whether the MySQL server is not overloaded or down. If the error flickers, this is usually it.
- Try repairing the tables. When you suspect a corrupted database, add the line
define('WP_ALLOW_REPAIR', true);towp-config.php, then visityourdomain.com/wp-admin/maint/repair.phpand run the repair. When done, be sure to remove that line from the file, because the repair page is then publicly accessible. - Check the load and bots. If the database falls over under load, look at the traffic stats. A sudden spike from one country or to one address is often bots. Limiting them (or a protective layer) takes the pressure off the database.
- Restore the database from a backup. If the tables are seriously corrupted, the cleanest option is to restore the database from a backup. If you keep regular backups, it is a matter of a few minutes.
If the site comes back after the fix but runs slowly or the database is cluttered, it is worth tidying it up afterwards.
When to call for help
If the credentials in wp-config.php match, the host says the database is working, and the error remains, or the tables are corrupted and you have no recent backup, it is the moment to stop experimenting on a live site. We handle exactly these failures, when a site is down: we diagnose for free, work on a copy, and recover the site without the risk of content disappearing in the process.
Frequently asked questions
Do I lose my site's content with this error?
Usually not. The most common cause is wrong credentials or an overloaded database server, not data loss. Even with corrupted tables, the content can usually be recovered through a database repair or a backup.
The error appeared right after moving the site to a new server. Why?
Because after a migration the database credentials change, most often DB_HOST and the password. The wp-config.php file still points to the old server. Entering the new details from your hosting panel usually resolves it immediately.
Sometimes the error is there, sometimes the site works. What is going on?
A flickering error is almost always overload: the database server or the connection limit refuses to serve requests under heavier traffic, and works again under lighter traffic. The solution is to relieve the database or a stronger hosting plan.
What does "database repair" do, and is it safe?
WordPress's built-in repair tries to rebuild corrupted MySQL tables. It is safe, but because the repair.php page is accessible without a login during the repair, you must remove the WP_ALLOW_REPAIR entry from the config file afterwards.