How to Fix WordPress Error Establishing a Database Connection?

If you are facing the ‘Error Establishing a Database Connection’ issue in your WordPress site, don’t hit the panic button just yet! Because in this step-by-step tutorial, we are going to show you how you can easily solve this database connection error all by yourself.

The error establishing a database connection is one of the most common problems that every WordPress user experiences. It means your site can’t connect with the database. That makes your site inaccessible to everyone. 

But don’t worry! We have got you covered. Ever played a detective? We’re doing just that, investigating why this unwelcome guest crashes your online party and how you can show it the exit door.

So, get ready with your computer and jump into the ways to fix your database connection errors!

What Does Error Establishing a Database Connection Mean in WordPress?

We are giving an example to easily understand what is error establishing a database connection in WordPress. Let’s assume WordPress as a book library, and the database as its storage. 

Every time someone visits your website, WordPress needs to fetch data from its storage. For this communication to happen, WordPress relies on specific details:

  • Database Name: This is like the book library’s name.
  • Database Username: Think of this as the librarian’s ID.
  • Database Password: A secret key to access the library.
  • Database Server: The address to find the library.

These essential bits of info are stored in a file called ‘wp-config.php,’ the brain of your WordPress site.

If any of these details are wrong or if the database is corrupted or the server isn’t responding, WordPress can’t access your data. That’s when the WordPress database connection error message appears.

It’s like a phone call to the book library, and if the library is closed, you can’t get the information you need. 

Why Error Establishing a Database Connection Happens? 

The error establishing a database connection issue in WordPress can happen for several reasons. It often involves the communication breakdown between your website and its underlying database. 

Here is a list of reasons that can cause your database connection error: 

  • Incorrect Database Credentials: If the database name, username, password, or server information in your WordPress configuration file (wp-config.php) is wrong, WordPress can’t connect to the database.
  • Corrupted Database: Data corruption within the database due to server issues, incomplete updates, or plugin conflicts can create the database connection error.
  • Unresponsive Database Server: If the database server is down, overloaded, or facing connectivity problems, WordPress can’t access the data it needs, as a result, you can face this problem. 
  • Plugin or Theme Issues: In some cases, a poorly coded or incompatible plugin or theme might interfere with the database connection, causing the error.
  • Hacked or Compromised Site: Security breaches can disrupt the database connection, especially if the attacker altered the database or its credentials.
  • Sudden Traffic Spikes: Unexpected spikes in website traffic could overload the server and impact the database connection, leading to the error.

By identifying the specific cause, you can take appropriate steps to fix the error and ensure your WordPress site runs smoothly. Now let us show you how to fix error establishing a database connection error with ease. 

6 Steps to Fix WordPress Error Establishing a Database Connection

Without making any further delay, let us show you the 6 ways to solve the “error establishing a database connection” problem at a glance: 

  • Check the Database Login Credential of Your WordPress Site 
  • Fix Your Corrupted WordPress Database
  • Repair Your Plugins and Themes File 
  • Reinstall the WordPress Core File 
  • Check If Your Database Server is Down 
  • Revert to an Earlier Version   

Now we are going to elaborate each step in detail so that you can easily understand what you should initiate in each step. Keep reading! 

Note: We strongly recommend taking a complete backup of your website prior to making any changes. This ensures that if anything unusual happens, you can easily restore your site to its previous state using the backup. 

1. Check the Database Login Credential of Your WordPress Site

One of the most common reasons for facing the database connection error is incorrect database credentials. If you have recently moved your WordPress site to a new hosting, there is a high chance you’ll face this issue. 

The connection details for your WordPress site are stored in the wp-config.php file. It is the WordPress configuration file that contains important WordPress settings including database information. There you’ll find four important pieces of information that must be correct to avoid database connection errors. 

  • Database name
  • MySQL database username
  • MySQL database password
  • MySQL hostname (server)

To check whether your wp-config.php file carries this information correctly or not, you have to access the wp-config.php file.  

So, log in to your control panel and find the “File Manager” option. Click on the File Manager option and you’ll be redirected to a new interface.

Now navigate to the public_html -> wpconfig.php file. 

Now open the wp-config.php file by clicking on the file and find the below information: 

/** The name of the database for WordPress */
define( 'DB_NAME', '********' );

/** MySQL database username */
define( 'DB_USER', '*********' );

/** MySQL database password */
define( 'DB_PASSWORD', '********' );

/** MySQL hostname */
define( 'DB_HOST', 'localhost' );

You need to make sure that the information for the database name, username, password, and database host is correct.
You can confirm this information from your WordPress hosting account dashboard. Simply log in to your hosting account and click on MySQL Management.

MySQL Management

There you’ll find the database name. Click on the database name.

Then you’ll find the database username, password, and hostname.

That means you have found out the database name, username, password, and hostname. 

Now, see if the names for the database, username, and host are the same as what you found in the wp-config.php file. If the wp-config.php has different names, just update them to match the names you found in your MySQL file. 

In terms of password, copy the password from the wp-config.php file and paste it in the “Change Password” option. Then hit the Save button and check if the “error establishing a database connection” problem has been solved or not. If it’s yet not disappeared, follow the next solutions. 

2. Fix Your Corrupted WordPress Database 

If you are still facing the same problem, it’s time to check if your database has been corrupted. Your database could be corrupted due to reasons such as a server crash, a possible hardware failure, a bug in MySQL, or something else entirely.

For testing whether your database is corrupted or not, navigate to the login page of your WordPress site, i.e., yourdomain.com/wp-admin. 

If you see the “error establishing a database connection” on both the frontend (yourdomain.com) and the backend (yourdomain.com/wp-admin) of your website, your database is not corrupted. So, you can skip this step and jump to the next step. 

But if there is error like “Database tables unavailable” which means your database is corrupted and you need to fix it. Follow the below steps to get your job done:

Access your wp-config.php file by navigating to your Control panel -> File Manager -> public_html -> wp-config.php.

Now click on the wp-config.php file to edit the file. Then insert the below mentioned code at the bottom of the file: 

define ( 'WP_ALLOW_REPAIR' , true);

Save the file and go to yourdomain.com/wp-admin/maint/repair.php. You’ll find an interface like the following screenshot.

Now hit either the “Repair Database” or “Repair and Optimize Database” and WordPress will repair your database. 

After repairing the database, don’t forget to remove the line of code that you have added to your wp-config.php file. So, remove the code, save the file, and go to yout browser to check if your site is loading. 

If your site is loading without showing the “error establishing a database conncetion” which means your corrupted database was the reason to get the error. But if your site still showing the database error notice, you should try the next solution. 

3. Repair Your Plugins and Themes File

If you have recently installed or updated any plugins or themes on your site, this could be a possible reason to encounter the MySQL database connection error. So, the process is simple, we will find the culprit if it’s a theme or plugin, then we will delete that plugin or theme from the site. Follow the below procedure to complete this step with ease: 
To fix the issue, navigate to your Control panel -> File Manager -> public_html -> wp-content, and find the plugin and theme folder.

First, we will check the plugins. Just rename the plugins folder anything that you want, e.g., “plugin_testing”. It will deactivate all the activated plugins on your site. 

Now go to your browser and reload your site, if your site loading without showing the database error, that means your plugin was the culprit. To identify which plugin is causing the issue, you have to check every plugin separately. 

It’s also an easy process. Just return your plugin folder’s name to its original name, i.e., “plugins” and enter into the folder. Then rename each plugin folder one by one and keep checking your site from the browser if it’s running without the database error or not. 

After renaming each plugin folder at a time, if you see the same “error establishing a database connection” message that means this plugin folder is not the culprit. So, you can rename the folder to its original name and move on to the next plugin folder to check. 

By following this process, if you find the faulty plugin folder, just delete the folder and solve the problem. But If none of your plugins are causing problems, its time to turn towards your theme folder. 

Just follow the same process with the theme folder. 

If this doesn’t help, the error establishing a database connection in WordPress could be caused by something else. 

4. Reinstall the WordPress Core File

Sometimes your WordPress core file could be the reason for facing database connection failure. So, in this step, we are going to replace the core WordPress file of your website. Remember, we won’t touch your plugins, themes, or media files. Instead, we will just replace your WordPress core file. 
To replace your existing WordPress file, you have to download a fresh copy from WordPress official website. Just visit the site and there you’ll find the file to download for free.

After downloading the file, unzip the folder on your local computer. Then delete the wp-config.php file and the wp-content folder

You might be wondering why we are deleting these files. Deleting those files means this process doesn’t overwrite your existing files and folders. That way, you retain your themes, plugins, media files, and above all, the wp-config.php file that has all your website configuration details. 

Now log in to your control panel and upload the WordPress file using the File Manager option. This is a sensitive step. If you make any mistake, you could break your site design and lose your data. Having said that, since you have already taken a full backup of your site, so, no worries. If you make any blunder, there is a ready backup to recover your site. 

After uploading the WordPress file, go to your browser and reload the site. If you’re still facing the database error, proceed to the next step. 

5. Check If Your Database Server is Down 

If you’ve been experiencing high traffic recently, there’s a chance that your database server may not be able to handle the load, leading to the “error establishing a database connection” issue. This situation often arises for shared hosting users.

To check if your database server is down, you should use the live chat or support system of your hosting provider to ask them if your database is responsive or not. 

Also, you can check it manually. Navigate to your hosting control panel and access your public_html folder. Then create a new PHP file there and name it as you want, e.g., testdatabase.php. 

Now copy this code and paste it to the newly created PHP file:

<?php
$link = mysqli_connect('localhost', 'username', 'password');
if (!$link) {
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully';
mysqli_close($link);
?>

Don’t forget to replace the “username” and “password” with your database username and password. Now save the file and access it through your browser. 

Open the browser and visit yourdomain.com/testdatabase.php. If it connects successfully, that means your database server is not down and something else is causing the database connection error. 

6. Revert to an Earlier Version   

If you’re using a backup plugin to automate site backups, you’re in luck—this process is for you. However, quite a few hosting services offer regular backup features, so it’s essential to ensure you have backups ready to proceed with this step.

In some cases, this method could be a quicker solution, especially if you aren’t worried about losing any data between when your last backup was taken.

So, revert to an earlier version and then update the site for any changes that you may have made. Thus you can solve your WordPress database connection issue. 

Bonus: Tips to Prevent Future Database Connection Error  

One of the best ways to keep your WordPress site healthy and prevent the dreaded “Error Establishing a Database Connection” issue is by taking proactive measures. Here’s how you can do it:

  • Regular Backups: Think of backups as insurance for your website. Set up regular automated backups of your website and database. If something goes wrong, you’ll have a recent backup to restore from. Thus you can ensure your data stays safe even if you encounter a database connection issue.
  • Monitoring Server Resources: Keep an eye on your server’s performance. Monitor disk space, memory usage, and other resource metrics. When these resources run low, it can impact the database connection. Regular monitoring allows you to address resource limitations before they cause problems.
  • Keeping Plugins and Themes Up to Date: Outdated plugins and themes can introduce vulnerabilities or conflicts that affect your database connection. Stay up to date with the latest versions of plugins and themes. Regular updates often include bug fixes, security patches, and improved compatibility with WordPress updates.
  • Security Measures: Implement robust security practices. Use strong, unique passwords for database access, WordPress admin, and hosting accounts. Install a security plugin to help protect your site from malicious attacks. Regularly scan for malware or suspicious activities. A secure site is less likely to experience database-related issues due to compromised security.

By following these steps, you’re not only preventing the “Error Establishing a Database Connection” problem but also ensuring the overall reliability and security of your WordPress website. 

How to Solve Error Establishing a Database Connection Error- Key Takeaways

Now you know how to tackle a database connection failure. So, if you ever face this problem, do not be scared. Instead, you can just open your website and this step-by-step tutorial parallelly to fix your WordPress database connection error like a pro.  

We have talked about the 6 proven ways to solve this issue. All these ways are tested by us. So, you can rely on us in this matter. Also, we have shared a few tips that you must follow to avoid this kind of issue in the future. 

That’s all from our side! Now it’s time to hear from you. If you want us to add anything related to this topic to make this guide more exclusive, you are always welcome to share that with us using the comment box below. So, what are waiting for? Let’s get started!

Leave a Comment

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

Scroll to Top