WordPress cannot reach its MySQL database. The usual causes, in order: wrong database details in wp-config.php after a move or password change, a database server that is down or overloaded, or corrupted tables. Check them in that order.
What the error means
Every page in WordPress starts by connecting to the database with four values from wp-config.php: DB_NAME, DB_USER, DB_PASSWORD and DB_HOST. If that connection fails, WordPress cannot load anything and shows this message on every page.
1. Check wp-admin too
Open /wp-admin/. If it says "One or more database tables are unavailable. The database may need to be repaired", the connection works but tables are damaged; go to step 4. If it shows the same connection error, keep going.
2. Check the details in wp-config.php
Open your hosting panel's database page and compare the database name, user and host with wp-config.php. This breaks most often after moving the site or changing the database password.
define( 'DB_NAME', 'your_database' );
define( 'DB_USER', 'your_db_user' );
define( 'DB_PASSWORD', 'the-real-password' );
define( 'DB_HOST', 'localhost' );
DB_HOST is usually localhost, but some hosts use a hostname or 127.0.0.1; your panel shows the right one. If you are not sure of the password, reset it for that database user in the panel and update wp-config.php.
3. Check that the database server is up
Try logging into phpMyAdmin from your hosting panel. If that fails too, the database server is down or overloaded. Check your host's status page and contact support. On shared hosting, a sudden traffic spike or a runaway plugin can hit the connection limit; the error then comes and goes.
4. Repair corrupted tables
Add this line to wp-config.php, then open /wp-admin/maint/repair.php and click Repair Database:
define( 'WP_ALLOW_REPAIR', true );
Remove the line as soon as you are done: while it is there, anyone can open the repair page without logging in.
If you see the install screen instead
If WordPress asks you to install it again after a move, the connection works but the table prefix is wrong. Check that $table_prefix in wp-config.php matches the prefix of the tables in phpMyAdmin.
How to stop it happening again
- Keep daily database backups stored off the server.
- Clean revisions, spam and expired transients so tables stay small. Database Monitor & Cleanup shows a health score and does this on a schedule.
- Watch for plugins that run heavy queries; see how to monitor the WordPress database.
Frequently asked questions
What causes "Error establishing a database connection"?
Wrong database details in wp-config.php, a database server that is down or overloaded, or corrupted database tables.
Where are the database details in WordPress?
In wp-config.php in the site's root folder: DB_NAME, DB_USER, DB_PASSWORD and DB_HOST.
How do I repair the WordPress database?
Add WP_ALLOW_REPAIR set to true in wp-config.php, open /wp-admin/maint/repair.php, run the repair, then remove the line.
Why does the error come and go?
Usually the database server is overloaded or hitting its connection limit during traffic spikes. Ask your host and look for heavy plugins.
Free tool: Database Monitor
Health score, table manager, one-click clean-ups and scheduled optimisation for the WordPress database.