WordPress Error Establishing a Database Connection

Oh ! Stuck with WordPress Error Establishing a Database Connection  ?  If you are familiar with web for sometime then you may have seen this problem before too. Its frustrating when this happens without changing anything in your website. I have faced this types of issue many times in my clients website, so now when I get this error I would say my client ” Keep calm for 30 minutes ” and work to solve the issue. We can easily Fix the Error Establishing a Database Connection in WordPress. First  create a backup of WordPress , especially Database Itself. 

Why you get WordPress Error Establishing a Database Connection  ?

The problem Error Establishing a Database Connection in WordPress may be different within site to site, I have seen one main problem is shared hosting. If your website have good number of visitors and your Web hosting is shared Hosting then it could be the main problem for high traffic website, so you should think of changing your  Host to Optimized WordPress Hosting, You can check the link for Suggested Hosting.  If this is hosting issue then your website should run time to time while you keep reloading.

Other reason could be your database credentials like username or password is changed.  Remember if you have made any changes to your WordPress or your Database.  Sometimes this may happen when your database is corrupt or not responding.

 

Solve : Error Establishing a Database Connection in WordPress

Lets get started by checking your WordPress Admin , go to yourwebsite.com/wp-admin and check if you see same error. Sometimes you may see “One or more database tables are unavailable” This means there is some error within your database and this needs to be repaired. Login to your C panel and go to Wp-config.php and add the code below before ‘That’s all, stop editing! Happy blogging’ line.

define('WP_ALLOW_REPAIR', true); 

After adding this code to your wp-config.php file go to yourwebsite.com/wp-admin/maint/repair.php

Choose one of the option as Repair Database wont take more time to complete while optimizing will take long time.  If both options wont work for you, you are having more trouble so lets continue to read post.

 

This worked ? Also Read Best WordPress Backup Plugins

Checking Database Info in Wp-Config.php

Your all import database and WordPress connection is handled by wp-config.php file, this is where you can check and change if there is any error within database.

 define('DB_NAME', 'database-name');
define('DB_USER', 'database-username');
define('DB_PASSWORD', 'database-password');  
define('DB_HOST', 'localhost');

As seen on some forums , people have also solved this issue by replacing local host to IP , For example on MAMP, the DB_Host value when changed to the IP will also work. As Ip varies from host to host. like this :

 define('DB_HOST', '127.0.0.1:8889'); 

If still this is not working , then there must be something error with your server.

Working with your hosting.

As I have discussed previously also, this might be due to high load on your server, I would recommend to stop generating traffic for some time so you can see if site works fine with low traffic. If you want to test if MySQL server is running , you can check the other website running on same server. Try if its due to high traffic. Lets solve by checking if database have enough permission. Create a new php file (testconnection.php) and add following code to file.

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

Remember to replace username and password with username and password of you own database. If it connects then it means your database have enough permission.  If you cannot connect using MySQL then this is due to your server.  Please again, check if everything is correct in wp-config.php file. Sometime this happens due to user permission reset.  so if you get access deniened while you tried connecting to PhpMySQL or test connection.pho then contact your host as soon as possible.

Hope this solves your error, please comment or mail me at riwajmail@gmail.com or call me for quick support at 977 9849136140 ( Call charge may apply as per your carrier ) I could help you as possible. Please share how you did in comment below. I will keep updating this documents.

 

 

Shares 0

Leave a Reply

Related Posts

Wordpress

How to change WordPress Language ?

Do you ever wonder how to change language of WordPress ? I was working on my clients website who was from Germany. I saw it was Germany language and it was not easy changing WordPress Language Read more…

Wordpress

Schedule your social Media post from WordPress

Have you ever imagined scheduling your social Media posts using WordPress ? It has now been possible to schedule post to social medias from WordPress. Recently WordPress.com has introduced this feature where you can Schedule your Read more…

Themes

20+ ecommerce WordPress themes using Woocommerce

THE SHOP The Shop is one of the easy to use full featured e-commerce theme which is compatible with the popular Woo Commerce plugin. The themes comes with custom front page, fonts and color options, Read more…