How To Resolve WordPress Error Establishing A Database Connection

Share On :

how-to-resolve-wordpress-error-establishing-a-database

WordPress is a website creating and managing tool that enhances user experience by providing plugins and templates suitable for any work including business, online store, blog, and portfolio. This flexibility allows you to customize your website the way you want and make it your own. 
Despite customer-friendly services, something that frustrates users and website owners is the message that pops up out of nowhere “Error Establishing a Database Connection”. But, you don’t need to worry anymore as it can be fixed! 

Here Is How You Can Fix This Issue To Get Rid Of Error Establishing A Database Connection

Firstly, lets understand how WordPress works: 

As we all know, the information of every website is stored in a database. When the error shows up, it clearly means that website fails to connect to the database. Every website is designed via a programming language and so is the WordPress created with PHP. Whereas, the function of MySQL system is to store and manage data. What exactly happens when a user visits a site? Whenever someone visits the website for any purpose, PHP sends the request to the MySQL system to collect all the necessary data the user asked for. However, if the connection between PHP and MySQL is disrupted, users receive the message “Error Establishing A Database Connection”. Let me provide you with a relevant example of this. Let’s suppose you have called your friend but the call suddenly dropped as the signals were weak. So your communication with your friend was disturbed due to weak signals. Those weak signals are the reason behind the Error message that appears in WordPress. 

Before we hop onto fixing the problem, the initial step is to know what actually leads to Error Establishing a Database connection: 

Database is like the store house where the information of any website is kept. This requires proper information such as the database name think of it like some label, your username and password for WordPress to connect to the database. How does this work? All this information is stored in some special file (wp-config.php) which helps WordPress connect to the database. If the user fails to provide WordPress with any one of the proper credentials, this error message may pop up. 

Secondly, if you have moved your website to a new server or hosting company, your first step should always be updating your credentials. Otherwise, every time you log in the website will show the error.

Pirated themes and plugins can also be the source of error. This brings damage to the database in the form of losing information or making it unavailable. As a consequence, a notification bursts forth claiming “Error Establishing A Database Connection”.

Sometimes, one of the main reasons of error is the sudden increase in website traffic. It happens when a large crowd is trying to have access to your website at the same time. 

Core files help your website run smoothly by providing it with a proper structure and controlling its basic functionality. However, if there is an issue with the plugin or design then it can negatively impact the core files. Similarly, if someone tries to modify core files and does it incorrectly, the connection between the website and the database is disrupted. 

Break Free From This Error Message With The Following Ways: 

Your Domain Must Be Linked To Your Host: Here Is The Way To Do It

When website is handed over to a new host, all the files are automatically moved with it. To access those files, you need to provide the website with updated credentials because the previous database has none. Whenever your database points to the previous host, it shows the error. Think of it like you have bought a new home and you shift all your stuff there. Now it’s time to hold a party at your place by calling your friends and showing them your new beautiful house. For this, you’ll have to provide them with the new home address rather than the previous one. 

How to get it done right? Begin by checking the nameserver which means finding out where your domain points to : old host or a new host. There are multiple tools available such as Dnschecker, Dnsdumpster, DNS lookup, Host, Dig and many more. You can use whatever meets your convenience. Additionally, you can look up the dashboard or control panel provided by the hosting provider. Once you find the relevant name servers, copy them and paste them into the correct field in the domain registrar control panel when you log in. 

Monitor Your Database Server Status: 

When WordPress can’t connect to the database server (MySQL), the reason can be the sudden increase in website traffic. Remember, this is the case only when everything else is up to the mark. The best way to deal with it is to contact the company that hosts your website to find out if the database server is working properly. You can also compare the other websites you have on the same server to check if those are working properly. 

In case you have one website only in the hosting account, these major steps you need to follow: 

  • Go to the main control panel
  • Get an access to phpMyAdmin as it helps to manage website database
  • Connect to database through phpMyAdmin
  • Now check if user can have an excess to the database. 
  • A new file is to be created testconnection.php and the code provided below is to be pasted

<?php
$link= mysqli_connect(‘localhost’, ‘username’, ‘password’);
If(!$link) 
{die(‘Could not connect: ‘. Mysqli_error());}
Echo’Connected successfully’;
Mysqli_close($link);?>

Verify Database Login Credentials

WordPress has a special file “wp-config.php” file where these following information is stored: 

  • Database name
  • Database username
  • Database password
  • And other WordPress settings 

You have to make sure that the details listed are correct along with the name of the database host from the WordPress hosting account that you have. There you will find a database section under which you’ll find MySQL database. Once you click on it, you will be taken to a page that manages the database on your hosting dashboard. There you can find all the relevant details. 

Now update the information and save the changes. Then try to open your website once again to check if the problem is solved or not. If the “Error Establishing A Database Connection” still exists, you have to look into it even further.  

Repair The Damaged Files

We have discussed above how pirated plugins and themes create an issue. Therefore, you are required to use another theme by first selecting the option appearance and then selecting Theme. 

If this issue remains unresolved, its time to turn off the plugins. Another way out can be renaming the plugin folder in the cPanel. In this way, if the error goes away, then for sure the plugin was the red flag giving you “Error Establishing A Database Connection”.

Here is how you can do that: 

  • Login to cPanel dashboard
  • Head to the file manager section
  • Click on public_html
  • Then select wp_content
  • When you find plugin folder, right click on it
  • Final step is to Rename it

The Renaming can be done to the themes as well. After you find the issue, give back plugins their original name. 
Image source: https://wedevs.com/blog/486795/fix-wordpress-error-establishing-database-connection/

When Everything Else Fails, Create A New Database

These are the steps that you need to follow: 

  • Head to the Databases section
  • Select MySQL Databases 
  • In Create New Database section, assign the desired name to your Database 
  • Click on Create Database button to begin the process
  • Once the new database is created, click on “Go Back” and you’ll land to the same page again.
  • Scroll down and you will find the section MySQL Users
  • Provide the required information such as the username and password that you wish to create.
  • Once the credentials are provided, generate the new user by clicking “Create User” and connect them to your database. 
  • Next step: scroll down and fill in the section “Add User To Database”

Then arrives the section below named “Modify Databases” provided with further two options on the right side named “Check databases” and “Repair Databases”. In your case you have to choose the later. 

Once this process ends, check your WordPress site to find out if the “Error Establishing A Database Connection” disappears or pops up all over again. 

Look For Clues In The Error Logs

Check the PHP error log to know what went wrong when the website’s code was running.

Check the MySQL error log to find out the issue in your database.

Take Help From Hosting Company’s Support Team

Even after applying all the techniques, the problem is still there, it’s high time to contact the hosting company’s support team. They will not only figure out what exactly went wrong but also will fix the problem of your server. It will help restore the database connection. 

Conclusion 

Conclusively, with such technical problems, it is crucial to understand where the problem is stemming from rather than hopping on to the solution first. The potential causes of “Error Establishing A Database Connection” are a sudden increase in traffic, wrong credentials, pirated plugins and themes. When you have identified the problem then move on to the solution accordingly. We have provided you with several techniques that can help you repair the damaged files, link the domain to the host, verify database credentials, create a new database when other ways don’t work, look for clues in the error logs, and take help from the Hosting company’s support team. 

TellerToday collects & utilizes cookies from third-parties & affiliate networks to improve user experience. If you buy a product or service after clicking on one of our links, we may get a commission.