How to Fix Error Establishing a Database Connection in WordPress

·

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 at least to working condition. The site was down for 20 minutes and again up running and again down.

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

Even the solutions on the internet were 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.

Disclosure: I may receive affiliate compensation for some of the links below at no cost to you if you decide to purchase a paid plan. You can read our affiliate disclosure in our privacy policy.

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 a problem connecting to its database.

But wait, that’s 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.

error

Generally, whenever a page is loaded on your website, it’s built on the fly means it doesn’t exist in practice 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.

The 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 the front end and back end is not the same as “Error establishing a database connection”,
  • website is down
  • A 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. [ Keep a backup before proceeding. Use BackUpBuddy ]

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 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.

define('WP_ALLOW_REPAIR', true);

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

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

wordpress-database-repair-wordpress-database-connection-error

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 log in. 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. These 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:

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'database_name_here' );
 
/** MySQL database username */
define( 'DB_USER', 'username_here' );
 
/** MySQL database password */
define( 'DB_PASSWORD', 'password_here' );
 
/** MySQL hostname */
define( 'DB_HOST', 'localhost' );

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 anyone of these values is incorrect, WordPress will not be able to connect to the database. If every detail of your database is correct, then you should make it fully sure by checking it with a script.

Sometimes MySQL server stops working if it gets a lot of traffic or especially if you are on cheap shared hosting, you can either ask the support team to make sure that your MySql server is working fine or just following these 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

<?php
$test Connection = mysql_connect('localhost', 'root', 'password');
if (!$testConnection) {
die('Error: ' . mysql_error());
}
echo 'Database connection working perfectly! and MySQL is responsing good';
mysql_close($testConnection);
?>

Make sure you replace the 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.

 error-establishing-database-connection

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 list all the information.

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

UPDATE wp_options SET option_value = 'http://YOURURL.com' 
WHERE option_name='siteurl'

What worked for Me?

This happened to me when I updated my WordPress, and after the 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 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 realized 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 work 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 flipping up and down.

And I keep receiving the Site monitor Up & Down email notification. Moreover, the outdated plugin affected my MySQL.so file, and HTTP.so by modifying them. Since I don’t have access to them I contacted the support team to restore them. and after all, that done.

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 minutes.

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 the 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 a comment on how you got into this problem and which solutions worked for you.

Similar Posts

Leave a Reply

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