How to Fix “Error Establishing a Database Connection” in WordPress

Seeing "Error Establishing a Database Connection" in WordPress? Don’t stress! This error means WordPress can’t reach your database, which makes your site go offline.

The good news? It’s a common problem with easy fixes. Follow this guide, and your site will be back in no time!

Table of Contents

Why Does This Error Happen?

Several things can cause this issue:

Now, let’s fix it! 👇

Step-by-Step Fixes for This Error

1. Check Your Database Credentials

Incorrect database details are the most common reason for this error.

				
					define('DB_NAME', 'your_database_name');
define('DB_USER', 'your_database_user');
define('DB_PASSWORD', 'your_database_password');
define('DB_HOST', 'localhost'); // Sometimes this is '127.0.0.1'

				
			

🔄 Refresh your website—if it loads, you’re all set! 🎉

2. Repair Your Database

A corrupt database can break your site. Fix it using WordPress’s built-in tool:

				
					define('WP_ALLOW_REPAIR', true);
				
			
				
					https://yourwebsite.com/wp-admin/maint/repair.php
				
			

🔄 Refresh your website—if it loads, you’re all set! 🎉

3. Restart Your Database Server

Sometimes, your database server crashes and needs a restart.

For cPanel users:

For VPS/dedicated users:

				
					sudo service mysql restart
				
			

For shared hosting users:

Contact your hosting provider and ask if their MySQL server is down.

🔄 Refresh your website—if it loads, you’re all set! 🎉

4. Restore WordPress Core Files

Broken WordPress files can cause database errors. Here’s how to replace them:

✅ This keeps your site intact while fixing broken files.

5. Increase Database Memory Limits

If high traffic is causing the error, increasing memory can help.

Add this to wp-config.php:

				
					define('WP_MEMORY_LIMIT', '256M');
				
			

Open .htaccess (in your root folder) and add:

				
					php_value memory_limit 256M
				
			

✅ This lets WordPress handle more visitors without crashing!

6. Contact Your Hosting Provider

If nothing works, ask your hosting provider for help.

❓ Questions to ask:

They may fix it for you or give you further steps.

Final Thoughts

The "Error Establishing a Database Connection" is annoying, but you can fix it fast. Try these steps:

🚀 Got your site back? Let us know in the comments!