Error Establishing a Database Connection

Error Establishing a Database Connection

Elena Rodriguez ·

Symptoms

  • White page displaying only 'Error establishing a database connection'
  • WordPress admin dashboard inaccessible with database error message
  • Front-end loads but admin shows 'One or more database tables are unavailable'
  • Intermittent database errors under traffic spikes

You hit your site and got a stark white page with nothing but “Error establishing a database connection.” Your site is down, your admin is locked, and nothing works. This error means WordPress tried to talk to your MySQL database and got rejected. The fix is almost always in wp-config.php or your database server itself.

What Causes Error Establishing a Database Connection?

  1. Wrong database credentials in wp-config.php — Someone changed the database password in the hosting panel but forgot to update wp-config.php. This is the cause 60% of the time.
  2. Database server is down — Your MySQL service crashed or your host is having infrastructure issues. Common on shared hosting during traffic spikes.
  3. Corrupted database — A plugin update interrupted mid-write, or your host ran out of disk space during a query. Tables get corrupted.
  4. Corrupted WordPress core files — A failed update can break the database connection layer in wp-includes/. Rare, but it happens.
  5. Exhausted database connections — Your hosting plan has a connection limit and you hit it. Poorly coded plugins that don’t close connections are usually the culprit.

How to Fix It

Step 1: Verify Your Database Credentials

Open wp-config.php in your site root via FTP, SSH, or your host’s file manager. Check 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' );

Log into your hosting control panel (cPanel, Plesk, or your host’s dashboard) and confirm every value matches exactly. Pay attention to:

  • DB_HOST — Most hosts use localhost, but some (like Kinsta) use a socket path or IP address. Check your host’s documentation.
  • DB_PASSWORD — Copy-paste it. Don’t retype. Trailing spaces will break it.

If anything is wrong, fix it, save the file, and reload your site.

Step 2: Test the Database Connection Directly

SSH into your server and test the connection manually:

mysql -u your_database_user -p -h localhost your_database_name

If this fails with “Access denied,” your credentials are wrong. If it fails with “Can’t connect to MySQL server,” your database server is down.

No SSH access? Create a temporary file called test-db.php in your site root:

<?php
$link = mysqli_connect( 'localhost', 'your_database_user', 'your_database_password', 'your_database_name' );
if ( ! $link ) {
 die( 'Connection failed: '. Mysqli_connect_error() );
}
echo 'Connected successfully';
mysqli_close( $link );

Visit yourdomain.com/test-db.php in your browser. Delete this file immediately after testing. Leaving it on your server is a security risk. Skip this if you want headaches later.

Step 3: Restart MySQL (If the Server Is Down)

If you have root or sudo access:

sudo systemctl restart mysql

Or on older systems:

sudo service mysql restart

On managed hosting, you can’t do this yourself. Contact your host’s support — they can restart MySQL for you, usually within minutes. If this keeps happening, your site has outgrown that hosting plan.

Step 4: Repair the Database

Add this line to wp-config.php just above the “That’s all, stop editing!” comment:

define( 'WP_ALLOW_REPAIR', true );

Then visit:

https://yourdomain.com/wp-admin/maint/repair.php

Click Repair and Optimize Database. WordPress will attempt to fix corrupted tables.

The right way to do this: use WP-CLI if you have SSH access. It’s faster and gives you actual error output:

wp db repair
wp db optimize

Remove the WP_ALLOW_REPAIR line from wp-config.php when you’re done. That repair page requires no authentication. Leaving it enabled is a security hole.

Step 5: Check for Plugin Conflicts

If the error appeared right after a plugin update, deactivate all plugins via WP-CLI:

wp plugin deactivate --all

No CLI access? Rename the plugins folder via FTP:

/wp-content/plugins/ → /wp-content/plugins-disabled/

If your site comes back, rename the folder back and reactivate plugins one at a time to find the offender.

Step 6: Replace Core Files

If nothing else works, you may have corrupted core files. Download a fresh copy of WordPress and replace wp-admin/ and wp-includes/ via FTP. Do not touch wp-content/ — that’s your themes, plugins, and uploads.

Via WP-CLI:

wp core download --force --skip-content

This overwrites core files without touching your content directory.

Prevention

  • Use a managed host with automatic database monitoring. Kinsta, Cloudways, and WP Engine all restart crashed MySQL instances automatically. Cheap shared hosting won’t.
  • Keep wp-config.php credentials in sync. If you rotate your database password (and you should periodically), update wp-config.php at the same time. Do both in the same maintenance window.
  • Run wp db optimize monthly. Add it to a cron job or use a plugin like WP-Optimize. Fragmented tables slow down queries and increase the chance of corruption.
  • Monitor database connection limits. If you’re on shared hosting and running WooCommerce with multiple caching and analytics plugins, you can hit connection limits during sales. Upgrade your plan before Black Friday, not during it.

Last verified: April 2026