How to fix Error Establishing a Database Connection in WordPress
What’ll be your reaction if you open your website and a simple 4 words message greets you saying Error Establishing a Database Connection? Well, I don’t want that anyone face such situation but it’s a reality that the worst fear of a webmaster is to encounter such error on the site. Although, once in the online journey, you all have faced some errors like 404 Error, 503 Internal Server Error, White Screen of Death and Error Establishing a Database Connection etc. Well, this becomes frustrating when we are doing everything alright and suddenly an error comes up saying Error Establishing a Database Connection. Me too have faced the error some days ago and searched the solution of it but don’t get what I was looking for. Well, after analyzing the possible causes, I put up some points important to fix and understanding the error. Though, there may be many reasons of having this error. But, it’s not so much difficult to get rid of such situation. Here you can find the ultimate solution to fix error establishing a database connection in WordPress.
What is Error Establishing Database Connection?
Well, to understand it, firstly you have to understand that what is WordPress? Actually, WordPress is a CMS designed especially for blogging. However, it is made up of PHP and MySQL. Well, you all have some idea of these both terms, but let me clear that all your website data is stored on MySQL database and PHP is used to retrieve necessary information as per request. Thus, a perfect collaboration between MySQL and PHP is necessary to work upon WordPress.
If any user requests the web to show a web page, then PHP fetches the content of that page from the database and simply shows you the requested page. For instance, if you enter Blogtacker.com, then PHP simply fetches the content stored in the database and shows the homepage of the site. Now if, for any reason, PHP is not able to connect with MySQL, then it simply shows a blank screen with a message that Error Establishing a Database Connection. This means PHP is no longer able to retrieve information from the database.
What causes Error Establishing a Database Connection?
As, I said above that there may be many reasons to cause such error. Well, some of the main reasons that cause the dreadful situation are compiled here-
- Wrong Login Details to access database
- May be your database is corrupted
- Server that hosts your database may be down
Well, before going to troubleshoot the error, I recommend you to have a backup of your site, as, I don’t want that after fixing this error, you put in another problem. Umm, just kidding but for safety, it is needed to take backup regularly. If you are stuck in this situation before having a backup, then need not worry, you can use your cPanel or contact your web hosting provider.
Wrong Login Credentials
One of the most common causes of this error is Incorrect Login Details for your database. Well, to fix this cause, you have to access the wp-config.php file of your site. This is the important file required you to connect with your database. You can get it from any FTP client or cPanel>File Manager. It is located in the root folder of your WordPress directory. Well, thereafter you have to confirm that every login credentials are correct or not. As, it may be wrong Database Name, User Name or Password etc. Always keep in mind that if anyone of them is entered wrong, you’ll not be able to connect with your database.
Please note that DB_Host need not set as “localhost” every time. As, it depends on hosting providers, as, for some hosts its value is ‘localhost’ but for others it may be 127.0.0.1
Database is corrupted
Well, this could be a rare reason that your MySQL Database has been corrupted. Although, it can be corrupted by many reasons that a bad plugin or theme can corrupt it, but it’s quite rare. However, still if it has corrupted then probably you’ll not be able to access your WordPress dashboard. Well, WordPress is a robust platform that has inbuilt feature to repair the database. To enable this, you need to enter the following line in the wp-config.php file.
Well, this line enables you to repair your database. The only thing you have to do is go to this page after setting values to your wp-config.php file.
Replace yourwebsite.com to your actual site name.
Once, you have repaired your database, don’t forget to remove the code added to wp-config.php Because you don’t let any user to access database repair page, as, it’s not secure so anyone can access that page if you haven’t remove the added line.
Server that hosts your database may be down
If above-discussed methods didn’t work for you, then probably your host server is down. It may be down due to any reason from web hosting company or if you have received more traffic than your server can handle. Well, if it happens you should immediately make a phone call to your hosting company. They’ll fix the problem at their end or guide you to resolve the error.
One more solution that may help you to get rid of Error Establishing a Database Connection-
Well, it is very rare when a webmaster forgets that when will his hosting expire? So, if any person forgets to renew the web hosting on time, then, he may also encounter such type of error.
Well, these were the possible causes and solutions. However, it is not necessary that these all work for you. So, it is highly recommended to keep a backup of your website.