🎉 Free WordPress fix for our first 50 sites — in exchange for an honest review. Claim a spot →

Errors

WordPress Database Connection Error: Fix It Step by Step

Jul 18, 2026 · 7 min read · By the Mend engineering team

"Error establishing a database connection" means WordPress tried to connect to MySQL and failed — the site can't load any content until that connection is restored. In most cases the cause is either wrong credentials in wp-config.php, a crashed MySQL service, or a host-side resource limit that killed the database process. Work through the steps below in order and you'll find the culprit quickly.

What You're Seeing — and What It Actually Means

WordPress stores everything — posts, pages, settings, user accounts — in a MySQL database. Every page load begins with a database query. When that connection fails, WordPress has nothing to show and surfaces this error instead.

You may see two slightly different versions:

  • The front end shows the error — visitors get a blank page with the message.
  • The admin shows "One or more database tables are unavailable" — WordPress connected but found a corrupted or missing table.

These look similar but point to different root causes, and the fix guide below covers both.

The Most Likely Causes

  • Wrong credentials in wp-config.php — the database name, username, password, or host doesn't match what your server expects. This is the single most common cause, especially right after a migration or a host switching you to a new server.
  • MySQL service is down — the database server crashed, ran out of memory, or was restarted by your host. This is common on shared hosting under heavy load.
  • Database tables are corrupted — a failed update, a server crash mid-write, or a full disk can corrupt InnoDB or MyISAM tables.
  • Too many database connections — your site opened more connections than the server allows. Heavy traffic, runaway plugins, or a misconfigured connection pool can trigger this.
  • Wrong database host value — most shared hosts use localhost, but some use a socket path or a remote hostname. After a migration this is frequently wrong.
  • The database itself was deleted or renamed — rare, but it happens after control panel accidents or botched migrations.

Before You Start: Take a Backup

If your site is partially accessible — even just the admin — export a database backup via phpMyAdmin or your host's control panel before touching anything. If the site is completely down, log in to your hosting control panel and run a full cPanel/Plesk backup first. You cannot make things worse with a backup in hand, but you can without one.

Step-by-Step Fixes

Step 1: Check wp-config.php Credentials

Connect to your server via FTP, SFTP, or your host's file manager. Open wp-config.php in the root of your WordPress install. Look for these four lines:

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

Now log in to your hosting control panel (cPanel, Plesk, or your host's dashboard) and navigate to the MySQL Databases section. Confirm that:

  1. A database with that exact name exists.
  2. A user with that exact username exists.
  3. That user is assigned to that database with full privileges.
  4. The password matches — reset it in the control panel and update wp-config.php if you're not sure.

The DB_HOST value is almost always localhost on shared hosting. Some managed hosts (WP Engine, Kinsta, Cloudways) use a different hostname or socket — check your host's documentation or support chat if localhost doesn't work.

After correcting any mismatch, save the file and reload the site. If it comes up, you're done. If not, keep going.

Step 2: Check Whether MySQL Is Running

If the credentials are correct but the error persists, the database service itself may be down. On shared hosting you can't restart MySQL yourself — contact your host's support directly and ask them to check whether MySQL is running on your account's server. Most hosts have a live chat or status page; this is a legitimate support request they handle every day.

On a VPS or dedicated server where you have root access, SSH in and run:

sudo systemctl status mysql
# or on some distros:
sudo systemctl status mariadb

If the service is stopped, start it:

sudo systemctl start mysql

Then check /var/log/mysql/error.log to understand why it stopped — a full disk, an out-of-memory kill, or a crash report will show up there.

Step 3: Test the Connection Directly

Create a temporary PHP file called db-test.php in your WordPress root with this content — replacing the placeholders with the values from your wp-config.php:

<?php
$link = mysqli_connect( 'localhost', 'your_db_user', 'your_password', 'your_db_name' );
if ( ! $link ) {
    die( 'Connection failed: ' . mysqli_connect_error() );
}
echo 'Connected successfully';
mysqli_close( $link );

Visit https://yoursite.com/db-test.php in a browser. The exact error message MySQL returns will tell you whether it's a credentials problem, a host problem, or an access-denied issue. Delete this file immediately after testing — never leave raw database credentials in a public PHP file.

Step 4: Repair Corrupted Tables

If WordPress connects but shows the "tables unavailable" variant of the error, you likely have table corruption. WordPress has a built-in repair tool. Add this line to wp-config.php temporarily:

define( 'WP_ALLOW_REPAIR', true );

Then visit https://yoursite.com/wp-admin/maint/repair.php. Click Repair Database. Once it finishes, remove that line from wp-config.php — leaving it enabled is a security risk because anyone can trigger a repair without logging in.

For more severe corruption, open phpMyAdmin from your hosting control panel, select each table in your database, and run Operations → Repair Table. If InnoDB tables are involved, a repair tool may not be enough — you may need to restore from a backup.

Step 5: Check for a "Too Many Connections" Problem

If the site goes down under traffic or at predictable times, a connection limit may be the cause. In phpMyAdmin, run this query:

SHOW STATUS LIKE 'max_used_connections';
SHOW VARIABLES LIKE 'max_connections';

If max_used_connections is near or equal to max_connections, you're hitting the ceiling. On shared hosting, ask your host to raise the limit or investigate which process is holding connections open. On a VPS, look for a runaway plugin — persistent object caching (Redis or Memcached) can dramatically reduce connection overhead. See what plugins actually do to your server for more context on connection pooling.

Step 6: Verify the Database Exists

Open phpMyAdmin and confirm the database named in wp-config.php actually appears in the left-hand sidebar. If it doesn't exist — perhaps after a migration or a control panel mistake — you'll need to either restore from a backup or recreate the database, import a dump, and reassign user privileges.

After the Fix: What to Check

  • Visit your site's front end and admin. Confirm posts, menus, and settings are intact.
  • Check wp-config.php to make sure you removed any temporary lines (like WP_ALLOW_REPAIR).
  • Delete db-test.php if you created it.
  • Scan your error log for repeated MySQL crashes — a one-off is usually fine, recurring crashes signal an underlying resource or configuration problem.

How to Prevent This Error

  • Keep automated backups running — daily offsite database backups mean a corrupted database is a 10-minute restore, not a crisis.
  • Use a staging site for migrations — test credential changes there before touching production.
  • Monitor uptime — a tool that alerts you the moment the site goes down lets you catch database crashes before users do.
  • Stay on a host that fits your traffic — shared hosting with a 10-connection MySQL limit is the wrong home for a site that gets traffic spikes.
  • Don't let plugins accumulate — every active plugin that runs its own database queries at page load is a connection, and they add up.

A Mend Care Plan covers automated backups, uptime monitoring, and managed updates for $99/month — so database crashes get caught and flagged before you find out from a customer.

When to Call a Professional

Call in help if:

  • The credentials look right but the connection still fails and your host says MySQL is running.
  • The repair tool can't fix the corrupted tables and you don't have a clean backup to restore from.
  • The database crashes repeatedly — a symptom that needs proper diagnosis, not repeated restarts.
  • You're on a VPS and not comfortable reading MySQL error logs or adjusting server configuration.
  • The site went down hard and you need it back up within the hour.

If any of those apply, Mend's Emergency Rescue gets a senior engineer on the problem fast — most database connection issues are resolved the same day, with a plain-English report of exactly what failed and what changed. No card required to start with a free diagnosis.


For a broader look at all the errors WordPress can throw and what each one means, the WordPress fix guides index is a good starting point — particularly the critical error guide if you're also seeing PHP fatal errors alongside the database problem.

Frequently asked questions

Can a WordPress plugin cause the database connection error?

Indirectly, yes. A plugin that opens many database connections simultaneously — or one that corrupts a table through a bad query — can trigger this error. If the error appeared right after installing or updating a plugin, disable that plugin via FTP by renaming its folder and test again.

Will I lose data if I repair the database?

The built-in WordPress repair tool (wp-admin/maint/repair.php) is non-destructive — it attempts to fix table structure without deleting rows. That said, severely corrupted tables sometimes can't be fully repaired, which is why a backup before any repair attempt is essential.

Why does the error only appear sometimes, not all the time?

Intermittent errors usually point to a "too many connections" problem or MySQL running out of memory under load and being killed by the OS. The database comes back on its own, so the error clears — then it crashes again under the next traffic spike. Check your MySQL error log for out-of-memory kills or max-connections messages.

My host says MySQL is fine. Why am I still seeing the error?

If the server is healthy, the problem is almost certainly in wp-config.php — a typo in the database name, username, or password, or a DB_HOST value that doesn't match what the server expects. Use the db-test.php script from Step 3 to get the exact error message MySQL returns; that will pinpoint the mismatch immediately.