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

Database

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

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 on shared hosting.

The message affects the entire site including wp-admin. Fix credentials and server access first before you touch plugins or themes.

Never paste live database passwords into support tickets or public forums. Share credentials only through secure channels like BugShield Vault.

Step 1: Check if the problem is your host

Before editing files, confirm MySQL is running. Many UK shared hosts post maintenance notices on status pages. A sudden sitewide database error often means server-side downtime, not a WordPress bug.

  • Open your host status page or support dashboard.
  • 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. 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' );
Some hosts use 127.0.0.1, a socket path, or a remote hostname instead of localhost. Copy DB_HOST exactly from the hosting panel, not from an old backup.

Step 3: Reset the database user password

After migrations or security scans, passwords in wp-config.php and MySQL often drift apart. In cPanel MySQL Databases, change the 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.

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 prefix matches, for example wp_.

In phpMyAdmin, select the database and run Check Table on wp_options. Repair 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 fixed price with direct developer chat.

FAQs

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.

Rather someone else fixed it?

Request a fix at a confirmed price. Named UK developer, secure Vault, direct chat.

Request a Fix