Easily fix “Error Establishing a Database Connection” in WordPress [7 Tips]

Published No Comments on Easily fix “Error Establishing a Database Connection” in WordPress [7 Tips]

One of the most common issues that a WordPress user often faces is “Error Establishing a Database Connection”. It may occur for many reasons. If your database connection is not secured, you’ve overloaded traffic on your site, your server is down, etc.

How do you deal with this issue when you see it?

Well, first of all, you don’t have to panic. Take a deep breath and calm yourself. Because every problem has a solution.

In this tutorial, I’m going to share all the possible ways that you can quickly fix this “well-known” WordPress site’s issue by yourself.

But before I proceed, let’s get introduced to “the WordPress database” first.

A simple explanation of WordPress database

Explaining what is WordPress database - Error Establishing a Database Connection

A WordPress database is a structured collection of data that stores all the content and settings related to a WordPress website. It is an important element of the WordPress content management system (CMS).

After you install WordPress, it immediately creates a database to store different items on your site. For example, blog posts, pages, comments, user data, plugin data, general settings, and theme settings. And this is where the role of a database comes in. WordPress fetches these data and shows them to your visitors.

So when it fails to connect with your WordPress site, the message pops up “Error Establishing a Database Connection

But your users may still see the front page of your website because of the “cache.” file. As an admin, you won’t get the backend access. So this problem occurs for many reasons.

Now let’s talk about them one by one in the next portion!

What causes showing “Error Establishing a Database Connection

What causes showing "Error Establishing a Database Connection"

WordPress is written in PHP and utilizes MySQL as a database. After someone visits your site, certain PHP codes will ask the database for information about your site. And then the user can see the information on your website.

The database connection error can be caused due to many reasons. Such as:

  • Wrong information in your database
  • Error in the WordPress general settings
  • A corrupted database or the database server is not working
  • Database responsiveness issue

However, WordPress needs some general information about a database to make a secure connection. Such as database name, username, password, and server. If any of these things are wrong, WordPress won’t be able to connect to your database server, and you’ll get the “Error establishing a database connection” message.

👨🏻‍💻Learn how to remove malware from the WordPress site💥

How to fix when you see “Error establishing a database connection” on your WordPress site

How to fix when you see "Error establishing a database connection" on your WordPress site

Let’s look at a step-by-step guide to fixing the database establishment error in WordPress!

01. Check your WordPress credentials

In the earlier section, I’ve already discussed how WordPress sends some required information to make a secure connection with the database. And they are your database name, username, password, and server. And these are your website’s credentials.

You’ll find them in your wp-config.php file of your cPanel. It is the configuration file for WordPress, which has essential settings like database details.

Here it’s👇

Do make sure that your website has the right credentials. Just log in to your cPanel dashboard, find the file manage (different previews for different hosting providers), and open the wp-config.php file. And then check the credentials.

That’s all!

02. Match your database host information

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

Most companies that host WordPress use localhost as the host for their database. But some companies that run WordPress use separate servers to host databases.

In that case, the host details for your database won’t be localhost. You need to confirm your database host details with the company that hosts your WordPress site.

03. If you can repair your database

It’s another most common “Error Establishing a Database Connection” issue that WordPress users face. If your database is corrupted, you may need to repair the WordPress database to solve it. Common errors like database table unavailability, or something else, repairing the database could help you to solve the problem.

Well, due to different issues in the plugins or themes that are regularly added or removed, you may encounter this issue. Nothing to worry about, it also has a solution.

✅ So again open your cPanel, and go to your wp-config.php. And then add the code inside this file.

define('WP_ALLOW_REPAIR', true);
repairing the WordPress database

✅ After you do that, just visit – http://www.yoursite.com/wp-admin/maint/repair.php

✅ You’ll see the below message if you don’t add the line (^) to your wp-config.php.

✅ Once you add the code line in the wp-config.php, you’ll see this message after visiting the URL.

Note: You don’t have to be logged in to your WordPress admin section. After you add the code to your wp-config.php, you’ll automatically see this message and repair your database.

04. Check your database server

The “Error Establishing a Database Connection” can appear if your database server (MySQL) is down. There are so many reasons that let your database server down. One of them is reaching the maximum connectivity issue.

For example, if a database receives a maximum number of client requests, this pitfall may occur. For this, your hosting server fails to respond to so many requests at a time.

So how to solve if your server is down or taking too much time to load? Let’s figure out below:

✅ First, go to your cPanel again and access your public_html. And there, create a new folder and name it mytest.php

Creating a new file in cpanel

✅ Open the file and paste the below code in the file.

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

✅ And then hit the save button after adding the code

how to add a code in the public_html folder

Now, you need to replace the database password and username with your credentials. And lastly, go to your website to check whether it’s working or not. If you see that it’s connected successfully, that means your user has enough permissions, and something else could be the reason for showing the “Error Establishing a Database Connection”.

05. Restart your server

Another best way to solve the “Error Establishing a Database Connection” is to restart your server for getting a better performance. However, your server is already active, but you can still restart it. And it works.

✅ Try finding the button “restart server” in your hosting account and click on it. And that’s all.

Remember: This won’t work for shared servers. Only VPS, local, and dedicated systems will be able to use this.

06. Restore the corrupted files

Corrupted files can also cause the “Error Establishing a Database Connection” issue in WordPress.

One common cause of file corruption is a faulty theme or plugin. To identify which one is causing the error, you can check them separately.

Checking through the Theme

If the error occurs after activating a new theme, it’s possible that the theme is the problem.

✅ To do that go to Appearance>Themes. Then just activate other themes to fix this error.

Choosing a theme

Checking through the Plugin

If switching to a different theme does not solve the “Error Establishing a Database Connection” issue, it may be caused by a corrupted plugin. To identify the problematic plugin, you can try disabling all plugins and then re-enabling them one by one.

Another option is to deactivate all WordPress plugins by renaming the folder that contains them in your hosting control panel.

Here’s how you can do it:

✅ Log in to your hosting control panel.

✅ Head to the public_html folder

Opening public_html folder

✅ Then click on the “wp-content” folder and find the “plugins“. Rename the plugin to any.

It will disable all of your plugins.

Changing the plugin name

Note: The same troubleshooting steps are applicable to disabling all WordPress themes. However, you should avoid disabling both themes and plugins at the same time since it will become more difficult to identify the root cause of the problem.

If the “Error Establishing a Database Connection” issue is resolved after renaming the plugins folder, it indicates that one of your plugins is causing the problem.

After you get access to your site’s back-end, you can now again restore your plugin folder name to its original. And you’ll find all of them inactivated on your plugin dashboard. For that, you need to activate them one by one.

07. Ask for help (Bonus)

If all other solutions fail, you should consider contacting your web hosting company. A reliable WordPress hosting provider will assist you in troubleshooting the issue, guide you in the right direction, or even resolve it on your behalf.

On the other hand, you can hire WordPress developers or seek help from the WordPress community to resolve your database error problem.

Over to you

Experiencing the “Error Establishing a Database Connection” error on your WordPress website can be frustrating as it prevents your website from loading and may cause you to lose traffic and customers.

This database error happens when the WordPress website cannot establish a connection to the server, and it may be caused by different factors such as incorrect login credentials in the wp-config.php file, corrupted files, server overload, and so on.

I tried my best to convey the most common issues and how to fix them. I hope this post will help to solve the “Error Establishing a Database Connection” for your WordPress site.

However, if you want to boost your eCommerce growth, you can check out the article to get proven some tips and tricks.

By Nahid Sharif

Hello, I'm nahid. A WordPress enthusiast, marketer, writer, traveler, and future influencer. Taking writing as a passion and marketing as a profession. A big fan of crime thrillers & thriller suspense movies. If writing didn't work for me, I would definitely be a private detective😎

Leave a comment

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

%d bloggers like this: