New 24/7 monitoring and daily cloud backups now included in every Shield Pro plan.

Database

Learn how to fix a WordPress database connection error.

Error establishing a database connection means WordPress cannot reach MySQL with the credentials in wp-config.php. This tutorial walks through credential checks, host outages, and table repairs you can run from hosting panels.

Time: 20-40 minutes Level: Intermediate
Neil McNaught, founder of BugShield and WordPress author
Written by
Written by
Updated
Updated

What this problem looks like

This error appears when WordPress boots but MySQL rejects the connection. Common causes include wrong passwords after a host migration, expired database users, localhost vs 127.0.0.1 mismatches, and full disk space.

The message affects the entire site including wp-admin. Confirm database credentials and that MySQL is reachable before you change plugins or themes.

Step 1: Check if the problem is your host

Before editing files, confirm MySQL is running. A sudden sitewide database error often means the database server is down or unreachable, not a WordPress bug.

  • Check your hosting dashboard for MySQL or database service alerts.
  • In cPanel, open MySQL Databases and confirm the database still exists.
  • Check disk usage. A full partition stops MySQL from writing.

Step 2: Verify wp-config.php database settings

Open wp-config.php via SFTP or File Manager. It usually sits next to the wp-admin and wp-content folders. Compare these four constants with the values in your hosting database panel.

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

Step 3: Reset the database user password

After migrations or security scans, passwords in wp-config.php and MySQL often drift apart. In your hosting panel’s database tools, change the database user password, then update wp-config.php to match.

  1. Create or locate the database user in hosting.
  2. Assign the user to the correct database with ALL PRIVILEGES.
  3. Set a new strong password.
  4. Update DB_PASSWORD in wp-config.php and save.

Step 4: Test connection with phpMyAdmin or WP-CLI

Log into phpMyAdmin from hosting. If you can browse tables, credentials are valid and the issue may be corrupted tables or a wrong table prefix.

If your host offers SSH or terminal access, you can also check and repair the database with WP-CLI from the WordPress root. These commands need server access. If you only have a hosting control panel, stay with phpMyAdmin for browsing tables and running Check Table / Repair Table from there.

bash
cd /path/to/wordpress
wp db check
wp db repair

Step 5: Check table prefix and corruption

If $table_prefix in wp-config.php does not match actual table names in MySQL, WordPress cannot find its options table. Confirm the prefix matches, for example wp_.

In phpMyAdmin, select the database and export a full backup first (SQL or gzipped SQL). Keep that file somewhere safe, then run Check Table on wp_options. Repair only if the status shows corruption.

When to stop DIY and hire help

If credentials are correct but the error persists, you may have deeper corruption or a host configuration issue outside wp-config.php. BugShield diagnoses database connection failures at a confirmed price without hourly billing surprises.

Questions answered

WordPress database connection error FAQs

Answers about database credentials, hosting failures, repairs, and safe recovery when WordPress cannot connect.

Can a plugin cause a database connection error?

Rarely. This error usually means MySQL is down or wp-config.php credentials are wrong. Fix database access before blaming plugins.

Should I reinstall WordPress to fix this?

No. Reinstalling core files does not fix credentials. You risk overwriting wp-config.php if you are not careful.

What is the difference between localhost and 127.0.0.1?

On some Linux hosts, localhost resolves to a socket while 127.0.0.1 uses TCP. Use the exact DB_HOST value your host documents.

How do I fix this after moving hosts?

Import the database, create a new MySQL user on the new host, and update all four DB constants in wp-config.php to match: DB_NAME, DB_USER, DB_PASSWORD, and DB_HOST.

What causes a WordPress database connection error?

Common causes are incorrect credentials in wp-config.php, an unavailable database server, a damaged database, an account suspension, or a host migration that changed connection details.

How do I check WordPress database credentials?

Compare DB_NAME, DB_USER, DB_PASSWORD, and DB_HOST in wp-config.php with the database details in your hosting panel.

Does a database connection error mean my WordPress data is lost?

Usually not. The message means WordPress cannot reach or authenticate with the database, not that posts, users, orders, or settings have been deleted.

Can a hosting outage cause the database connection error?

Yes. If credentials have not changed and several hosted sites fail together, check the host status page or ask support whether the database service is unavailable.

Should I use WordPress database repair mode?

Use repair mode only when logs or database tools indicate damaged tables. Remove the repair setting from wp-config.php as soon as the repair is complete.

When should I get help with a WordPress database connection error?

Ask for help when credentials are correct but the database remains unavailable, tables report corruption, or the affected site contains live orders or recent customer data.

Would you rather a developer fixed it?

Request a fix at a confirmed price and speak directly with the BugShield developer working on your site.

Request a Fix