Error Establishing a Database connection
While you are working with WordPress, you may have seen this error establishing a database connection at least once.It could be a MySQL database connection issue .It could be wrong login credentials and need to repair your database and MYSQL server is unresponsive.
How to fix Error Establishing a Database Connection in WordPress ?,based on any one of the following cause,you need to solve this problem.
MYSQL Database User Has Limited Privileges
Repair Database for WP-admin
The only wp-congfig.php file is located in your c panel's file manager section.Before that you need to allow to repair your database ,it can be easily done by adding the following code to your wp-config.php file.
define('WP_ALLOW_REPAIR', true);
After adding the code,just enter this url path in the browser to repair database.
http://www.yourwebsitename.com/wp-admin/maint/repair.php
Here you need not to be logged in to repair database.WordPress automatically fix this error establishing a database connection.If problem fixed,then remove the above code from your wp-config.php file.
If problem not solved jump to next possible issue of mysql reset password.
Wrong MYSQL database Login Credentials
In most of the cases you can see the error establishing a database connection in WordPress due to wrong mysql database login details.You should reset mysql password in order to get back your website. Open your wp-config.php by using cpanel login credentials.Under file manager you can see a file name called wp-config.php,which stores the your wordpress database the details of database name, database user, database password and database host.Now reset your password in wp-config.php file.
define('DB_NAME', 'database-name');
define('DB_USER', 'database-username');
define('DB_PASSWORD', 'database-password');
define('DB_HOST', 'localhost');
This problem often due to changing the root password or database user password.You need to change the same in your wp-config.php file.
Sometimes your BD_HOST valud always may not be localhost.It's totally depends on your webhosting company.You can see this localhost value in Hostgator and Bluehost.
Make sure all values in wp-ocnfig.php file are correct.If database repaired this problem solved.If not follow to next issue might be MYSQL server side.
MYSQL Server is Unresponsive
Error establishing a database connection also happens due to high traffic to your website with shared hosting plan.Will you MYSQL server responsive ?,in most of the cases if your website gets high traffic,then your mysql server becomes unresponsive due to shared web hosting.
To fix this issue,contact your hosting company or do it your self with following instructions.
1.Log on to cpanel with credentials
2.Open phpMyAdmin and connect to database
3.Create a new file and name it testconnection.php. Add the following php code and replace the username and password.
4.If problem not solved contatc your web host support.
<?php
$link = mysql_connect('localhost', 'root', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
MYSQL Database User Has Limited Privileges
In rare cases,MySQL database user privileges can become corrupted or limited.Then you need to delete current user and connect your database with new user.To fix this issue,open your wp-config.php,than replace old database username with new username.