WordPress Error Establishing a Database Connection – WordPress is the most popular and robust Content Management System that is used worldwide by several WordPress developers. In fact, more than 35% of the total website in the world wide web is built with WordPress, as you will get several benefits of WordPress while using it as the backbone of your website.

But being a WordPress website owner, you will feel most helpless when all of a sudden you see an “Error Establishing a Database Connection” in place of your beautiful and functional website. No matter how many times you refresh, you won’t be able to access your website.

This is a bit scary situation, as when your website is down you lose your audience, your customer, and the most important thing you lose an opportunity to earn revenue. Not only that, but the downtime of your website will also place a negative impact on your SEO ranking.

Got scared, right! Don’t worry, this is one of the common issues in WordPress. In fact, a few days ago one of our own websites got hit with the same error, but we fixed it with a few minutes. There are several reasons that can land your WordPress website in an “Error Establishing a Database Connection”.

A sudden increase in huge traffic can also be one of the reasons for getting this error. So, it is definitely not necessary that you have done something wrong or placed a wrong code on your WordPress website for which you are seeing the “Error Establishing a Database Connection” error.

What is WordPress Database?

WordPress Database is a structured set of organized data, where all the essential information like the usernames, passwords, pages, posts, themes, plugins, configuration settings, etc. of your WordPress website are stored.

By default, WordPress uses the MySQL database, a Database management System where it stores all the necessary information. And when a visitor opens your website, your WordPress website sends a request to the Database server and fetch specific information required to show your visitor.

So, you must keep your WordPress database safe, secure, up and running.

What is Error Establishing a Database Connection?

What is Error Establishing a Database Connection - Blog Haveli
What is Error Establishing a Database Connection – Blog Haveli

As mention earlier, all of the essential information of your WordPress website, such as usernames, passwords, post data, page data, meta information, plugin & theme settings, etc. are stored in your MySQL database.

When a person visits your website, WordPress runs a PHP function that connects your website to the database and gets the specific information that is required to show to your visitor for that particular page.

And for any reason, if your WordPress is not able to get connected to your Database that means you have landed to an “Error Establishing a Database Connection”. When this error occurs, you will not able to see your frontend and will be left with a blank screen with “Error Establishing a Database Connection” message.

Alarming: Why and when does Error Establishing a Database Connection Occurs?

As you know WordPress is built using PHP and MySQL. While making your WordPress website all the information that is used to build the complete website, the maximum portion of the information is stored in the MySQL database, except the media files. Starting from the username, password to the theme or plugin setting all are stored in the database. 

Every time when a new page or a post is created, modified – all the information that is used for that particular page or post is stored in the database of your WordPress website. So that when the page is loaded, it gets and displays the complete website on the fly with the help of Php functions and MySQL quires.

So, when you get the message of “Error Establishing a Database Connection” it means that WordPress is trying to tell you that something went wrong while getting connected with the MySQL Database while loading your website.

That’s why the “Error Establishing a Database Connection” message show because without getting a proper establish connection to your MySQL Database server, WordPress cannot retrieve any information about your webpage that needs to put a page together.

Now, as we are pretty clear about the idea that why “Error Establishing a Database Connection” message occurs, so now let dig down a little bit more and get a clear idea about what are the various scenarios when the “Error Establishing a Database Connection” message can occur.

Incorrect Login Credentials: The major reason for getting an “Error Establishing a Database Connection” message is providing an incorrect login username or password of the MySQL database. WordPress user two login credentials one for the WordPress dashboard and another one for the MySQL Database.

So, if you have recently changed the login credentials, you will not be able to access your database with the old ones. These types of mistakes mainly occur when somebody shifts their WordPress website manually from one server to another.

Corrupt Database: Another possibility of getting “Error Establishing a Database Connection” message is your Database might have got corrupted. A database can get corrupted for several reasons like malware, incompatible or faulty plugins, themes, bad piece of code.

Or maybe somebody has made some changes in the database that have made it corrupt, even problems at your host server or server running out of memory can display you an “Error Establishing a Database Connection” message on your WordPress website.

Corrupt Files: Sometimes you may contain some corrupted PHP files in the WordPress directory, this may occur due to hackers, that can inject some malicious code into your Php files. Even when someone has unintentionally made a wrong change in the code, that might have got conflict with another Php script.

Server Downtime: Sometimes the server cannot be reached, so you can also get “Error Establishing a Database Connection” message if your server is having downtime or in a maintenance mode.

You might also get this error if you get sudden high traffic that is beyond your web hosting plan, as it would make your server unresponsive. Also check your ISP and the Internet connection, and your browser cache once you face this issue.

7 Easy Step-By-Step Guide to Fix WordPress “Error Establishing a Database Connection”:

As we prioritize your time value, so let’s get the most common issues and their solution first to save your precious time and help your WordPress website to gain its functionality again.

Server Not Reachable:

Server Statistics - Error Establishing a Database Connection - Blog Haveli
Server Statistics – Error Establishing a Database Connection – Blog Haveli

The first step for debugging this problem is to check your hosting server if it is up and running. Because at any point of time, if your database server is facing too many concurrent connections at a single point of time, it could generate the error – resulting in failure of server reach-ability.

Suppose your website was working perfectly a minute ago, but now it is showing “Error Establishing a Database Connection”, so the first thing that you should do is to look for your server resource, whether it has reached its maximum limits.

This happens because most of the web hosting providers specify a limit on their servers on how many connections are allowed at once until and unless you are using a dedicated server that involves a huge investment of money.

Though you can optimize this kind of issue by using a cache plugin or a server-level caching that will help to minimize the database interactions with your site. This type of problem mainly happens in the shared servers. You might also consider looking for the best web hosting for WordPress.

To verify your server resource, firstly, login to your web hosting, then go to cPanel to see whether your server resource has reached its maximum limits or not. If not then move on to the next step, but if your server resource has reached its maximum limits then contact your hosting provider to resolve the issue.

Nowadays, almost every web hosting companies provide a quick communication platform via chat, phone call, support ticket, etc. that becomes handy while facing this type of problem. So, if you are facing a server downtime, you must contact the hosting provider and state the issue to resolve it as soon as possible.

Check Your Database Connectivity:

The first step that you test is whether your WordPress is able to connect with your database server using the database credential that you have provided in the wp-config.php file. To do that create a PHP file in the root folder of your WordPress website, name it as checkdatabase.php.

Now paste the following code into checkdatabase.php file. Just change the values of dbname, username, and password with those that you have given in your wp-config.php file.

<?php 
$servername = "localhost"; 
$dbname = "database_name"; 
$username = "database_username"; 
$password = "database password"; 
try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); 
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    echo "Connected successfully";  
} 
catch(PDOException $e){  
    echo "Connection failed: " . $e->getMessage();  
} 

First of all, make sure to remove or delete this file after the test is over and now browse the checkdatabase.php file in your web browser, an example of the link is given below:

https://yourdomain.com/checkdatabase.php

If you are getting a “Connection failed” message, that means your WordPress is not able to connect with your database, so you have to check the credential of wp-config.php or the cPanel.

And if you are getting a successful message that means your WordPress is able to connect with your database, so you don’t have to check the credential of wp-config.php or the cPanel, both are working fine. But if you are still getting the “Error Establishing a Database Connection” message then you have to check the other options like Repairing Your WordPress Database or Fix Corrupt Files of your WordPress.

Check Your Database Login Credentials in wp-config:

If you don’t find any issues with your server performance then the next step you should take is to check and ensure that your database login credentials are correct.

In fact, this is one of the most common reasons behind getting the “Error Establishing a Database Connection” error especially when someone migrates an existing website to another web hosting. All the connection details of your WordPress to your database server are stored in the wp-config.php file, that is stored in the root directory of your WordPress site.

To access your WordPress wp-config.php file first, you need to login to the cPanel of your web hosting and then browse to the root folder of your site using the file manager. Once you locate and open the wp-config.php file using code editor, you need to check and correct four pieces of information – Database Name, MySQL database username, MySQL database password and MySQL hostname (server).

Database Name: 

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

In place of ‘xxxxxx’ you should put the database name that you have created at the time of WordPress installation. And if you have migrated your WordPress website from another server, provide the new database name that you have created in the new server.

MySQL database username:

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

In place of ‘xxxxxx’ you should put the database username that you have assign while creating the database for your WordPress website. And if you have migrated your WordPress website from another server, provide the new database username that you have created in the new server.

MySQL database password:

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

In place of ‘xxxxxx’ you should put the database password that you have assign while creating the database for your WordPress website. And if you have migrated your WordPress website from another server, provide the new database password that you have created in the new server.

MySQL hostname:

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

You should not do any changes if your hostname is mention as ‘localhost’. But if it is not ‘localhost’ like – define( ‘DB_HOST’, ‘xxxxxx’ ); then you should replace ‘xxxxxx’ with ‘localhost’.

After performing the necessary changes, save the file and reload your website. If you are not getting the “Error Establishing a Database Connection” error anymore then you are good to go, but if you are still getting the same error kindly follow the next step.

Check Database Details in cPanel:

If you are still getting the same “Error Establishing a Database Connection” error then the first thing that you have to check is whether the database details in the wp-config.php is matching with the database details in the cPanel of your web hosting.

To do that you need to login to the cPanel of your web hosting, then go to the phpMyAdmin section and click on it. On the left-hand sidebar, you should be able to see the database name that you have used for your WordPress website. Ignore the “information_schema” database.

Database Name - Error Establishing a Database Connection - Blog Haveli
Database Name – Error Establishing a Database Connection – Blog Haveli

Now, you need to verify that this is the correct database that you are using for your WordPress website. You can do that simply by clicking on the database, then click into the wp_options table where you will be able to find the URL of your website.

Database URL - Error Establishing a Database Connection - Blog Haveli
Database URL – Error Establishing a Database Connection – Blog Haveli

If you are not able to see the correct URL of your website in the wp_options table, then this is not the correct database that you are looking for. Repeat the same process until you are able to locate the correct database for your WordPress website.

Once you have located the correct database name, compare whether your wp-config.php contains the same database name in define( ‘DB_USER’, ‘xxxxxx’ ); if not please update the wp-config.php file by replacing ‘xxxxxx’ with the same database name that you have in your phpMyAdmin.

If your database name was already correct but you are still getting the “Error Establishing a Database Connection” message then you need to check your username and password that you have assigned to the database in cPanel.

Database Option - Error Establishing a Database Connection - Blog Haveli
Database Option – Error Establishing a Database Connection – Blog Haveli

For that, you need to go back to your cPanel and click the MySQL Databases option, where you can find the Current Databases option. Locate your database and verify whether the name of the Privileged Users assigned to the specified database is as same as your username provided in the wp-config.php file, if not update the wp-config.php file.

You can also change the password of the database’s username of the cPanel to make it as same as the password provided in the wp-config.php file, just by clicking on the change password option.

Fix Your WordPress Site URL

Database URL - Error Establishing a Database Connection - Blog Haveli
Database URL – Error Establishing a Database Connection – Blog Haveli

If you have built your WordPress website in a demo URL or in your local machine, then you might get an error after you transfer your website to a new URL. To resolve this issue, first, you have to locate your correct database with correct credentials.

Now, once you have located your required database from the phpMyAdmin, you need to navigate and open wp_option. At the top of the wp_option table, you will be able to see the values for your site’s URL and name. If the URL and name are matching to your desired WordPress website URL and name, then you are good to go. If not, then you have to update the table and provide your desired URL and name of your WordPress website.

Repair Your WordPress Database

Repair Database - Error Establishing a Database Connection - Blog Haveli
Repair Database – Error Establishing a Database Connection – Blog Haveli

One of the reasons for getting an “Error Establishing a Database Connection” message might your database has been corrupted. WordPress database is a little complex, and there are lots of tiny things have to need to stay working perfectly to avoid any kind of trouble.

To check whether your database is corrupt or not, you have to simply navigate to “yourwebsite.com/wp-admin/”, please replace yourwebsite.com with your own domain name. And after opening the link, if you are getting any error – “One or more database tables are unavailable. The database may need to be repaired.”, then you might consider your database is corrupted and it needs to be repaired.

To repair your WordPress database, you need to open your wp-config.php file in a code editor and then on any blank line, paste the following codes in it:

define('WP_ALLOW_REPAIR', true);

This code will enable the database repair option so that you can repair the database of your WordPress just by a single click. Now, save and close the file after you have to paste the code into your wp-config.php file.

The next thing that you have to do is open a new tab and navigate to the link as given below, just by replacing yourwebsite.com with your own domain name:

http://www.yourwebsite.com/wp-admin/maint/repair.php 

After you navigate to the given link, a database repair page will load, where you have to click on the Repair Database. And as soon as you press the Repair Database you will be taken to a results page where you can see the checks and repairs happening in real-time as shown in the picture:

Database Repaired - Error Establishing a Database Connection - Blog Haveli
Database Repaired – Error Establishing a Database Connection – Blog Haveli

Once the process gets completed, please make sure to open the wp-config.php file and remove the line of code that you have just pasted, which is define(‘WP_ALLOW_REPAIR’, true);.

Fix Corrupt Files of your WordPress Website

Your WordPress file can get corrupt for a couple of misfortunate events, one of the most common ones is when you may have got an update of a plugin or theme and while performing the update the whole website gets collapsed. Or maybe you have tried to customize something or made some changes in the codes and all of a sudden your complete site gets collapsed.

So, to solve this issue, first, go to your root folder and navigate to wp-content and open it. If you think that this error is produced after you made an update to a particular plugin, then rename the plugin folder or that particular plugin folder.

Now, reload the page, if it is working and not showing any error, then delete that plugin and reinstall it again. To get confirm which plugin was recently updated see the Date Modified section in the cPanel. But if the website is still producing an error, get a fresh WordPress on your website.

To get a fresh WordPress file without losing any data, first of all, download a fresh copy of WordPress from WordPress.org. Now unzip or extract the WordPress folder in your computer and delete the wp-content folder and wp-config-sample.php file which you will find within the WordPress folder.

Now upload the remaining files and folders to your root directory, overwriting your existing files and folders. This will help you to replace all the problems or the code malfunctioning from your website and will give you a fresh copy of WordPress along with a clean and not corrupted code.

How to Avoid Error Establishing a Database Connection in the Future?

While there is not any 100% fail-proof technique that could protect your WordPress website from getting an “Error Establishing a Database Connection” error. But yes, you can at least prevent up to some extent. To achieve that goal, you need the two most essential elements:

Good Web Hosting: If you have hosted your WordPress website with one of the best Web Hosting providers, then you are in a safe hand. Nowadays, many good web hosting providers offer premium and fast support, that would cut down half of your technical issues.

And while working with some of the best Web Hosting providers you don’t have to worry about your sudden peek in the traffic. The Web Hosting providers will manage it without producing any “Error Establishing a Database Connection”.

But for that, you need to wisely choose your web hosting, before that you need to know your traffic, the complexity of your website and so on. You can get a good and clear idea of how to choose the best web hosting for WordPress before purchasing any web hosting for your WordPress website.

Best Back-up Plugin: Choosing and installing one of the best backup plugins can avoid any technical bug that may occur in the future. Once you take a complete back-up of your website including all your media files and database, you can restore it at any point of time.

So, when you are getting any technical errors due to new update, or any custom code that have made your website collapse, on that very moment, you can restore your website to the previous stage just by a single click of your mouse. UpdraftPlus WordPress Backup Plugin is one of the best Back-up plugins that you might consider to use on your WordPress website.

You can also check out all the essential and Best WordPress Plugins that can keep your WordPress website safe and secure, preventing you from getting any error and keep your website up and running.


Frequently Asked Questions (FAQs):

What is Error Establishing a Database Connection Error?

Error Establishing a Database Connection Error is a error that occurs when your PHP codes is unable to connect to with your MySQL database and cannot retrieve any information it which is required to fully build a particular web-page.
So, for any reason, if your WordPress is not able to get connected to your Database that means you have landed to an “Error Establishing a Database Connection”. When this error occurs, you will not able to see your frontend and will be left with a blank screen with “Error Establishing a Database Connection” message.

What is WordPress Database?

WordPress Database is a structured set of organized data, where all the essential information like the usernames, passwords, pages, posts, themes, plugins, configuration settings, etc. of your WordPress website are stored.

Why and when does Error Establishing a Database Connection Occurs?

Common scenarios when the “Error Establishing a Database Connection” message can occur are Incorrect Login Credentials, Corrupt Database, Corrupt Files and Server Downtime.

Step-By-Step Guide to Fix WordPress “Error Establishing a Database Connection”

Some of the most common issues and their solution that can help your WordPress website to gain its functionality again are check Server Not Reachable, Check Your Database Connectivity, Check Your Database Login Credentials in wp-config, Check Database Details in cPanel, Fix Your WordPress Site URL, Repair Your WordPress Database, and Fix Corrupt Files of your WordPress Website.

How to Avoid Error Establishing a Database Connection in the Future?

While there is not any 100% fail-proof technique that could protect your WordPress website from getting an “Error Establishing a Database Connection” error. But yes, you can at least prevent up to some extent. To achieve that goal, you need the two most essential elements – Good Web Hosting, and a Best Back-up Plugin.

LEAVE A REPLY

Please enter your comment!
Please enter your name here