Error Establishing a Database Connection in WordPress How to Fix Them

Error Establishing a Database Connection in WordPress How to Fix Them

Are you seeing ‘Error establishing database connection’ on your WordPress website? It is a fatal error that makes your WordPress website inaccessible to users.

This error occurs when WordPress cannot connect to the database. Many things can affect your WordPress database connection, which makes troubleshooting a bit difficult for beginners.

In this article, we will show you how to easily fix the error establishing a database connection in WordPress. 

Note: You can see the tutorial video at the end of the article.

What does WordPress "Error Establishing Database Connection" mean?

Before we look at the error, we need to understand how WordPress works. Like most CMS (Content Management System) applications, it consists of two integral parts – files and a database.

Files are the framework of the website and contain WordPress core functionality, media, themes and plugins.

On the other hand, the database contains the custom settings and unique content of the website. All pages, text, text and design are saved as input to tables.

The files communicate with the database through various PHP and MySQL functions and extract data visualized in web pages.

Therefore, when your website cannot connect to the database, it cannot receive the data it needs to display the pages. WordPress warns you of the problem by displaying the message “Error Establishing Database Connection” as seen in the image below.

error-establishing-a-database-connection-2

What are the most common causes of “Error Establishing Database Connection”?

The ‘Error establishing database connection’ issue can be caused by incorrect database information in your WordPress settings, corrupted database, or an unresponsive database server.

A database is software that makes it easy to store, organize and import data into other software.

As a content management system, WordPress uses a database to store all your content and other website data. It then connects to the database every time someone visits your website.

WordPress needs the following information to connect to the database:

  • Database name
  • Database username
  • Database password
  • Database server

This information is stored in your WordPress configuration file called wp-config.php.

If any of these items are incorrect, WordPress cannot connect to your database server and you will see the error ‘Error establishing database connection’.

It is one of the most common WordPress errors. Besides incorrect credentials, this error can also appear if the database server is down or the database files are corrupt.

Let’s take a look at how to fix the error establishing database connection in WordPress with step-by-step troubleshooting.

There are several different scenarios where the error appears. The most common are:

  • Incorrect database settings in the configuration file – Incorrect database credentials are probably the most common cause of an interrupted database connection. If the database name, username, password or hostname is incorrect, the website will not connect to the database and will give “Error establishing database connection”.
  • Insufficient database user privileges – Another reason for the error to occur is that the database user does not have sufficient privileges even if the credentials are correct. The user must have permission to execute any MySQL operation on the database.
  • Corrupted database tables – WordPress is a highly dynamic system and every time you make changes to your website, the tables of the database are changed. Adding or deleting plugins and themes creates new tables or deletes existing ones, new pages add new entries, etc.

With so many MySQL operations executing, sometimes tables can get corrupted, leading to another variation of “Error establishing database connection” – “One or more database tables are unavailable”.

  • Incorrect website URL – In rare cases, “Error establishing database connection” can be caused by incorrect website URLs.
  • Corrupted WordPress files – Another reason you might see the “Error Establishing Database Connection” message is because your website’s WordPress core files are corrupt. This can be caused by a malware infection, failed update, or incomplete file transfer when manually migrating your site.
  • The database server is down or overloaded.

How to Fix “Error Establishing Connection” in 6 Methods

Below are the six most common solutions to fix the WordPress “Error Establishing Database Connection” error. Make sure you have a backup of your site before you start dealing with the error. If things go wrong, you can use it to restore your site.

A - Make Sure Your Domain Name Reveals Your Hosting Provider

  • Difficulty: Easy
  • Success rate: Moderate
  • Estimated time to fix: 1 minute

Switching to a new hosting provider automatically moves your files to the new host. But if your domain does not point to your host where the database is hosted, it will cause a database error. 

You can check this in the whois section of any hosting site.

If the results show otherwise, change the nameservers at your domain registrar. To get a list of nameservers, simply access your hosting control panel and enter them in your domain registrar’s control panel.

Another option is to forward your domain through the A record. It is a type of DNS record that assigns your domain to the correct IP address. Note that the A record only points to a static IP address.

To perform this alternative, find your current IP address or A record value and replace them with those from your new web host. Do this using a local computer console or an online DNS checker.

In the example above, the website’s A record value is 123.4.567.89. If the value matches your new hosting IP address, your domain is already flagged correctly. Otherwise, you’ll need to update it from your domain registrar’s control panel.

B - Check Your Database Server

  • Difficulty:Easy
  • Success rate:Medium
  • Estimated time to fix:1-2 minutes

The message “Error Establishing Database Connection” may also appear on your WordPress site when your MySQL server is down. While many reasons can cause this error, often the problem is too many connections.

A database server has a maximum number of clients connected at a given time.

Once this reaches maximum capacity, it may not be able to handle any more connections. Therefore, we recommend contacting your hosting provider to check your MySQL server.

If you host multiple sites on the same server, you can see if your MySQL server is down on those sites as well. By the way, if you’re hosting a single site, use PHP to connect to MySQL and check if your WordPress database user has sufficient permissions.

Go to your hosting control panel and access your public_html folder. There, create a new PHP file and name it whatever you want. In this example, we’ll name the file test.php.

Paste the code below into the file.

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

Replace the database password and username with your credentials. Finally, save this file on your website and access it via web browser.

If it’s connected successfully, it means that your user has enough permissions, and something else is causing the “Error Establishing a Database Connection” message to appear.

Usually, a website on a shared server tends to experience database errors due to splitting resources. Performing website caching with plugins like W3 Total Cache or WP Rocket is a good way to reduce the load on your server.

C - Check Your Database Host Information

If you are sure that your database name, username, and password are correct, you may want to make sure you are using the correct database host information.

Most WordPress hosting companies use localhost as your database host. However, some managed WordPress hosting companies use separate servers to host their databases. In this case, your database host information will not be localhost.

You need to contact your WordPress hosting company to verify your database hosting information.

D - Repair WordPress Database

For example if you are getting different error in wp-admin “One or more database tables are unavailable. The database may need to be repaired”, then you need to repair your database.

You can do this by adding the following line to your wp-config.php file. ‘That’s all, stop editing! Happy blogging’ line wp-config.php.

define('WP_ALLOW_REPAIR', true);

Once you’ve done that, you can see the settings by visiting this page: http://www.yoursite.com/wp-admin/maint/repair.php

E - Fix Corrupted Files

Besides corrupted databases, corrupted files can also be the cause of “Failure Establishing Database Connection”.

A faulty theme or plugin is one of the factors that cause file corruption. Check them individually to determine which component is causing the error. If the error occurs after activating the new one, your theme might be the problem.

To fix this issue, go to Appearance -> Themes in your WordPress dashboard and then switch to another theme.

Go to the File Manager menu in your cPanel clipboard. Click on public_html -> wp_content and find the plugins folder. Right-click the folder, then select Rename

Doing so will disable all your plugins at once. The same troubleshooting steps apply to deactivating all WordPress themes. However, make sure that you do not disable both themes and plugins at the same time as this will make it much more difficult to find the root of the problem.

If the error disappears after renaming the plugins folder, the problem is with one of the plugins.

After regaining access to your site’s backend, restore the plugins folder to its original name. This way, not all WordPress plugins will still be active, but you can activate each one from the admin area.

Important! Remember to delete the WordPress theme or WordPress plugin that caused the error.

However, if none of the above steps work, try reinstalling the core files on your WordPress site. To do this, download a new version of WordPress.

F - Create a New Database

If all of the above methods still don’t work, your database may have been completely corrupted and can no longer be used. In this case, you need to create a new MySQL database and user from your hosting control panel.

After a new MySQL database is created, restore a backup of your website to make sure you don’t lose any important website data.

In cPanel go to Databases -> MySQL Databases. At the top of the page you will see the Create New Database section. Start filling in your new database name and click Create Database.

The system will then add your new database to the Available Databases list.

You can also create a new MySQL user from the same window and assign it to your new database. Users can perform a variety of tasks, from creating new tables to updating rows, depending on privileges. 

In the MySQL Users section, enter your new database username and password, then click Create User.

Once a new user is created, scroll down to add the user to your new database.

Under the Replace Databases section, you will see two options to check or repair your database. Select the Repair Database option to fix a corrupted database.

Wait for the process to finish and access your WordPress site to see if the “Error Establishing Database Connection” message still appears.

G - Restarting the Web Server

Users on private servers, local server, and virtual private servers (VPS) can try to restart their servers.

This will restart your web and database server which may fix some temporary bugs causing the error.

H - Ask For Help

If all else fails, you may need to contact your web hosting company. All good WordPress hosting companies will help you troubleshoot the problem, point you in the right direction, and even fix it for you.

Video Tutorial

Play Video

If you have any questions about this article or other topics, please click the button below. Your questions will be answered soon.

To see all our themes and experiences, you can go to our themeforest profile by clicking the button below. Click and see now.

Leave a Reply

Your email address will not be published.