Fix the Error Establishing a Database Connection in WordPress Error

Are you facing a problem with your WordPress like Establishing a Database Connection? Don’t worry. It’s not just you who face this issue, It’s quite common these days. If you are WordPress or any other CMS user, you may be familiar with this issue. If not, let me tell you How you can fix Error Establishing a Database Connection Issue in WordPress and get your Site back. There is a lot of cause for this issue. Go through the methods mentioned below and let’s get rid of Error Establishing a Database Connection issue that you are facing on your WordPress Blog.

fix error establishing database connection

Let me share My story. When I was just getting started with my brand new blog earlier, I faced this issue. Everything was working fine and suddenly when I visited my site BOOM! It was only showing Error Establishing a Database Connection, nothing beyond it. I was so scared and initially, I thought I lost my blog forever. But, that all was just my thoughts. We can easily fix Fix Error Establishing a Database Connection issue. The problem is with your Database. So misconfiguration or a recent edit in the Database file may be made it corrupted Etc, Etc. The reason may vary with the situation. So, let’s check how? You can also Add an SSL Certificate to your Blog for Free as well.

Let’s Fix Error Establishing a Database Connection Error in WordPress

Let me show you the screenshot of the Error in the below given picture. This is how an Error Screen will appear when you try to visit your Website:

Error-Establishing-a-Database-Connection-Error

So, make sure that you are getting an Error like one above.

First of all, visit your site both from the Front End and Back End. And see if the site is accessible through the Backend or you may see an error like One or more database tables are unavailable. If you see an error like one above. Then, it may be a problem with the Database file. So, let’s try to fix Fix Error Establishing a Database Connection Error.

So, let’s repair our Database file.

Copy the following line mentioned below and paste it into the wp-config.php file. You need to paste this code just above the That’s all, stop editing! Happy blogging’ line in the wp-config.php of your blog.

 

[php]Pdefine(‘WP_ALLOW_REPAIR’, true);[/php]

Once you have done the required editing in the wp-config.php file of your blog, Visit your blog in the link pattern like : www.example.com/wp-admin/maint/repair.php [Replace example.com with your WordPress Site Domain URL]

Repair WordPress Database

You are not required to be logged into your WordPress admin panel to access the URL. This error can happen even when you are logged out and cannot access Admin Area or something.

Check, whether your issue has solved or not. Once, if the problem has solved, Right away remove the edited part from the wp-config.php file. If the problem still persists, then continue further.

Have a Look at your Blog’s wp-config.php File

There may be some problems with your Wp-config.php file even.  Maybe WordPress not able to access your Database. The wpconfig.php file is one that directs WordPress to reach your Database. If you have recently changed your Root Password or the Database Password, then the error might occur.  If this happens you will need to make changes to the wp-config.php file. Open the and check if the values are the same as you mentioned. The wpconfig.php file is the one that has every WordPress installation details.

While you are editing wpconfig.php or any other important files, make sure you don’t mess up with anything. A single fault can crack your site completely.

[php]define(‘DB_NAME’, ‘database-name’);
define(‘DB_USER’, ‘database-username’);
define(‘DB_PASSWORD’, ‘database-password’);
define(<strong>’DB_HOST'</strong>, ‘localhost’);
[/php]

Check and make sure that all Username and Password are the exact correct. The DB_HOST value will change according to your Hosting Company.

Check whether this method helped you or not.

Changing DB_HOST Value

Most of the hosting will be having localhost as the default DB_HOST Value. Changing it into URL or the IP Address as per your host may solve the issue.

You can find the list of DB_HOST values for some Hosting companies in the Official WordPress Codex Article.

Check Database Username and Password

So, in this method, we are going to create a new PHP file and then check it by visiting the PHP file. This method is again a simple one, So, let’s try it out.

First of all, Go to your Hosting Dashboard and Open up the File Manager.

Click on the New File option. You can name it anything you want like wpcheck.php or anything you wish

Now, a Blank Code Editor window will open up.

Paste the below-given Code in it with required changes.

[php]

<?php
$link = mysql_connect(‘localhost’, ‘root’, ‘password’);
if (!$link) {
die(‘Could not connect: ‘ . mysql_error());
}
echo ‘Connected successfully’;
mysql_close($link);
?>

[/php]

 

Editings to make replace, localhost, Username, and Password with your Database host, Username and Password.

Once, you did required changes in the file, Save it.

Once you saved the file, You wanted to visit this URL: yoursite.com/wpcheck.php

Replace yourite.com with your Domain name and wpcheck.php with the name you have given to the file we created earlier.

If you get any error like Could not connect or Some Error due to Username and Password, then surely the problem is with the Database host, Username or Password, That’s exactly the reason why we were not able to connect.

To Solve this issue, all you wanted to do is, Simply create a new Database User and update the same details in the wp-cofig.php file of your blog.

Create New Database User with Password

So, let’s see how to create a Database new user account.

  • Go to your Hosting Dashboard and go to Database section.
  • Click on MySQL Database
  • Add a User
  • Enter a Username and Password and Create new Database profile

(New Database Profile should be created with “All Previlages ” option)

Now, we have created a fresh new database, Next is to add this to our WordPress wp-config.php file.

Once, you have linked your new database with your Wp-config.php file, try to access your website and see if it is working.

Even after trying all these, If you are not able to access your Website then Go to your Host’s Customer Support and raise a ticket.

Final Words

So, Guys, these are some quick troubleshooting methods to you can try. Don’t worry if any methods didn’t work for you. You can always contact your Hosting Support for help. Always your Hosting Company support team will help you to fix your almost all problems. Still, there are many Hosting Companies without good support. If your Hosting Companie’s Support is good enough, then they surely will help you to fix Error Establishing a Database Connection Error issue.

In the above methods, I have mentioned you to edit some core Files of your Website. You should handle these files with extreme care. A single mistake can completely break your site. So, you should be very careful while editing these kinda files. You can also take a complete backup of your current site and then do a fresh installation of WordPress and then restore the backup. Recently, When I got this error, I Simply Deleted the WordPress installation and then Did a fresh new installation and that worked. (But, not sure in your case.)

Comment down below, if you need any further assistance in this or any other blogging related issues. We will catch you at the soonest. Also, share which method worked for you.

About the author

Astro Joseph

Hey Guys, This is Astro from IPEE World. I am a Computer and Smartphone Tech Addicted Blogger, I write about various technology topics including Tricks and Hacks, Windows, iOS, Android, Tips and Tricks and a lot more. Aims to keep you updated about the latest Buzz including Tips & Tricks from the Tech Industry.

Leave a Comment

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