Jamsheer K
WRITTEN BY
Jamsheer K
Posted on Mar 01, 2023

We’ve all been there: you’re trying to access your website, but all you see is an error message saying, “Error Establishing a Database Connection.” Not only is this frustrating, but it can also be a serious problem, especially if your website is critical to your business.

The good news is that this issue is fixable, even if you’re not a WordPress expert. In fact, with a few simple troubleshooting steps, you can get your website up and running again in no time! So don’t panic – we’re here to help.

In this article, we’ll walk you through everything you need to know to fix the Error Establishing a Database Connection message. From common causes of the problem to step-by-step solutions, we’ve got you covered. So, let’s dive in and get your website back on track!

What is the “Error Establishing a Database Connection” Error?

Have you ever encountered the message “Error Establishing a Database Connection” while attempting to access your WordPress website? If that’s the case, you’re not alone! This error message can be annoying and confusing, especially if you are unfamiliar with how WordPress operates.

We must first examine how WordPress displays web pages to understand this error. A database is required for every WordPress site to store information such as posts, pages, user data, and so on. When a user enters a URL into their browser, WordPress queries the database using the PHP programming language. The information is then displayed as a web page in the visitor’s browser.

If WordPress is unable to connect to the database, the “Error Establishing a Database Connection” message will be displayed. This error prevents not only visitors from accessing your site but also you from accessing your WordPress dashboard.

But don’t worry! We’ve got your back. In this article, we’ll look at a few different solutions to help you resolve this problem and get your website back online.

What is the “Error Establishing a Database Connection” Error?

What are the Common Issues of the Error Establishing a Database Connection Error?

If you’ve ever encountered the Error Establishing a Database Connection message on your WordPress site, you know how frustrating it can be. There are four common reasons for this error:

Incorrect login credentials: 

This is the most common cause of the error, and it happens when WordPress has the wrong database name, username, or password. Remember, these details are different from the ones you use to log in to your site.

Database corruption: 

Your WordPress MySQL database contains a lot of important information. If any part of it is deleted or corrupted, you might see this error message. This can happen from manually fiddling with the database, or it can occur naturally over time.

WordPress core file corruption: 

Sometimes, corruption can occur in the core WordPress files that make up your site. Even though these files are separate from the database, they can still lead to the same error.

Problems with the web server or hosting provider: 

If your hosting provider experiences outages, data loss, or hardware failures, WordPress may not be able to connect to your database, resulting in this error.

Although incorrect login credentials are the most common culprit, any of these reasons could be to blame. The good news is that fixing the error is usually possible with some troubleshooting.

Updating your credentials is just one of several potential solutions to this issue. Whether you’re a new or experienced user, we have workable solutions to get your site back up and running. Follow the steps below to fix this error:

Step 1: Checking Database Credentials

Wrong database credentials are one of the major reasons for this error. These details rarely change automatically, but they could be the source of the problem in case someone changes files or hosting.

When you create a website using WordPress, all the information about your site is saved in a database management system called MySQL. This database is located on a server. To get access to the database, WordPress needs some important information like the name of the database, the server it’s stored on, as well as a username and password. These pieces of information are called database credentials and they’re all saved in a special file called wp-config.php.

In case any information available in the wp-config.php file is not right, WordPress will be unable to connect to the database, and an error message will appear.

Checking the wp-config File

Checking the wp-config File - Error Establishing a Database Connection

You’ll need to access your server’s files to check the credentials of your database. Log in to your host’s cPanel, go to File Manager, navigate to the folder containing your website.

To check a website’s configuration file, follow these steps:

  1. Go to the website folder that you need to edit.
  2. Try looking for a file named as wp-config.php, once you have found the file, right-click to explore options. Before making any changes, it’s a good idea to download a backup. Then select “Edit.”
  3. Now, you need to scroll down for the section named MySQL settings. Here, you’ll find information like database name, hostname, username, and password. Make sure the information is right.
  4. To confirm the database credentials, go to your host’s cPanel and select “MySQL Databases.”
  5. Check your existing databases in order to ensure the name and username of the database.
  6. Scroll down for the section named current users. In this section, you can view usernames as well as change passwords.
  7. If name, username, and password of the database don’t match in the wp-config file, make the necessary changes and reload your website.

Step 2. Checking WordPress Dashboard

To troubleshoot a WordPress error, follow these steps:

  1. Try modifying your website’s URL and include /wp-admin to access the WordPress dashboard. 
  2. Go to the next step if you encounter the “establishing database connection” issue. Follow the guidelines if you encounter a different error message, as they can assist in fixing the issue.
  3. If you are able to access the dashboard, try deactivating themes, plugins, or custom codes one by one until the issue is resolved.

Step 3. Repairing Database

To repair a database table that is unavailable on your WordPress site, follow these steps:

  1. Log in as the website’s administrator and search for a notification that reads, “There are one or more inaccessible database tables. It could be necessary to fix the database.”
  2. Open wp_config.php file and find the section that says, “That’s all, stop editing! Happy blogging.” Add the following code, just above this line: define(‘WP_ALLOW_REPAIR,’ true);
Error Establishing a Database Connection
  1. Next, navigate to your website’s repair page on examplewebsite.com/wp-admin/maint/repair.php.
  2. You’ll see two different options: “Repair Database,” which will find and repair common issues, and “Repair & Optimize Database,” which will repair the database and optimize it for better performance. Both options can take some time to complete, and anyone will be able to access the page.
how to fix Error Establishing a Database Connection
  1. Once the repair is complete, delete the code from wp_config.php file and check your site to see if the error is resolved. 

Step 4. Checking if the Server is Working Properly

It’s common for servers to experience downtime, which means your website may not be accessible to visitors. This can happen if you’re using single server, but it’s less likely if you’re using cloud-based plan since your website will automatically switch to a different server.

If you’re on a shared hosting plan, heavy traffic can cause your server to slow down or crash, resulting in an error message for your site. Here’s what you can do to troubleshoot:

  1. Check if other websites on the same server are also down. If they are, it’s likely that the server is experiencing an issue.
  2. If you got access to VPS, local, or dedicated server, log in to the hosting account and check for any notifications regarding your server’s status. If it’s not running, you may need to restart it to restore access to your website.

Step 5. Rebooting Server

Even when the server is up and running, it can be helpful to reboot the server occasionally, it assists in resolving specific problems that are preventing your website from operating as it should.

If you have a VPS, local, or dedicated server, you can restart your server by logging in to hosting account and looking for the icon or option that allows you to do so. This will typically be located in the server management section of your hosting account.

However, if you are on a shared hosting plan, you won’t have the option to restart your server, as this is something that can only be done by the hosting provider. In this case, you can contact your hosting provider’s support team and request they restart the server for you.

Rebooting Server

Step 6. Updating WordPress or Removing Corrupted Themes and Plugins

WordPress files can sometimes become corrupt, leading to errors on your website. This can be caused by outdated plugins or themes, problems with WordPress core, or issues during updates or connections to external services.

To fix this issue, you can either remove files with the help of FTP or access them through your hosting provider’s admin panel.

Checking the wp-config File - Error Establishing a Database Connection

If you’re using cPanel, you can access the website files by going to the “Files” section and selecting “File Manager.” From there, navigate to the “public_html” folder if the website is main site on the server. If it’s another site on the server, find a folder matching the website name and click it. Next, look for the “Content” folder and click it.

Updating WordPress or Removing Corrupted Themes and Plugins

Once you’re in the “Content” folder, find the folders that contain your plugins and themes. Try renaming the folder for the suspected culprit based on what was updating or needed updating. Plugins are often the cause of file corruption, so start with those if you’re unsure. If this fixes the problem, rename the folder with its original name and open the folder. Then, start renaming one of the themes or plugins and test the website till the error disappears. If you can identify the problematic theme or plugin, delete it to solve the issue.

Updating WordPress or Removing Corrupted Themes and Plugins

However, if renaming the plugins and themes folders doesn’t solve the problem, then the issue may lie elsewhere, and it’s time to consider other solutions.

Step 7. Updating Website URL

Sometimes the issue with the error establishing a database connection could be related to the website URL in the database. Although it’s not very common, it’s still worth trying if other solutions don’t work.

To update your site URL, you can go to the Databases section in your hosting account’s cPanel and choose phpMyAdmin. Then, select database and click on SQL. After that, add the following SQL line with the URL:

UPDATE wp_options SET option_value=’examplesite.com’ WHERE option_name=’siteurl’

Updating Website URL

Remember to replace “examplesite.com” with your website’s URL. Then, click on Go to update URL in the database. Finally, check if the issue is resolved by reloading your website.

Step 8. Replacing Corrupt WordPress Files

If you suspect some of the WordPress files are corrupt, one solution is to download a fresh WordPress copy and extract the files. Make sure to download the same version of WordPress as the one you are currently using.

Once you have the new WordPress files, you need to remove the wp-content folder as well as the file named wp-config-sample.php. The wp-content folder contains your plugins, themes, and uploaded files. 

Deleting this folder won’t delete your actual content or settings, but it will remove any customizations made to the site. The wp-config-sample.php file is a sample configuration file that you can use to create a wp-config.php file, which is a critical file that contains your database connection information.

After deleting the wp-content folder and the wp-config-sample.php file, you need to upload the fresh WordPress files in the root folder on your server. This will replace all the WordPress files but the two files you just deleted. You can do this using an FTP client or through your web host’s file manager.

Once you have uploaded the new files, try accessing your website again to see if the issue has been resolved.

wordpress.org

Step 9. Contact Your Hosting Provider

Contacting your hosting provider can be helpful in resolving the “Error establishing a database connection” issue in WordPress for a few reasons:

Checking server-side issues: 

The hosting provider can check if the issue is related to a server-side problem, such as a problem with the MySQL database server or an issue with the server configuration.

Checking database credentials:

The hosting provider can check if the database credentials are correct and if the database user has the necessary permissions to access the database.

Checking resource allocation: 

The hosting provider can check if your website is exceeding resource limits or hitting resource caps, which could lead to issues with the database connection.

Restoring a backup: 

If necessary, the hosting provider may be able to restore a backup of your website or database to a previous state before the error occurred.

Overall, contacting your hosting provider can be helpful in diagnosing and resolving the “Error establishing a database connection” issue in WordPress, especially if the problem is related to server-side issues or resource allocation.

Final Thoughts

Finally, the “error establishing a database connection” issue in WordPress can be frustrating and confusing for website owners. However, with the right steps and troubleshooting techniques, the problem can be identified and fixed. Readers can learn how to check the database login credentials, repair the database, and update WordPress files by following the steps outlined in this blog. Contacting the hosting provider can also be beneficial in resolving any server-related issues. Website owners can successfully resolve the “error establishing a database connection” issue and get their site back up and running with a little patience and persistence.

If your issue is still unresolved, don’t hesitate to get in touch with WPclerks a WordPress support services providing company. We are available to help by taking care of your one-time tasks quickly and easily. Forget about hours spent searching for solutions yourself.

Frequently Asked Questions (FAQs)

What causes the “Error establishing a database connection” message in WordPress?

This error occurs when WordPress is unable to establish a connection to the database. This could be due to incorrect database credentials, corrupted files, or problems with the database server.

How can I fix the “Error establishing a database connection” message in WordPress?

There are several steps you can take to fix this error. You can check your database credentials, repair corrupted files, increase server resources, and contact your hosting provider for assistance.

How do I check my database credentials in WordPress?

You can check your database credentials in the wp-config.php file located in the root directory of your WordPress installation. Make sure that the database name, username, and password are correct.

How do I repair corrupted files in WordPress?

You can repair corrupted files by replacing them with a fresh copy of WordPress files. You can also disable plugins and themes, rename the .htaccess file, and restore from a backup.

How can I increase server resources to fix the “Error establishing a database connection” message in WordPress?

You can increase server resources by upgrading your hosting plan, optimizing your database, and disabling unused plugins and themes.

What should I do if none of the above steps work?

If none of the above steps work, you can contact your hosting provider for assistance. They can check the server logs and provide you with additional troubleshooting steps.