Quick answer: “Error establishing a database connection” means WordPress can’t talk to its MySQL/MariaDB database. The most common causes are wrong database details in wp-config.php (name, username, password or host), a database server that’s down or overloaded, corrupted database tables or a database user without permissions. Check your credentials first, ask your host whether the database server is up, then repair the database if needed.
What the error means
WordPress stores all your content and settings in a database. Every time someone visits, WordPress connects to that database using the details in wp-config.php. If the connection fails, WordPress shows this error instead of your site. Your content is almost always still safe — it’s the connection that’s broken.
Common causes
| Cause | Clue |
|---|---|
| Wrong database name, user or password | Happens right after moving hosts or changing passwords |
| Wrong DB_HOST | Host doesn’t use localhost |
| Database server down | Several sites on the same host are down |
| Too many connections / traffic spike | Error appears at busy times, then disappears |
| Corrupted tables | Admin shows “One or more database tables are unavailable” |
| Database user lost permissions | After hosting changes |
Step 1: Check the database details in wp-config.php
Open wp-config.php in your site’s root folder (via your host’s file manager or SFTP) and find these lines:
define( 'DB_NAME', 'database_name' );
define( 'DB_USER', 'database_user' );
define( 'DB_PASSWORD', 'password' );
define( 'DB_HOST', 'localhost' );
Compare them with the database details in your hosting panel (usually under MySQL Databases). A single wrong character is enough to cause the error. If you’re not sure of the password, reset the database user’s password in your hosting panel and update wp-config.php to match.
Step 2: Check DB_HOST
Most hosts use localhost, but some use a separate database server with its own address (and sometimes a port). Your host’s documentation or support can confirm the right value.
Step 3: Check whether the database server is running
If the details are correct, the database server may be down or overloaded. Signs: other sites on your account are also down, or phpMyAdmin won’t open. Contact your host — they can restart the service or tell you about an outage. On shared hosting, a traffic spike can also hit connection limits; upgrading your plan or adding caching helps.
Step 4: Test the connection
Try logging in to phpMyAdmin with the same database username and password. If that fails, the credentials or permissions are wrong. If it works, the problem is likely corrupted tables or the server under load.
Step 5: Make sure the user has permissions
In your hosting panel, check that the database user is assigned to the database with All Privileges. After migrations, this link is sometimes missing.
Step 6: Repair the database
If the dashboard says tables need repair, add this line to wp-config.php:
define( 'WP_ALLOW_REPAIR', true );
Then visit yoursite.com/wp-admin/maint/repair.php and click Repair Database. Remove the line afterwards — the repair page doesn’t require a login while it’s enabled. You can also repair tables from phpMyAdmin (select tables → Repair table).
Step 7: Check the site URL after a migration
After moving hosts or domains, the site and home URLs in the database (wp_options table) may still point to the old address. Update them, or set WP_HOME and WP_SITEURL in wp-config.php. See how to move a WordPress site.
Step 8: Restore a backup
If the database is badly damaged, restoring a recent backup is often fastest. See how to restore a WordPress backup.
Quick fix table
| Situation | Fix |
|---|---|
| Just moved hosts | Update DB_NAME, DB_USER, DB_PASSWORD, DB_HOST |
| Just changed database password | Update DB_PASSWORD in wp-config.php |
| Error comes and goes at busy times | Add caching; upgrade hosting |
| Tables marked as crashed | Run the repair tool |
| Every site on the account is down | Contact host about the database server |
How to prevent it
- Keep automatic backups of your database.
- Use caching to reduce database load — see our caching guide.
- Choose hosting with enough resources for your traffic.
- Write down database details safely when you migrate.
Frequently asked questions
Did I lose my content?
Usually not. The data is still in the database; WordPress just can’t reach it.
Why does the error appear only sometimes?
Intermittent errors usually mean the database server is overloaded or hitting connection limits.
Is it safe to use the repair tool?
Yes, but back up first and remove WP_ALLOW_REPAIR when finished.
Can a plugin cause this error?
Rarely. It’s almost always credentials, the server or corrupted tables.
Final thoughts
Start with wp-config.php — most cases are a simple credentials mismatch. If those are right, your host can quickly check the database server. Official reference: editing wp-config.php.
A real-world example
A small business site suddenly showed “Error establishing a database connection” on every page. The owner hadn’t changed anything. After checking wp-config.php and finding the details correct, she contacted her host. The database server had run out of connections because another site on the same shared server was being hit by heavy bot traffic. The host moved her account to a less busy server, and the site came back within an hour. The lesson: if your settings are correct and nothing changed, ask your host to check the database server before you start editing files.
Can a hack cause this error?
Sometimes. Attackers who gain access to a server can change the database password, corrupt tables or overload the database. If the error appears alongside other warning signs — unknown admin users, strange files or redirects — treat it as a possible security incident. This was a real concern in October 2026, when the PHP2Shell vulnerabilities let attackers run code on unpatched servers. Ask your host to confirm the server was patched, check for compromise and change your database password.
How to check your database details step by step
Open wp-config.php in your hosting File Manager and find four lines: DB_NAME, DB_USER, DB_PASSWORD and DB_HOST. In your hosting control panel, open the MySQL or database section and compare each value. The database name and user often include a prefix from your hosting account, such as “abc123_wp”. If any value differs, correct it in wp-config.php and save. If you’re unsure of the password, create a new one for the database user in the control panel and paste it into wp-config.php. DB_HOST is usually “localhost”, but some hosts use a different address — check their documentation.
Repairing a corrupted database
If the error mentions that tables need repair, WordPress has a built-in repair tool. Add define( 'WP_ALLOW_REPAIR', true ); to wp-config.php, then visit yoursite.com/wp-admin/maint/repair.php and choose “Repair Database”. Remove the line from wp-config.php when you’re done, because the repair page doesn’t require a login. Always take a backup before repairing.
Preventing the error in future
Choose hosting with enough database resources for your traffic, keep regular backups, remove plugins that create huge amounts of database data, and clean up old revisions and transients from time to time. If you run a busy site or store, ask your host about object caching, which reduces the number of database queries on every page.
WordPressPrime.com is an independent guide and is not affiliated with the WordPress Foundation, WordPress.org or Automattic. Always back up your website before making changes, and check official sources for the latest details.
