ArsalLocationStarter

Technical Review: Hostcry Engineering Team

How to Fix “Error Establishing a Database Connection” in WordPress (Step-by-Step)

AI Summary
This common WordPress error means your site can't connect to its database, usually due to incorrect credentials, server issues, or corrupted files. The quickest fixes involve checking your wp-config.php file for correct database details and using WordPress's built-in repair tool. To prevent future issues, always maintain backups and consider using optimized WordPress hosting with reliable database servers.

Seeing the “Error Establishing a Database Connection” message in WordPress means your website is unable to connect to its database. This is a common issue and can be fixed quickly if you follow the correct steps.

Video Tutorial

What Causes This Error?

This error usually happens due to:

  • Incorrect database credentials
  • Corrupted database
  • Server/database downtime
  • Corrupted WordPress files

Step 1: Check Your Database Credentials (Most Common Fix)

  1. Log in to your cPanel
  2. Open File Manager → go to public_html
  3. Find and open wp-config.php

Verify these details:

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

Make sure they match your MySQL database details in cPanel.


Step 2: Repair the Database

  1. Open wp-config.php
  2. Add this line:
define('WP_ALLOW_REPAIR', true);

  1. Visit:
yourdomain.com/wp-admin/maint/repair.php

  1. Click Repair Database

⚠️ Remove the line after repair for security.

Step 3: Check if the Database Server is Down

  • Try accessing phpMyAdmin from cPanel
  • If it doesn’t open → server issue

In this case, contact your hosting provider.

Step 4: Check Database User Permissions

  1. Go to MySQL Databases in cPanel
  2. Ensure:
  • The database user is assigned
  • Has ALL PRIVILEGES

Step 5: Restore Backup (If Available)

If the database is corrupted:

  • Restore from backup via cPanel
  • Or use hosting backup tools

Step 6: Re-upload Core WordPress Files

If files are corrupted:

  1. Download fresh WordPress from wordpress.org
  2. Upload and replace:
  • wp-admin
  • wp-includes

⚠️ Do NOT replace wp-content or wp-config.php


Common Mistakes to Avoid

  • Editing the wrong database
  • Forgetting to remove repair mode
  • Using incorrect DB_HOST
  • Overwriting important files

Pro Tips

  • Always keep daily backups
  • Use reliable hosting to avoid DB crashes
  • Monitor uptime and database health

Want to Avoid This Error Completely?

Most database errors happen due to poor hosting environments.

👉 Choose optimized WordPress hosting with stable database servers and automatic backups


Conclusion

The “Error Establishing a Database Connection” can look scary, but in most cases, it’s just a configuration issue. By checking your credentials and repairing the database, you can fix it within minutes.