How to Fix "Error Establishing a Database Connection" in WordPress (2026 Updated Guide)
![]() |
| WordPress error screenshot (database connection error) |
Is your WordPress site showing "Error Establishing a Database Connection"? Don't panic! This is one of the most common WordPress errors, and the good news is
**it's fixable**
php
define('DB_NAME', 'your_database_name');
define('DB_USER', 'your_database_user');
define('DB_PASSWORD', 'your_database_password');
define('DB_HOST', 'localhost');
5. Cross-check these details with your hosting database info (cPanel > MySQL Databases)
6. If anything is wrong, update it and save the file
Pro Tip:
If `DB_HOST` is not `localhost`, ask your hosting provider for the correct value.
If your database is corrupted, WordPress has a built-in repair tool.
Steps:
1. Open `wp-config.php`
2. Add this line before `/* That's all, stop editing! */`:
php
define('WP_ALLOW_REPAIR', true);
3. Visit this URL:
https://yoursite.com/wp-admin/mwp-repair.php
4. Click "Repair Database" or "Repair and Optimize Database."
5. After repair, remove the line from `wp-config.php` (for security).
Method 3: Check If Your Database Server Is Down
Sometimes the issue is on the hosting side, not your site.
How to Check:
Method 4: Restore wp-config.php from Backup
If you recently edited `wp-config.php`and broke something, restore it from a backup.
Steps:
Method 5: Check for Corrupted Core Files
If nothing works, your WordPress core files might be corrupted.
Steps:
1. Download a fresh copy of WordPress from https://wordpress.org
2. Extract the files
3. Upload only wp-admin and wp-include folders via FTP (overwrite existing)
4. DO NOT touch wp-content (it has your themes & plugins)
Method 6: Increase PHP Memory Limit
Low memory can sometimes cause database connection issues.
Steps:
1. Open wp-config.php
2. Add this line:
3. Save and reload your site
Method 7: Scan for Malware & Hacked Files
If your site was hacked, malicious code could break the database connection.
How to Fix:
Use Wordfence or Sucuri to scan your site
Remove infected files
Change all passwords (hosting, database, WordPress admin)
Still Not Fixed? Contact Your Hosting Provider*
If none of these methods work, the issue might be:
Final Thoughts
The "Error Establishing a Database Connection" error is scary but fixable. In most cases, it's just a wrong credential or corrupted file.
Follow the methods above step by step, and your site will be back online.
Need expert help? I'm a WordPress & cybersecurity specialist who has fixed hacked and broken WordPress sites. Get in touch
FAQ
Q: Will I lose my data if I repair the database?
A: No. The repair tool only fixes corrupted tables — your content stays safe.
Q: How do I prevent this error in the future?
A: Use regular backups, strong passwords, and a security plugin like Wordfence.
Q: Can a plugin cause this error?
A: Yes. A faulty plugin can crash the database. Deactivate plugins via FTP if needed.
Tags: WordPress Error Fix, Database Connection Error, WordPress Troubleshooting, WordPress Security, Fix WordPress Errors
In this guide, I'll walk you through 7 proven methods to fix this error step by step, even if you're a beginner.
What Does "Error Establishing a Database Connection" Mean?
This error means your WordPress site cannot connect to its database. Without database access, WordPress can't load your content, posts, or pages.
Common Causes:
Method 1: Check Your wp-config.php File
The most common cause is wrong database credentials in your `wp-config.php` file.
What Does "Error Establishing a Database Connection" Mean?
This error means your WordPress site cannot connect to its database. Without database access, WordPress can't load your content, posts, or pages.
Common Causes:
- Incorrect database credentials (username, password, database name)
- Corrupted database
- Database server down
- Corrupted WordPress core files
- Hacked or infected website
Method 1: Check Your wp-config.php File
The most common cause is wrong database credentials in your `wp-config.php` file.
Steps:
1. Log in to your hosting cPanel or use FTP (FileZilla)
2. Navigate to `public_html` (or your root directory)
3. Find and open `wp-config.php
4. Look for these lines:
1. Log in to your hosting cPanel or use FTP (FileZilla)
2. Navigate to `public_html` (or your root directory)
3. Find and open `wp-config.php
4. Look for these lines:
php
define('DB_NAME', 'your_database_name');
define('DB_USER', 'your_database_user');
define('DB_PASSWORD', 'your_database_password');
define('DB_HOST', 'localhost');
5. Cross-check these details with your hosting database info (cPanel > MySQL Databases)
6. If anything is wrong, update it and save the file
Pro Tip:
If `DB_HOST` is not `localhost`, ask your hosting provider for the correct value.
![]() |
| Screenshot of wp-config.php file (highlight DB credentials) |
Method 2: Repair Your WordPress Database
If your database is corrupted, WordPress has a built-in repair tool.
Steps:
1. Open `wp-config.php`
2. Add this line before `/* That's all, stop editing! */`:
php
define('WP_ALLOW_REPAIR', true);
3. Visit this URL:
https://yoursite.com/wp-admin/mwp-repair.php
4. Click "Repair Database" or "Repair and Optimize Database."
5. After repair, remove the line from `wp-config.php` (for security).
Method 3: Check If Your Database Server Is Down
Sometimes the issue is on the hosting side, not your site.
How to Check:
- Try accessing phpMyAdmin from cPanel
- If you can't log in, your database server might be down
- Contact your hosting support immediately
If you recently edited `wp-config.php`and broke something, restore it from a backup.
Steps:
- Check if your hosting has **automatic backups** (cPanel > Backups)
- Download the old wp-config.php
- Replace the current one via FTP
Method 5: Check for Corrupted Core Files
If nothing works, your WordPress core files might be corrupted.
Steps:
1. Download a fresh copy of WordPress from https://wordpress.org
2. Extract the files
3. Upload only wp-admin and wp-include folders via FTP (overwrite existing)
4. DO NOT touch wp-content (it has your themes & plugins)
![]() |
| Fresh WordPress download page |
Method 6: Increase PHP Memory Limit
Low memory can sometimes cause database connection issues.
Steps:
1. Open wp-config.php
2. Add this line:
php
define('WP_MEMORY_LIMIT', '256M');
define('WP_MEMORY_LIMIT', '256M');
3. Save and reload your site
Method 7: Scan for Malware & Hacked Files
If your site was hacked, malicious code could break the database connection.
How to Fix:
Use Wordfence or Sucuri to scan your site
Remove infected files
Change all passwords (hosting, database, WordPress admin)
Need professional help? I offer WordPress malware removal & security hardening services. Contact me here
Still Not Fixed? Contact Your Hosting Provider*
If none of these methods work, the issue might be:
- Database server crash
- Hosting configuration problem
- IP blocked by firewall
Final Thoughts
The "Error Establishing a Database Connection" error is scary but fixable. In most cases, it's just a wrong credential or corrupted file.
Follow the methods above step by step, and your site will be back online.
Need expert help? I'm a WordPress & cybersecurity specialist who has fixed hacked and broken WordPress sites. Get in touch
Need help fixing a hacked WordPress website?
Contact me on Fiverr.
FAQ
Q: Will I lose my data if I repair the database?
A: No. The repair tool only fixes corrupted tables — your content stays safe.
Q: How do I prevent this error in the future?
A: Use regular backups, strong passwords, and a security plugin like Wordfence.
Q: Can a plugin cause this error?
A: Yes. A faulty plugin can crash the database. Deactivate plugins via FTP if needed.
Tags: WordPress Error Fix, Database Connection Error, WordPress Troubleshooting, WordPress Security, Fix WordPress Errors






.png)
Comments
Post a Comment
Have a question about WordPress security or need professional help fixing a hacked site? Feel free to leave a comment below! For urgent malware removal and security hardening services, you can reach me directly on my Fiverr profile: https://www.fiverr.com/naime_sheikh