Skip to content
Black Friday 50% off + 15,000 credits Ends Nov 30 Ends Nov 30 see the deal
Black Friday15,000 free credits + 50% offends Nov 30

Error Establishing a Database Connection in WordPress: How to Fix It

WordPress shows this message when it can't reach its database, so every page goes down at once, wp-admin included. The cause is almost always one of four things: wrong details in wp-config.php, a database server that's down or overloaded, damaged tables, or a hosting limit. Here is how to tell which, and fix it.

Updated WPOSAI + WordPress How-TosMaintenance & Security
In this article
  1. 01The short answer
  2. 02What the message means
  3. 031. Check with your host first
  4. 042. Check the database details in wp-config.php
  5. 053. Test the login on its own
  6. 064. Repair damaged tables
  7. 075. When it comes and goes: load and limits
  8. 086. Still down?
  9. 09Where WPOS fits
Key takeaways
  • The message means WordPress can't reach its database. Your content is still there.
  • Check your host first: a database server that's down, overloaded or over a plan limit is common.
  • Compare DB_NAME, DB_USER, DB_PASSWORD and DB_HOST in wp-config.php with your hosting panel.
  • A five-line PHP test file tells you in seconds whether those details work.
  • Damaged tables: add WP_ALLOW_REPAIR, run wp-admin/maint/repair.php, then remove the line.
  • If the error comes and goes at busy times, cut database load with caching and ask about connection limits.

The short answer

WordPress couldn’t connect to its MySQL or MariaDB database. Your content is still there. Work through these in order:

  1. Check your host. Look at its status page and your hosting dashboard for an outage or a database over its limits.
  2. Check wp-config.php. Compare DB_NAME, DB_USER, DB_PASSWORD and DB_HOST with the database details in your hosting panel.
  3. Test the login on its own with a five-line PHP file, so you know whether the details work.
  4. Repair damaged tables if wp-admin says tables are unavailable.
  5. Cut the load if the error comes and goes at busy times.
  6. Still down? Check for a hacked site and restore a backup.

WordPress’s own guide to common errors gives the same order: wrong details in wp-config.php first, then problems at the web host, then a compromised site.

What the message means

Every page WordPress serves starts by connecting to its database with the four details in wp-config.php. When that connection fails, WordPress stops and shows a short message instead of the page:

  • Visitors see only “Error establishing a database connection”, with a 500 status code.
  • In wp-admin, or with debugging turned on, WordPress adds the long version: either the username and password in wp-config.php are wrong, or the database server couldn’t be reached, which could mean it’s down. It then asks whether the username and password are right, whether the hostname is typed correctly, and whether the database server is running.
  • “Error reconnecting to the database” means WordPress connected, lost the connection partway through the page, tried again five times a second apart, and gave up. That points at a database server that restarted or is under heavy load.
  • “One or more database tables are unavailable. The database may need to be repaired.” means the connection works but some tables are damaged (step 4).
  • “Cannot select database” means the username and password work, but the database named in DB_NAME doesn’t exist or that user can’t use it (step 2).
What you seeMost likely causeStart with
The error on every page, right after a move, a new host or a password changeWrong details in wp-config.phpStep 2
The error on every page, and you changed nothingThe database server is down or the account hit a limitStep 1
“One or more database tables are unavailable”Damaged tablesStep 4
The error comes and goes, often at busy timesToo many connections or an overloaded serverStep 5
“Error reconnecting to the database”The server restarted or is overloadedSteps 1 and 5

1. Check with your host first

If you didn’t change anything before the error appeared, the problem is most likely on the server. WordPress’s guide names two hosting causes: the database has reached its quota and been shut down, or the server is down.

  • Check your host’s status page for an outage.
  • In the hosting dashboard, look at the database’s size and any usage limits on your plan.
  • Ask support directly: is the database server up, did my account hit a limit, and can you send me the database error log?

On most shared and managed hosting you can’t restart the database yourself, so a quick message to support often settles it in minutes.

2. Check the database details in wp-config.php

If the error started after you moved the site, changed hosts or changed the database password, the details in wp-config.php are the first suspect. Open the file in the site’s root folder with your host’s file manager or SFTP. These four lines are the ones that matter:

define( 'DB_NAME', 'database_name_here' );
define( 'DB_USER', 'username_here' );
define( 'DB_PASSWORD', 'password_here' );
define( 'DB_HOST', 'localhost' );

Compare each value with the database section of your hosting panel (in cPanel, MySQL Databases). The usual mismatches:

  • A missing prefix. Many hosts put the account name in front of database and user names, such as acct_wp123. WordPress’s own message asks the same question.
  • A changed password. If you or the host changed the database user’s password, wp-config.php still has the old one. Set the same new password in both places.
  • The wrong host. localhost is right on many hosts, but some run the database on a separate server. Copy the exact host name the panel gives you, with the port if it shows one, such as db.example.com:3306.
  • A user without access. The user must be added to the database with full privileges, or WordPress gets “Cannot select database”.
  • Copying mistakes. Stray spaces, a missing quote or curly quotes from a word processor break the line.

Save the file and reload the site. With WP-CLI, wp config get DB_HOST prints a value from the command line.

3. Test the login on its own

To know for sure whether the details work, test them outside WordPress. Create a file called db-test.php in the site’s root folder with the same four values:

<?php
// db-test.php: delete this file as soon as you've read the result.
mysqli_report( MYSQLI_REPORT_OFF );
$link = mysqli_connect( 'localhost', 'username_here', 'password_here', 'database_name_here' );
echo $link ? 'Connected.' : 'Failed: ' . mysqli_connect_error();

Open https://yoursite.com/db-test.php in your browser:

  • Connected: the details are fine. Look at damaged tables (step 4) or load (step 5).
  • Access denied for user: the username or password is wrong, or the user isn’t allowed to use that database.
  • Unknown database: DB_NAME is wrong.
  • Connection refused, a timeout, or a host that can’t be found: the host name is wrong or the database server is down. Back to your host.

Delete the file straight away: it contains your database password. The mysqli_report line stops newer PHP versions from turning a failed login into a fatal error, the same thing WordPress does itself.

4. Repair damaged tables

If wp-admin shows “One or more database tables are unavailable. The database may need to be repaired.”, WordPress has a built-in repair page. Back up the database first if your host’s panel can still export it, then:

  1. Add this line to wp-config.php, above /* That's all, stop editing! */:
    define( 'WP_ALLOW_REPAIR', true );
  2. Open https://yoursite.com/wp-admin/maint/repair.php.
  3. Click Repair Database, or Repair and Optimize Database, and wait. Large databases take a while.
  4. Remove the line again. The wp-config.php documentation says why: while it’s there, anyone can use the repair page without logging in, because the page is built for sites whose users can’t log in.

With WP-CLI, wp db check and wp db repair do the same job. Many hosts also have a repair button in phpMyAdmin or the database section of their panel.

5. When it comes and goes: load and limits

An error that appears at busy times and clears by itself usually means the database server is refusing new connections: too many at once, a per-account limit on your plan, or a server short of memory or disk. Fixes, roughly in order:

  • Ask your host for the database error log and the connection limits on your plan. If the site has outgrown the plan, moving up is the real fix.
  • Turn on page caching, so most visits are served without touching the database. Our guide to speeding up WordPress covers caching and the rest.
  • Find the heavy plugin. Query Monitor (200,000+ active installs, updated 27 October 2026, tested with WordPress 7.1.4) shows the slowest database queries on each page and which plugin ran them.
  • Stop bots from hammering the site. Floods of login attempts and search requests all hit the database. Our WordPress security checklist covers login protection and firewalls.
  • Tidy the database once it’s stable: old revisions, spam comments and expired transients add up on older sites.

6. Still down?

  • Check for a hack. WordPress’s guide lists a compromised site as the next suspect once wp-config.php and the host check out. Look for changes to wp-config.php you didn’t make, and scan the site.
  • Restore a backup of the files and the database from before the problem started. Our guide to backing up WordPress covers what to keep and how to restore it.
  • Ask the host to restore the database from its own backups if yours are missing.
  • Show visitors a better page while you work. A file called db-error.php in wp-content replaces the bare message. On WordPress 7.1, have it send the status code itself:
<?php
// wp-content/db-error.php: shown instead of "Error establishing a database connection".
http_response_code( 503 );
header( 'Retry-After: 600' );
?>
<!doctype html>
<title>Back soon</title>
<h1>We'll be right back</h1>
<p>The site is having a technical problem. Please try again in a few minutes.</p>

A 503 tells search engines the outage is temporary. Once the site is back, set up uptime monitoring so you hear about the next outage before your visitors do. If the site loads but shows a blank page or a critical error message instead, our guides to the white screen of death and the critical error cover those.

Where WPOS fits

WPOS can’t fix this error from inside WordPress. Its agent works in wp-admin, and when the database is unreachable, wp-admin is down too, so start with your host and the steps above. Where it helps is afterwards: the agent checks its own work with browser screenshots before it reports a change as done, and the free trial comes with a staging sandbox next to the live site for trying plugin changes and updates first. A check after the fix becomes one request:

“Open the home, shop and contact pages and every page in the main menu, take a screenshot of each on desktop and phone, and list any that show a database error or a blank page.”

WPOS is our product, so weigh this section accordingly; you can try it free for 30 days with 5,000 credits, a live site plus a staging sandbox, and no card.

Frequently Asked Questions

WordPress couldn't connect to its database. Most often the database details in wp-config.php are wrong, for example after a move or a password change, the database server is down or overloaded, the account hit a hosting limit, or tables are damaged.

Almost certainly not. Your posts, pages and settings are still in the database; WordPress just can't reach it. Don't reinstall WordPress or create a new database: fix the connection and the site comes back as it was.

In wp-admin, WordPress shows the long version: either the details in wp-config.php are wrong or the database server couldn't be reached. Visitors only see the one-line message. If wp-admin says tables are unavailable, the database needs repairing instead.

Then the cause is usually on the server: the database is down, restarting, overloaded or over a limit on your hosting plan. Check your host's status page and ask support whether the database server is up and whether your account hit a limit.

No. While that line is in wp-config.php, anyone can open the repair page without logging in. Add it, run the repair, then delete the line straight away.

A short outage won't. Visitors and search engines get an error status, and search engines come back later. If it lasts for hours or keeps happening, pages can drop out of results until the site is stable, so fix it quickly and set up uptime monitoring.

Black Fridayends Nov 30

Start free with 15,000 credits.

Three times the usual 5,000, no card needed. Then 50% off your site licences for 2 months with FRIDAY50.

Claim 15,000 credits