WordPress Error Establishing a Database Connection – Causes & How to Fix It

Seeing "Error Establishing a Database Connection" in WordPress? Learn the top causes and easy fixes to restore your website fast. Complete troubleshooting guide inside.

Learn how to fix the “Error Establishing a Database Connection” in WordPress. Step-by-step solutions to common causes like wrong credentials, database issues, and server errors.

If you have worked with WordPress for a while, you have probably encountered the scary “Error Establishing a Database Connection” error. This message can be annoying, especially when your site becomes suddenly unavailable, and you don’t know the cause. Thankfully, this is remediable in most cases once you understand why it’s happening.

In this guide, we’ll dissect the various problems that might be causing this error and also give you solutions to each to help you get your site up and running again in no time.

What Does “WordPress Error Establishing a Database Connection” Mean?

WordPress is powered by PHP to create web pages and is used by MySQL to store and retrieve your content (posts, users, settings, etc.). The “Error establishing a database connection” error means the WordPress is unable to connect to the MySQL database.

If this connection is broken, your website can be unreachable and typically show nothing but a white screen or the error message mentioned above.

Common Causes of the Error

There are a number of things that can lead to WordPress database connection errors.

1. Database Username or Password is not valid

Your wp-config. php is where name, username and password are stored as well as host. If any of these are wrong, then WordPress won’t be able to connect to your database.

2. Corrupted Database

Your database may become corrupt (bad plugin/theme, a bad server crash), and this can cause WordPress to have no connection.

3. Database Server Issues

Occasionally, the MySQL server (the database server) is down as well or simply too busy to connect.

4. Web Hosting Problems

In shared hosting environments, resource limits can be met, or issues can occur that will result in a temporary connection failure.

5. Corrupted Core Files

In fact, if there’s one core file with a mix-up or infected by malware, then the database connection process will get disrupted.

How to Fix the “Error Establishing a Database Connection”

Let’s walk through the step-by-step solutions:

✅ Step 1: Check wp-config.php Credentials

Access your website’s root directory via FTP or cPanel > File Manager, and open the wp-config.php file. Verify the following lines:


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

  • Make sure the credentials match those in your hosting control panel (like cPanel > MySQL Databases).

  • For some hosts, DB_HOST may not be localhost. Check with your host to confirm.

✅ Step 2: Repair the Database

If your credentials are correct, the database may be corrupted. WordPress has a built-in database repair tool.

  1. In wp-config.php, add this line before /* That's all, stop editing! */:

define(‘WP_ALLOW_REPAIR’, true);

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

3. Choose Repair Database or Repair and Optimize Database.

4. Once complete, remove the repair line from wp-config.php.

✅ Step 3: Check Hosting Server Status

Contact your web host to ask:

  • Is the MySQL server running?

  • Are there any known issues or outages?

  • Have you hit your hosting resource limits?

If your host confirms issues, you may have to wait or consider upgrading your plan.

✅ Step 4: Test Database Connection via PHP Script

You can create a simple test script to check if your database credentials are working:

  1. Create a new file dbtest.php in your WordPress root directory.

  2. Add the following code:

<?php
$link = mysqli_connect(‘localhost’, ‘db_user’, ‘db_password’, ‘db_name’);
if (!$link) {
die(‘Could not connect: ‘ . mysqli_error($link));
}
echo ‘Connected successfully!’;
mysqli_close($link);
?>

Replace 'localhost', 'db_user', 'db_password', and 'db_name' with your actual database values.

  1. Visit http://yourdomain.com/dbtest.php to see the result.

✅ Step 5: Restore from Backup

If you recently made changes (installed a plugin/theme or edited files) and the error appeared, restoring your site from a recent backup might be the quickest fix.

Make sure to back up your current files and database before restoring.

✅ Step 6: Reinstall WordPress Core Files

If you suspect corrupted files, reinstalling WordPress core files can help:

  1. Download the latest WordPress version from wordpress.org.

  2. Extract the files on your computer.

  3. Delete the /wp-admin/ and /wp-includes/ folders from your live site via FTP.

  4. Upload fresh copies from the downloaded package.

  5. Do not delete the wp-content folder or wp-config.php.

How to Prevent This Error in the Future

  • ✅ Use a reliable hosting provider with good uptime and support.

  • ✅ Install a backup plugin to automate daily backups.

  • ✅ Limit plugin usage and only install reputable plugins and themes.

  • ✅ Monitor server resources to avoid hitting limits.

  • ✅ Enable uptime monitoring tools like UptimeRobot to catch downtime early.

Final Thoughts

The “Error establishing a database connection” is a common WordPress issue, but it’s usually easy to fix with the right steps. Start by checking your database credentials and move on to repair and testing if needed.

If you’re not comfortable editing site files or databases, it’s always best to contact your hosting support or a trusted developer.

Have you faced this error before? What worked for you? Let us know in the comments, and don’t forget to check out more troubleshooting tips on CustomWP.co.

 

Sandy Darek
Sandy Darekhttps://customwp.crecso.com/
I am a blogger and WordPress developer. I blog about Development Technologies. Feel free to contact me for custom WP development, link building, and blog outreach services.

Trending Posts

Discover more from CustomWP

Subscribe now to keep reading and get access to the full archive.

Continue reading