How to Fix the WordPress Database Connection Error (2026 Guide)

Fix WordPress database connection error

Open your site one morning and instead of your homepage you see a single stark line: “Error establishing a database connection.” No layout, no content, no admin area — just that message. It looks catastrophic, but usually it is not: your content is still safe in the database; WordPress simply cannot reach it. This guide shows you how to fix WordPress database connection error problems step by step, starting with the most common cause.

What the WordPress Database Connection Error Means

WordPress stores almost everything — posts, pages, users, settings, menus — in a MySQL or MariaDB database. The files on your server hold the theme, plugins, and uploads, but the content lives in that database. To build a page, WordPress reads four connection details from wp-config.php in your site root: the database name, username, password, and host.

When any of those details is wrong, or the database server does not answer, WordPress has nothing to build the page from, so it shows the database connection error instead. WordPress’s own documentation on common WordPress errors lists the same first checks. The message itself does not say which part failed, so work through the checks below in order.

Before You Touch Anything: Back Up First

Before editing wp-config.php or repairing tables, back up what you can still reach. If your host offers a one-click backup or snapshot, take one now, and at minimum download a copy of wp-config.php so you can restore the original values if a change makes things worse. If you do not have a backup routine yet, read my guide on how to back up your WordPress site once the site is back online.

Step 1: Diagnose the Error — Front End vs. wp-admin

Check these two places:

  1. Open your site’s front end and note the exact message.
  2. Now open yoursite.com/wp-admin.
  3. Compare what you see.

If wp-admin shows the same “Error establishing a database connection” message, the problem is the connection itself: wrong credentials in wp-config.php, a database user without the right privileges, or a database server that is down. Start with Step 2.

If wp-admin instead shows “One or more database tables are unavailable. The database may need to be repaired,” WordPress connected successfully but found a damaged table. Skip straight to Step 3 — you need a repair, not new credentials.

Step 2: Check wp-config.php Credentials

Wrong credentials are the most common cause, especially after a migration, a host move, or a password change. This is the first thing to verify when you fix WordPress database connection error issues, and it is also the most common place for a typo to hide.

Open your hosting File Manager or connect over SFTP, go to your site root, and open wp-config.php:

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

Now check the real values in your hosting panel — in cPanel under Databases, in Hostinger’s hPanel under Databases > Management. Compare character by character, including any prefixes your host adds. Database usernames and passwords are not your WordPress login credentials, and mixing them up is a classic mistake.

Pay special attention to DB_HOST. It is not always localhost. Some hosts use a specific hostname, such as a MySQL server address shown in the panel. If the panel gives you a hostname, use exactly that value. On some servers, switching localhost to 127.0.0.1 (or the reverse) also resolves the error, because the two can resolve to different connection methods — a Unix socket versus a TCP connection. Change one thing at a time and reload the site after each change.

If a database password was recently reset, update DB_PASSWORD to match, and update any other application using the same user. If you cannot find the correct values, ask your host’s support team to confirm them rather than guessing repeatedly.

Step 3: Repair Corrupted Database Tables

If wp-admin told you tables are unavailable, the database connected fine but one or more tables are damaged. Corruption can follow a server crash, a failed update, or a disk running out of space. WordPress ships with a built-in repair tool, but it is disabled by default for security.

  1. Add this line to wp-config.php, just above the line that says “That’s all, stop editing!”:
    define( 'WP_ALLOW_REPAIR', true );
  2. Visit yoursite.com/wp-admin/maint/repair.php and click Repair Database. WordPress checks every table and reports what it fixed.
  3. Reload your site. If it loads, a damaged table was the cause.
  4. Remove the WP_ALLOW_REPAIR line immediately. Leaving it in place lets anyone run the repair page on your site.

If you have WP-CLI access, the same check runs from the command line without touching wp-config.php:

wp db check
wp db repair

If the repair reports tables it cannot fix, restore those tables from a backup rather than deleting them — deleting a table deletes its content permanently.

Step 4: Check the Database Server and User Privileges

If the credentials are correct and the tables are sound, the database server itself may be the problem. Check your host’s status page first: a MySQL outage, a reboot, or a traffic spike that exhausted the connection limit produces exactly this error, and it clears on its own or with a host-side restart. This is also why the error sometimes appears and disappears — the server is refusing connections under load.

Next, confirm the database user still has privileges on the database. In your hosting panel, check that the user is assigned to the database with all privileges. This assignment can silently break during a migration, and it produces the same error as a wrong password.

Also check whether the database still exists. After a botched migration or an accidental deletion in the panel, it may be gone or renamed — in that case restoring from backup is the only recovery. A full server disk has a similar effect: MySQL cannot write, so connections fail, and your host can confirm disk usage.

Step 5: When Nothing Else Works — Restore or Reinstall Carefully

If every check above passes and the error persists, restore a backup from a known working state — files and database from the same point in time, so they stay consistent. Restoring only one half can create new mismatches.

Second, if you suspect damaged core files, reinstall WordPress core while preserving wp-config.php and wp-content. Never overwrite wp-config.php during a reinstall — it holds the credentials you just verified. This rarely fixes a genuine connection error, so treat it as a last resort. If the error still stands after a clean restore, escalate to your host with the steps you already tried; a server-side log will usually name the real cause.

How to Prevent the WordPress Database Connection Error

Once the site is back, a few habits make a repeat far less likely. Keep automated backups running on a schedule you have actually tested by restoring once. Always download a copy of wp-config.php before editing it, and never edit it in a word processor that can insert smart quotes. After any migration, verify the front end and wp-admin immediately and compare the credentials in wp-config.php against the new host’s panel — most migration-related connection errors are caught in that two-minute check. If this error recurs monthly, it is a hosting problem, not a WordPress problem, and moving hosts is the honest fix. For a migration done right, see my guide on migrating a WordPress site without breaking it.

Common Questions

Is my content lost when I see this error?

Usually not. The error means WordPress cannot reach the database, not that the database is empty. Do not delete or recreate the database before checking credentials and running a repair — recreating it is what actually destroys content.

Can a plugin cause this error?

Rarely, and only indirectly. A plugin cannot change your database credentials, but a faulty update can corrupt a table or crash a migration mid-write. If the error appeared right after an update, repair the tables first; if that fails, restore the pre-update backup. If your site shows a 500 error instead, see my guide on how to fix the WordPress 500 internal server error.

Should I increase PHP memory to fix it?

No. Raising the PHP memory limit does not fix a failed database connection, and it can mask the real problem. Fix the credentials, tables, or server issue first; tune memory separately if you later hit a genuine memory error.

The database connection error looks alarming because it takes down the whole site at once, but its causes are short and checkable: credentials, corrupted tables, user privileges, or the database server itself. Work through them in order and change one thing at a time. Most sites are back within the hour.

If your site is down with this error and you would rather have it fixed than troubleshoot it yourself, get in touch — send your site URL and what you changed most recently, and I will diagnose it and get it back online.