WordPress

A Solution to WordPress’ Error Establishing a Database Connection

top wordpress development agency london in uk

A database connection error occurs when WordPress is unable to establish a connection to the database being used. In such cases, your WordPress website may display an error message stating “Error establishing a database connection.” This critical error prevents users from accessing the WordPress website in question.

There are several potential factors that can cause this WordPress database connection error, making it somewhat challenging for beginners to identify the problem.

The purpose of this article is to provide guidance on resolving the “Error establishing a database connection” error.

The WordPress Database Connection Error

In WordPress, the ‘Error establishing a database connection’ issue can occur due to incorrect database information in your settings, a corrupted database, or an unresponsive database server.

Databases are programs that facilitate the storage, organization, and retrieval of data for other programs, enabling manipulation as needed.

As a content management system, WordPress stores all your website’s content and other data in a database. When someone visits your website, it establishes a connection to the database to retrieve information.

To establish this connection, WordPress requires the database name, username, password, and database server information. Let’s delve into the causes of this error:

  • The most common cause is incorrect database credentials. The login information for MySQL is separate from your WordPress login information.
  • A corrupted database can also trigger this error. Constant installation and deletion of themes, plugins, or users can lead to database corruption. Information may be accidentally deleted, or a table may be missing or corrupted.
  • WordPress files may be corrupt as well, which can occur due to malicious activities by hackers.
  • Your database server may be down. Overloading the database or having too many concurrent connections can cause the web host to go offline. On shared hosting environments, multiple users share the same resources.
  • High traffic can also overwhelm your web host’s database connections. Traffic increases can cause temporary database problems.

Fixing “Error Establishing a Database Connection”

We’ll recommend a path most likely to fix the problem for you quickly. After that, we’ll show you how to do all the fixes manually.

1. Contact Your Web Host

It is always advisable to seek support from your web host when encountering issues. If your website was functioning properly a short while ago but is now experiencing problems, it could be a fault with your web host.

In today’s landscape, reputable hosts typically provide live chat support. Reach out to them and explain that you are encountering an “error establishing a database connection.” Ask them the following:

1. Confirm whether the database server is operating smoothly without any issues.

  • This is important to ensure there are no spikes in activity or traffic on the server.

By informing them about the specific error you are encountering, they can identify the exact cause and, if you allow, correct it for you. Furthermore, they will assess the condition of your database and address any suspicious activity on the server.

If it turns out that the problem is not on their end, you may need to address it manually.

Manual Steps

To address the issue manually, follow these steps:

  1. Connect to your files using FTP (File Transfer Protocol). Your FTP account requires a login and password. If you are unsure about these credentials or their location, the easiest way to find them is by searching through the initial emails you received from your web host when you signed up. Alternatively, you can ask the live chat support representative to provide you with these details.
  2. Use the FTP login and password to connect to the server. Once connected, navigate to the WordPress root folder on the server. This folder is typically named “public_html” or “www.” After establishing the FTP server connection, you might find yourself in this directory.
  3. To confirm that you are in the WordPress root folder, look for subfolders such as “wp-content” and “wp-admin.” If you can see these folders, it indicates that you are in the correct location, which is the WordPress root directory.

2. Make sure that your database connection credentials are correct.

To establish a connection with the database, WordPress requires specific username and password credentials. Generally, these access details remain unchanged and do not require user intervention. However, certain modifications to site files, hosting environment, or other factors can lead to unexpected outcomes. While rare, it is possible for the access credentials to be modified. When incorrect credentials are used, it triggers the “error establishing a database connection.”

To rectify this, follow these steps:

  1. Connect to your site using FTP.
  2. Locate the “wp-config.php” file in the WordPress root directory.
  3. Download and save a copy of “wp-config.php” to your desktop.
  4. Within the “wp-config.php” file, you will find the following lines:

define(‘DB_NAME’, ‘some_db’);
define(‘DB_USER’, ‘some_username’);
define(‘DB_PASSWORD’, ‘some_pass’);
define(‘DB_HOST’, ‘localhost’);

Copy these four lines from the “wp-config.php” file and paste them into the chat window of your web host’s support.

Ask the support representative to review the details and confirm if they are correct for connecting to your database. They will examine the information and provide you with the accurate credentials, if necessary.

3. Make Sure Your Database Isn’t Corrupt

A smooth functioning WordPress database is crucial to avoid encountering errors, such as the “error establishing a database connection.” If something goes wrong with the database, it can lead to this issue. Here’s how you can check and potentially repair your database:

  • Start by accessing your WordPress dashboard at YOURSITE.com/wp-admin. If you still see the “error establishing a database connection” message, proceed to the next step.
  • Pay attention to any recommendations or suggestions provided by WordPress if you encounter a different error message. WordPress often offers helpful troubleshooting solutions for recent issues.
  • In some cases, you might come across an error message stating, “One or more databases are unavailable.” This indicates that you need to “repair” the database.
  • To repair the database, connect to your site via FTP and save a copy of the “wp-config.php” file from the WordPress root directory to your desktop.
  • Open the “wp-config.php” file using a text editor. Locate the section just before the line that says “That’s all, stop editing.”
  • Add the following line just above the mentioned line:

define(‘WP_ALLOW_REPAIR’, true);

  • Save the changes to the “wp-config.php” file and upload it back to your site using FTP, replacing the old file.
  • Access YOURSITE.com/maint/repair.php in your web browser to initiate the repair process.
  • On the repair page, click on one of the buttons provided, such as “Repair Database” or “Repair and Optimize Database.” Once the automatic repair process completes, check your site again.
  • If no errors are displayed, that’s a positive outcome! However, remember to restore the “wp-config.php” file to its original state by removing the line you added earlier. Upload the modified “wp-config.php” file back to your site via FTP.

4. Check Your Theme Files or Plugins.

WordPress files can become corrupt due to a few unfortunate circumstances.

Perhaps you updated a plugin, and everything collapsed. Is there an external service connected to the site? Maybe you edited some files manually?

If any of the above sounds familiar, you likely have identified the culprit. You can skip this step if you haven’t made any changes.

Follow these steps:

  • Inside the “wp-content” directory, rename the “plugins” folder to something else (anything other than “plugins”) and save the changes. Refresh your site to check if the error persists.
  • If the error disappears, you now know it’s a plugin issue. Go back to the FTP client and rename the folder back to “plugins”. Change the names of each folder within the plugins folder one by one.
  • Continuously check your site to determine when the error occurs or disappears. If you narrow it down to one specific plugin, you have found the culprit. You can search online for a solution or delete the plugin.

Let us help you

If you are still unable to figure it out or continue to encounter difficulties despite your research and efforts, let us assist you. WDA is a professional agency that provides support to customers in various scenarios. We have a proven track record of successfully developing, organizing, optimizing, and resolving issues for many of our customers.

Want to discuss a project?

Contact us and our specialists will respond to your inquiry as soon as possible.