WordPress Database Connection Error

How to Fix Error Establishing a Database Connection in WordPress

For a WordPress beginner, “Error Establishing a Database Connection” can be a nightmare. Numerous reasons can cause it. And sometimes, finding them and fixing the issue, takes too long. The frustration can rise so high that a newbie will remember this incident for rest of his life.

Now that I’ve explained the exact impact of this error, and hoping you’re not taking it as lightly, let’s have a look at how to solve it, and also, what causes it.

Tired of errors and security issues?
Give managed WordPress hosting a shot. Choosing a host like Flywheel saves you time, money, and frustration so you can do what you love: growing your blog.

What’s behind this error?

If you’ve manually installed WordPress then, you don’t need an answer to this question. But, I’m guessing it’s the second situation, i.e., installed WordPress via tools like Simple Scripts.

We don’t have to go through the details of the installation and thus, we’re not aware of what’s exactly going behind. Now, it’s time to learn few things.

A WordPress driven website is made up of a File Manager, which stores the front-end side of the data (files and folders), and a Database, which keeps the record (and the connection between files and folders) of most of the front-end data.

Both these ends should work together to maintain a WordPress driven website up and running. And, to make these two ends talking to each other and make them work in a harmony, a connection between them is established at the time of installation.

Now, if something happens to that ‘connection’ between the front-end and database, this error we’re talking about, occurs.

Now, front-end holds a particular file, which keeps the login and configuration details about the Database. If something is wrong in that file, a part of the configuration is incorrect, or the login credentials are wrong (or changes), this error occurs.

One more situation when this error can occur is when Database part becomes unresponsive. Or in the worst case, the Database end is corrupted due to some technical or coding related issue.

In real life situation, once WordPress is installed and if the webmaster never touches anything at the back-end or front-end, this error never (almost) occurs. But, even if one hasn’t done anything, the error can shoot up because of some mistakes at the Server level.

In most of the situations, server errors from the side of Hosting provider, causes the issue.

All you need to know to understand the roots of this error, and once you get it all, it’s time for us to move to our next phase, i.e., a solution to the problem.

How to Fix Error Establishing a Database Connection?

Depending on the cause, the fix can vary. You need to be aware of the actual cause before you can shoot up all your guns. Finding the cause is a crucial and tricky part, so, it’s always recommended to take help from the Support team behind the Hosting provider, in case you’re not aware of the coding part.

#1 Check Login Credentials

The wp-config file, stored on the front-end side (File Manager) holds the login credentials and configuration details regarding the Database. You need to check if anything is changed in that file and check if the login details are correct.

To test it, you can create a new file, name it anything and save it with ‘.php.’ extension. Now, open the file and paste the following code.

<?php

$test Connection = mysql_connect(‘localhost’, ‘root’, ‘password’);

if (!$testConnection) {

die(‘Error: ‘ . mysql_error());

}

echo ‘Database connection working!’;

mysql_close($testConnection);

?>

Save the file and then open the URL of that file in the browser. Once you hit enter, the code inside the file will be executed, bringing the accurate result.

If you find an error here, then the best solution is to change the username and password combination and reflect the same changes in the Database side too.

#2 Repair Database

Maximum times, the issue is at the front-end of the website, and the Dashboard area is still accessible. If that’s the case here, then you’ll get a message regarding repairing the corrupt database from the WordPress itself. Proceed with caution and if everything works as expected, the issue will be resolved.

In this situation, one or two tables in the Database becomes corrupt, causing the problem. If the native repair tool isn’t working, then you can also use third-party plugins or ask the Support team behind the Hosting provider to look into the tables and fix it.

If the Database is beyond saving and is corrupted completely, then you have to create a new Database and restore the backup files into the new one.

#3 Check the Server’s status

In most of the situations, a WordPress website starts on a Shared hosting plan as it receives about a hundred (or maximum, a thousand) of visits a day. But with time and efforts, the website becomes popular and starts getting hundreds of hits in a minute. The server finds an excuse to get unresponsive as it’s not meant to handle massive traffic.

There is a limitation up to which the server can manage the data traffic, and once it reaches the peak, errors start firing up.

So, if this is the case with your website, then you can’t do anything, except contacting the server provider. Communicate with the support team behind the Hosting Server provider and share the issue with them. If they are good as they promised, the issue will be resolved. Otherwise, it’s high time to switch to a new and better server.

Final words

As I explained earlier, most of the times, the error is caused because of the server. The moment a server becomes unresponsive, you can’t do anything to fix the issue, other than waiting for the Hosting provider to bring it up and running again. This is the exact reason why going with a known Hosting provider is highly recommended.

If your website gets tons of traffic, and you’re still sticking with the Shared hosting plan, then it’s high-time to shift onto a Managed WordPress Hosting plan (we recommend Flywheel – it’s easy, affordable, and saves a ton of time and frustration). Once you get up on a Managed or VPS plan, there will be no other site than yours on a single server, and the specifications of that particular server will be of a top-notch level to handle massive traffic.

Now, what’s your thought on this error? I’m pretty sure the fix has worked for you, and the website is up and running. What was the exact issue causing the error? Let’s discuss!

Editorial Staff
mail@85ideas.com

Editorial Staff at 85ideas is a team of WordPress experts led by Brian Harris. Here to share amazing tuts, guides and collections.

No Comments

Post A Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.