Fix the “Error Establishing a Database Connection” in WordPress

Learn how to fix the “Error Establishing a Database Connection” in WordPress. Step-by-step guide to check wp-config.php, MySQL server, and user credentials.

What Does This Error Mean?

When you see “Error establishing a database connection”, it means WordPress cannot connect to your MySQL database.

This can happen if:

  • Database credentials are incorrect
  • MySQL server is down
  • Database is corrupted
  • User privileges are missing

Step-by-Step Fix

✅ Step 1: Check wp-config.php

Open wp-config.php and confirm your database settings:

define( 'DB_NAME', 'your_database_name' );
define( 'DB_USER', 'your_username' );
define( 'DB_PASSWORD', 'your_password' );
define( 'DB_HOST', 'localhost' );
  • DB_NAME → must match your actual database name
  • DB_USER and DB_PASSWORD → must match your MySQL user credentials
  • DB_HOST → usually localhost, but can differ on some hosts (127.0.0.1 or remote IP)

✅ Step 2: Test Database Connection Manually

Run this from the terminal:

mysql -u your_username -p your_database_name

If you can log in → credentials are fine.
If not → fix your MySQL user or password.


✅ Step 3: Check MySQL Server Status

Make sure MySQL is running:

sudo systemctl status mysql

If it’s stopped, restart it:

sudo systemctl restart mysql

✅ Step 4: Repair Database (Optional)

If the database is corrupted, add this line to wp-config.php:

define('WP_ALLOW_REPAIR', true);

Then visit:

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

Repair and optimize the database.
Remove the line after repair.


✅ Step 5: Check User Privileges

Log in to MySQL and grant privileges:

GRANT ALL PRIVILEGES ON your_database_name.* TO 'your_username'@'localhost' IDENTIFIED BY 'your_password';
FLUSH PRIVILEGES;

✅ Step 6: Restore Backup (if needed)

If nothing works, restore your database from a backup.


⚡ Quick Recap

  • This error means WordPress can’t talk to MySQL.
  • Fix by:
    1. Checking wp-config.php credentials
    2. Testing MySQL connection
    3. Restarting MySQL
    4. Repairing the database
    5. Restoring from backup

✅ Following these steps should help you fix the Error Establishing a Database Connection and get your site back online.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top