TalkToWPTalkToWP
Log inStart Free
SoloOne WordPress site, watched and fixed for you.AgencyEvery client site in one fleet view.
NoticeboardCustomer storiesBlogFAQSecurity
Solo pricingAgency pricing
Log inStart Free

Error Establishing a Database Connection in WordPress: Fix

AG Abhishek Gupta Published 10 Nov 2026 7 min read
Quick answer

WordPress can't reach MySQL. Check DB_NAME, DB_USER, DB_PASSWORD and DB_HOST in wp-config.php, confirm the database server is up, then run WP_ALLOW_REPAIR.

“Error establishing a database connection” means WordPress could not log in to its database, so it cannot load any content. The usual causes are wrong credentials in wp-config.php, a database server that is down or overloaded, or a corrupted database. Check the four database settings first, ask your host whether the database server is running, then try WordPress’s built-in repair.

Key Takeaways

  • WordPress stores everything in a database. If it cannot connect, the whole site fails.
  • Check DB_NAME, DB_USER, DB_PASSWORD and DB_HOST in wp-config.php first.
  • If the credentials are right, the database server may be down, which is a host issue.
  • WP_ALLOW_REPAIR can fix corrupted tables, but remove it afterwards.
  • Take a backup before editing files, and export the database if you can reach it.

This error is frightening because the whole site disappears, front end and wp-admin both. The content is almost always still safe in the database. The problem is the connection between WordPress and it.

What Does “Error Establishing a Database Connection” Mean?

WordPress keeps posts, pages, settings and users in a MySQL or MariaDB database. On every page load, PHP connects to that database using four details from wp-config.php. If the connection fails for any reason, WordPress shows this message instead of your site.

You might see the message on the front end only, or also on wp-admin. If wp-admin shows a different message such as “One or more database tables are unavailable”, that points to corruption and the repair step below.

What Causes It?

  • Wrong credentials in wp-config.php, often after a migration, a host change or a password reset.
  • The wrong DB_HOST value. Many hosts use localhost, but some use a separate server name.
  • The database server is down or restarting, or has hit its connection limit during a traffic spike.
  • A deleted or renamed database or database user.
  • Corrupted tables, for example after a crash or a failed update.
  • A full disk or exceeded quota on the hosting account.
  • Hacked or modified files in rare cases.

How Do You Fix It?

Make a backup of your site files before you edit anything. If your host provides phpMyAdmin or a database export tool, take a database export too.

1. Check the wp-config.php credentials

Open wp-config.php in the site root through FTP, SFTP or your host’s file manager. Find these four lines:

define( 'DB_NAME', 'your_database_name' );
define( 'DB_USER', 'your_database_user' );
define( 'DB_PASSWORD', 'your_database_password' );
define( 'DB_HOST', 'localhost' );

Compare each value with what your host’s control panel shows under MySQL Databases or similar. Watch for typos, trailing spaces and changed passwords. Many hosts add a prefix to the database and user name (such as account_dbname), so the full name matters. If your host gave you a specific hostname for the database, use that for DB_HOST instead of localhost.

If you recently changed the database user’s password, update DB_PASSWORD to match.

2. Test the credentials outside WordPress

If you are not sure, log in to phpMyAdmin with the same user and password from wp-config.php. If the login fails, the credentials are wrong or the user lacks permission on that database. Fix this in your host’s panel by resetting the password or assigning the user to the database with all privileges.

3. Ask whether the database server is running

If the credentials are correct, check your host’s status page and contact support. Ask if the MySQL or MariaDB service is running and whether your account hit its resource or connection limit. On shared hosting a busy neighbour or a traffic spike can cause brief outages. If you manage your own server, check the service status and restart it, and look for a full disk.

4. Repair corrupted tables

If the connection works but tables are damaged, add this line to wp-config.php above the “stop editing” comment:

define( 'WP_ALLOW_REPAIR', true );

Then visit https://yoursite.com/wp-admin/maint/repair.php in your browser (replace the domain with yours). Click “Repair Database”. WordPress will check and fix the tables.

Important: this page needs no login, so anyone can open it while the line is in place. Remove the WP_ALLOW_REPAIR line as soon as you finish.

5. Check the site address and table prefix

If you recently moved the site, confirm that $table_prefix in wp-config.php matches the prefix of the tables in the database (the default is wp_). A mismatch can make WordPress think there is no data.

6. Restore from a backup as a last resort

If the database has been deleted or is badly damaged, restore the most recent backup through your host or backup tool. Accept that you may lose recent changes.

How Do You Prevent It?

  • Keep regular, tested backups of both files and database, stored away from your server.
  • Record your database details somewhere safe, and update wp-config.php straight after any password or host change.
  • Pick hosting with headroom. If the site hits connection limits often, upgrade or add caching so fewer requests reach the database.
  • Clean up the database now and then, by removing old revisions and spam, to reduce load.
  • Watch the site. Database outages often come in short bursts that you will miss without continuous monitoring.

TalkToWP checks every site every 5 minutes on every plan, Free included, so you hear about a database failure within minutes and get a plain-English explanation. The free plan covers one site, and you can sign up here. If what you see is a server error code instead, read about the 500 Internal Server Error. A critical error message points to a plugin or theme rather than the database.

Frequently Asked Questions

Have I lost my WordPress content?

Almost certainly not. The error means WordPress cannot reach the database, not that the data is gone. Once the credentials or server problem is fixed, your posts and pages return. Back up first if you can reach the database, in case a repair goes wrong.

Why does the error only appear sometimes?

Intermittent errors usually mean the database server is overloaded or hitting its connection limit during busy periods. This is common on shared hosting. Caching, fewer heavy plugins or a better hosting plan reduces it, and your host can confirm the limits.

What should DB_HOST be?

On many hosts it is localhost. Others use a specific hostname or an IP address, sometimes with a port such as :3306. Your host’s documentation or control panel shows the right value, so check it rather than guessing.

Is WP_ALLOW_REPAIR safe to use?

It is safe if you use it briefly. The repair page does not require a login while the constant is set, so remove the line from wp-config.php immediately afterwards.

The error appears only in wp-admin. What does that mean?

If only the dashboard fails, with a message about tables being unavailable or needing repair, the database is usually reachable but damaged. Use the repair step. If the problem persists, restore a backup.

AG
Abhishek Gupta Founder of TalkToWP. Has been fixing WordPress sites for agencies and owners for over a decade.

Related answers

All answers
Monitoring Uptime Said 100%, Site Was Broken: 5 WordPress Cases Read AI and MCP WordPress MCP Servers for Multiple Sites, Compared (2026) Read Plugin updates Update All WordPress Plugins at Once? A Decision Table Read