How to Fix WordPress Error Establishing a Database Connection

It’s one of the worst fears of a website owner, The longer your website is down, the more traffic you’ll lose. And that could mean lost subscribers, lost customers, and ultimately lost revenue.

Yesterday we ran into this error, and it took me more than 3 hours to get the site atleast to the working condition. The site was down for 20 minutes and again up running and again down.

It was something that never happened with us before but when I googled it, I didn’t find the perfect answer for my solution.

Even the solutions on the internet was not enough to solve my problem, although the article on wpbeginner was very helpful for solving the problem ” Error Establishing a Database Connection“.

But what exactly does that error even mean? And more importantly, how can you fix it and get your website back to normal as soon as possible?

In this article, we’ll go over exactly what the error message means and what could be causing it, and then I’ll walk you through some steps to troubleshooting and fixing the problem.

 

Why do you get this error? What Causes This Error?

Well the error message seems simple, and by that you can understand that your website is facing problem  connecting to its database.

But wait, thats not that simple..

This kind of error vary’s to different factors, it can be due to

  • invalid login credentials,
  • change in port,
  • file permission,
  • corrupt database,
  • changes in wp-config and
  • .htaccess.

 

What Does “Error Establishing a Database Connection” Mean?

First, let’s take a look at how WordPress works to display your website, and it’ll become clear why this error is so devastating.

Generally, whenever a page is loaded on your website, it’s built on-the-fly means it doesn’t exist in practically but it is created dynamically. PHP code is used to access your MySQL database and retrieve every bit of information needed to create the page. There’s a PHP query to access the post title, another to grab the author name, another to retrieve the publication date, and so on.

There’s a PHP query to access the post title, another to grab the author name, another to retrieve the publication date, and everything else that you see on the website.

 

Troubleshooting “Error Establishing a Database Connection”, Where does this problem occur.

First thing you need to check is that you are getting the same error on both the front-end (www.YourSite.com) of the site, and the back-end of the site (www.YourSite.com/wp-admin).

These are signs of database corruption.

  • If the error message is the same on both front end and back end is not same as “Error establishing a database connection”,
  • website is down
  • Database table is missing

then your database needs to be repaired.

You don’t need to worry about your database since WordPress has a built-in way to attempt to repair your database, but you’ll have to enable this feature first. [ ]

To do so, you’ll need to access your wp-config.php file, which contains your WordPress installation settings and configuration.

The wp-config.php in the your root WordPress file directory in your hosting. You can easily access it by logging into your cPanel, selecting File Manager, and navigating to the folder where you installed WordPress.
Once you have wp-config open, Add the below code just before ‘That’s all, stop editing! Happy blogging’ line wp-config.php.


Once you have done that, you can see the settings by visiting this page:

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

bigefpfhnfcobdlfbedofhhaibnlghod

You should see the above screen with two options to repair, or repair and optimize, your database. Feel free to choose either; just note that optimizing will take longer.

Please note that this database repair page is not secure; anyone can access that URL without having to login. Once you’re done repairing your database, be sure to remove the line of code you added to wp-config. This will disable access to the repair page and prevent anyone else from messing with your database.

 

Check your database login settings in wp-config

If the above step didn’t work or didn’t apply to you, then the next thing you can do is take a look at your database settings in your wp-config file.

This is where you specify the details for WordPress to connect your database. This details can be changed or altered by some plugins or by changing your hosting or editing your files.

Go ahead and open wp-config that for editing.
You’ll see the login credentials for your database, probably near the top of the file. It should look something like this:


As you can see, there are four pieces of information WordPress needs to access your database:

  1. The name of the database (“DB_NAME”)
  2. The login username (“DB_USER”)
  3. The login password (“DB_PASSWORD”)
  4. The database host (“DB_HOST”)

If any one of these values is incorrect, WordPress will not be able to connect to the database. If every details of your database is correct, then you should make it fully sure by checking it with a script.

Sometimes MySQl server stops working is it gets a lot of traffic or specially if you are on a cheap shared hosting, you can either ask the support team to make sure that your MySql server is working fine or just following this few steps.

Access your cPanel, File Manager and the root of your website where your wordpress is installed. Create a new file and name it ” mysql-check.php “and paste this below code


Make sure you replace username and password in the above codes. If you cannot connect to the database through phpMyAdmin, then it is possible that you have problems with your server and your MySQl is working fine.

 wordpress downloading index.php

Didn’t solved yet, then try this?

If all of them didn’t work for you then you should try this.

In your Database go to the table wp_options, then click browse, this will lists all the informations.

Search site URL .Click the edit button(looks like a pencil) and re input your URL into the Value text area and click save.

Alternatively in the SQL Console add the following line


 

What worked for Me?

This happened to me when I updated my wordpress, and after successful update I crashed into this error. I did all the above steps but was out of luck. I even contacted my hosting support but they even couldn’t find the error.

So I downloaded all my backups and created my site on a localhost, and to be surprised everything was working fine so now i know something was wrong with my server.

So I started my research with MySQL and phpMyAdmin. I attached my custom php application and it worked well. So now everything was clear.

The only problem is with my wordpress files, I deactivated all the files and themes, deleted all the cache but still it didn’t solve my problem.

I checked my .htaccess it was good, but when i clicked on the Change Permission button. I realised it was a small problem that consumed my all time and peace of mind.

Why the file permission changed?

When I updated my wordpress, some plugins didn’t worked well with that update and changed the file permission, why it happened, I don’t know. Also some outdated plugins were consuming my entire resources, for which my site was fliping up and down.

I restored my permissions and also the themes, plugins and caches..And Boom it was back again. SmartActiveBlogger was live again.

 

Conclusion

There is no certain factor why it happens and also there is no definite answer to rectify it. you have to go through all the solutions and check which works. If you are lucky then you can solve it within 5 minutes and if you are a bit out of luck as I was, you may take some more minuted.

But not as much time as I took since I have to do the research and give you the answer so you don’t face problem as i had.

Also keep an eye on error_log on your root of your wordpress. It can help you solve your problem if you have any server errors

Please write on comment on how you got into this problem and which solutions worked for you.

Author: EDITORIAL STAFF

Editorial Staff at SmartActiveBlogger is a team of WordPress experts led by Niraj Kashyap. Page maintained by Niraj Kashyap.

1 thought on “How to Fix WordPress Error Establishing a Database Connection”

Comments are closed.