Error Establishing a Database Connection in WordPress
Fix the WordPress error establishing a database connection: check the four wp-config.php credentials, confirm the database server is up, then repair tables.
You load your site and the page is replaced by one line: Error establishing a database connection. Nothing else renders, because WordPress stopped before it could build a page. The front end shows only that sentence, with an HTTP 500 status. /wp-admin/ shows a longer version that says what went wrong, and that difference is the quickest clue you have.
TL;DR: it is usually one of three things, and this is the order to check them: a wrong value in the four database constants in wp-config.php (the usual cause, according to WordPress’s own Common WordPress Errors page, and the first suspect after a host move), the database server refusing connections (often temporary), or damaged or missing tables. Open /wp-admin/ first to read the long message, then test each in order. Don’t jump to “reinstall WordPress” or “clear your cache”; neither touches the database connection.

Here is the mental model that makes this quick to fix. WordPress keeps all your content (posts, pages, settings, users) in a MySQL database, not in files. On every request it reads four login details from wp-config.php, connects to that database, and pulls what it needs. This error means that step failed. The fix is to work out where it failed.
Read the long version of the message first
WordPress 7.1.4 keeps the front-end message short on purpose. Open https://yoursite.com/wp-admin/ instead. The database check runs before the login check, so you get the detailed message even when you’re logged out. This is what each version means (read from the 7.1.4 source; the install redirect is also from our test):
What /wp-admin/ shows | What it means | Go to |
|---|---|---|
”Error establishing a database connection” followed by “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 [your DB_HOST] could not be established.” | WordPress could not log in to the database server at all. Check the host name it prints | Step 1, then Step 2 |
| ”Cannot select database” followed by “The database server could be connected to (which means your username and password is okay) but the [name] database could not be selected.” | The login worked. DB_NAME is wrong, the database doesn’t exist, or the user has no rights on it | Step 1, DB_NAME and DB_USER |
| ”One or more database tables are unavailable. The database may need to be repaired.” | WordPress connected but couldn’t read its settings table, while other WordPress tables exist | Step 3 |
A redirect to /wp-admin/install.php | WordPress connected but found no tables with the $table_prefix in wp-config.php | The prefix check below |
The front end shows the same short sentence for the first three. A redirect to the installer happens on the front end too.
Two things change what you see:
- A file at
wp-content/db-error.phpreplaces all of these messages with its own page, front end and admin alike. If you see a branded error page, rename that file to read WordPress’s own message. WP_DEBUGandWP_DEBUG_DISPLAYboth on make WordPress print the full message on the front end too, with MySQL’s own error (such as “Access denied for user …”) above it. That also shows your database host to every visitor, so prefer the log: withWP_DEBUGandWP_DEBUG_LOGon andWP_DEBUG_DISPLAYoff, WordPress stops hiding PHP’s connection warning, anddebug.loggets the MySQL error code. That is read from the 7.1.4 source, not run. WordPress debug mode shows how to turn those on and off safely.
About the install redirect. In our test (2026-10-31, WordPress 7.1.4, PHP 8.3.33, WordPress Playground with its SQLite database), changing only $table_prefix in wp-config.php sent both / and /wp-admin/ to /wp-admin/install.php. Don’t run the installer. Put the prefix back to the one your tables use (look at the table names in phpMyAdmin, for example wp_options means wp_). A half-finished database import gives the same redirect.
The three causes, in order of likelihood
- A wrong credential in
wp-config.php, the usual result of moving hosts. - The database server is down or overloaded, the usual result when nothing changed on your side.
- Damaged or missing tables, less common, and it has its own message in wp-admin.
Work through them in that order. The decision tree below is the same three steps as a flowchart, so you can see which branch you’re on before you start editing files.
Three decision points: wrong credential, server not reachable, or damaged tables. Everything after this section is the detail for each box.
| Symptom | Likely cause | Step to run |
|---|---|---|
| Error appeared right after a host move or server change | Wrong DB_NAME, DB_USER, DB_PASSWORD or DB_HOST in wp-config.php | Step 1: compare all four against the host’s control panel |
| wp-admin says “Cannot select database” | Wrong DB_NAME, or the user isn’t assigned to that database | Step 1: check the name and the user’s privileges |
| Error appeared with no change on your side, especially during a traffic spike | Database server overloaded or down | Step 2: run the mysqli test script |
| wp-admin says “One or more database tables are unavailable” | Damaged or missing tables | Step 3: run repair.php |
| Some pages load and others show the error | A page cache serving stored copies while the database is down | Step 2, testing with /wp-admin/ |
repair.php itself can’t connect | Nothing to repair; the connection is still the problem | Step 4: back to Steps 1 and 2, or restore a backup |
Step 1: check the four credentials
Open wp-config.php in the root of your site over SFTP or your host’s file manager (what this file is and how to edit it safely). Four lines define the connection:
define( 'DB_NAME', 'your_database_name' );
define( 'DB_USER', 'your_database_user' );
define( 'DB_PASSWORD', 'your_database_password' );
define( 'DB_HOST', 'localhost' );
Each must match exactly what your host assigned. Open your hosting control panel’s database section (in cPanel it is “MySQL Databases”) and compare character for character:
DB_NAME: hosts often prefix the name with your account, likecpaneluser_wpdb. The prefix is part of the name. WordPress’s own “Cannot select database” message asks the same question.DB_USER: the same prefixing applies, and the user must be assigned to that database, not just exist.DB_PASSWORD: one wrong character is enough to break the login. If you are unsure, reset it in the control panel and paste the new value in. Watch for a trailing space or a curly quote.DB_HOST: don’t assumelocalhost. Many hosts use a separate database server with an address likemysql.yourhost.com, sometimes with a:port. The control panel shows the right value, and the long wp-admin message prints the host WordPress actually tried.
Because this file is where these four values live, the safest way to get a clean, correctly quoted block is the wp-config.php generator. Fill in the four database fields and paste the result over the old lines.
This step alone fixes the error after most migrations, because credentials that were right on the old host are wrong on the new one. If you also moved to a new domain, the next problem is the old address stored in the database; search and replace the database URL safely covers that.
Step 2: confirm the database server is actually up
If the credentials are correct and the error persists, especially if it appeared on its own, the database server itself is the suspect.
On shared hosting this is common and often temporary: the MySQL service gets overwhelmed during a traffic spike or hits its per-account connection limit and starts refusing new connections. Reload after a short wait before doing anything drastic. If some pages load and others don’t, don’t read that as a working database. A page cache’s advanced-cache.php drop-in runs before WordPress connects (read from the 7.1.4 wp-settings.php), so cached pages can keep loading while the database is down. Test with /wp-admin/, which the cache doesn’t serve.
To test whether the credentials work outside WordPress, put a small script next to wp-config.php:
<?php
mysqli_report(MYSQLI_REPORT_OFF);
$link = mysqli_connect('localhost', 'your_database_user', 'your_database_password');
if (!$link) {
die('Connection failed: ' . mysqli_connect_error());
}
echo 'Connected: the server and credentials are fine.';
Keep the mysqli_report line. Since PHP 8.1, mysqli throws an exception when a connection fails. Without that line the script stops before its die() runs, and a live server with display_errors off shows a blank page or a server error instead of the reason. The line switches mysqli back to returning false, the same setting WordPress’s own database class uses before it connects.
Use your real DB_HOST, DB_USER and DB_PASSWORD. If it prints Connected, your login details work and the problem is elsewhere (the database name, or the tables in Step 3). If it prints a connection error, the message tells you which: “Access denied” means a wrong user or password; “Can’t connect to MySQL server” means a wrong host or a service that is down, so contact your host. Delete the script as soon as you are done.
Step 3: repair damaged tables
The sign of damaged tables is the wp-admin message “One or more database tables are unavailable. The database may need to be repaired.” WordPress shows it when it can connect, can’t read its settings table, and finds other WordPress tables in place. The front end still shows the short connection sentence, so check wp-admin before you decide.
WordPress has a built-in repair tool. Add one line to wp-config.php, above the “stop editing” comment:
define( 'WP_ALLOW_REPAIR', true );
Then visit this URL directly in your browser:
https://yoursite.com/wp-admin/maint/repair.php
It loads without a login, which is the point, since you may be locked out. It offers “Repair Database” and “Repair and Optimize Database.” The first runs MySQL’s CHECK TABLE on each WordPress table and runs REPAIR TABLE only on tables that fail the check. The second also runs ANALYZE TABLE and OPTIMIZE TABLE, and the page warns that the database is locked while it optimizes.
One limit worth knowing: the MySQL manual lists REPAIR TABLE as working for MyISAM, ARCHIVE and CSV tables. InnoDB, the engine most WordPress tables use on current MySQL, is not on that list. If the results page says “Failed to repair” for an InnoDB table, restoring a backup (Step 4) or your host’s help is the way forward.
Then delete that line from wp-config.php straight away. While it is there, anyone on the internet can open that URL and run a repair on your database. WordPress’s own results page asks you to remove it.
Step 4: when repair will not even connect
If the repair page shows the connection error itself, there is nothing to repair. The database can’t be reached, so you’re back to Step 1 or Step 2. If those check out and the site is still down, restore the database from a recent backup. Many hosts keep automatic backups, so check your control panel or ask support before you go hunting through tables you can’t connect to.
Advice you can safely ignore
“Just reinstall WordPress.” This error is about the database connection, not core files. Reinstalling replaces files that are working fine and touches nothing that is broken.
“Increase your PHP memory limit.” Memory exhaustion is a different error with a different message, covered in fixing the allowed memory size exhausted error. Raising the limit does nothing for a refused database connection.
“Clear your cache.” The failure happens in PHP before WordPress can build a page. Clearing the cache changes nothing while the connection fails, and a page cache can even hide the problem by serving stored pages. Clear it after the site is back.
“Edit the database directly to fix it.” Reaching for phpMyAdmin to hand-edit tables before you’ve confirmed the connection works is how a temporary outage becomes data loss. Confirm the connection first, and touch the data last, with a backup in hand.
Still stuck?
If the credentials work in the test script, the server is up, and repair connects and reports no errors, but the site still shows the message, look for something that talks to the database on its own. A wp-content/db.php drop-in (from a caching or database plugin) replaces WordPress’s database class and may have stored an old host. Rename wp-content/plugins to plugins-off over SFTP and move any db.php aside to rule them out. If the error clears, bring them back one at a time until it returns.
Related WordPress failures
A database connection error is only one way WordPress can go dark. If you fix it and a different broken page shows up, these are the usual next suspects:
- There has been a critical error on this website: the same “whole site down” look, but the cause is a PHP fatal error in a plugin or theme, not the database. Both replace every page with one line of text.
- The WordPress white screen of death: a blank page instead of a message, usually a PHP fatal error that WordPress couldn’t report.
- WordPress HTTP error on image upload: a narrower failure that only affects media uploads, worth ruling out once the database is working.
The line on screen is the tell: “Error establishing a database connection” is this guide, “There has been a critical error” is the PHP guide above, and an empty white page is the white-screen guide. If debug.log shows a PHP fatal error rather than a database problem, paste it into the WordPress error log decoder to see which plugin or theme it points at.
Sources
- WordPress Developer Resources: wp-config.php (database settings, WP_ALLOW_REPAIR)
- WordPress Advanced Administration: Common WordPress errors
- WordPress 7.1.4 source: class-wpdb.php (connection and select messages, mysqli_report), wp-load.php (where wp-config.php is loaded from), functions.php (dead_db, is_blog_installed), wp-admin/maint/repair.php, wp-settings.php
- PHP manual: mysqli::__construct / mysqli_connect(), mysqli_driver::$report_mode (default changed in PHP 8.1)
- MySQL 8.4 Reference Manual: REPAIR TABLE Statement
William Lopez is the founder of Odie Themes. He builds the free WordPress tools and themes on this site and has been on WordPress.org since December 2019.
FAQ
Questions
What does error establishing a database connection mean in WordPress?
It means WordPress read wp-config.php, tried to use your MySQL database with the details it found there, and failed before it could build a page. Usually a credential is wrong or the database server is down or overloaded. The same short sentence also appears when WordPress connects but cannot select the database, or finds some of its tables missing. Open /wp-admin/ to see the longer message that tells these apart.
Which file holds the WordPress database login?
wp-config.php, usually in the root of your site next to wp-load.php. If it is not there, WordPress also loads it from one folder above, as long as that folder is not another WordPress install. Four constants define the connection: DB_NAME, DB_USER, DB_PASSWORD and DB_HOST. A single wrong character in any of them produces this error, so check all four first after a host migration.
Why did the error appear when I did not change anything?
Usually the database server, not your site. On shared hosting the MySQL service can get overloaded or hit its connection limit during traffic spikes and refuse new connections. It often clears on its own within minutes. If it keeps happening, ask your host, or you may have outgrown the plan.
Is DB_HOST always localhost?
No, and assuming it is causes this error after many migrations. Plenty of hosts run the database on a separate server, so DB_HOST is an address like mysql.yourhost.com or an IP with a port. Your hosting control panel's database section shows the correct value. Copy it exactly, including any :port suffix.
How do I repair a corrupted WordPress database?
Add define( 'WP_ALLOW_REPAIR', true ); to wp-config.php, then visit yoursite.com/wp-admin/maint/repair.php and run the repair. It needs no login, which is why you must delete that line as soon as you are done. It checks every table and repairs the ones that fail, but MySQL can only repair MyISAM, ARCHIVE and CSV tables that way, so a damaged InnoDB table needs a backup. If the page will not connect at all, the problem is credentials or the server, not corruption.
Why does only wp-admin show the error and the front end loads?
Usually because a page cache is serving stored copies of your pages. A caching drop-in runs before WordPress connects to the database, so cached pages can load while the database is down. Test with a page the cache has not stored, or with /wp-admin/, which shows the longer message. The two versions of the error are also different: the front end shows one short line, wp-admin shows the detail.