Failed to connect to MySQL: Access denied for user

While accessing the website, I was getting the error as -
Failed to connect to MySQL: Access denied for user 'database_user'@'xxx.xx.xxx.xx' to database 'database_name'

Generally the above error comes on the website due to user not linked to the database and due to the lack of privilages assisgned to the user .

Solution - 

1. Login to user on server :  #cd ~username

2. Search the database displayed in the error on the server : #grep -rl "database_name"
You will get the configuration file for that database.

3. Check the configuration file of the database {it will contain the login details for that database} -

#cat configuration_file.php

Output:

<?php
$myhost = "host's_ip_address";
$myuser = "database_username";
$mypass = "xxxxxxxx";
$mydb = "database_name";
$key = "your_key"; //Don't tuch this !
$con = mysqli_connect($myhost, $myuser, $mypass, $mydb);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>

4.You can try to access the database which will confirm that the database is present or not -
#mysql -u username(present in configuration file) -p then press Enter
password: (Enter the password present in the configuration file)

You will enter the "mysql>" window as below -

mysql>

5.To check the database present for that user enter : mysql>show databases;
So as per the output there is no database present for that user due to privilages not assigned.

Output:

+--------------------+
| Database |
+--------------------+
| information_schema |
+--------------------+
1 row in set (0.00 sec)


6. Now login to cPanel >> MySql Databases >> click on Add (Change the privileges to all ) >> Save

7. Now check the database on the server by show databases command as used earlier
mysql> show databases;

Now the database is linked to the user as we have assigned all the privileges to the user -


Output:

+--------------------+
| Database |
+--------------------+
| information_schema |
| database_username |
+--------------------+
2 rows in set (0.00 sec)

8. Try accessing the website, it will start working fine without any errors.

  • 1 Users Found This Useful
Was this answer helpful?

Related Articles

How to reset SSH port back to default port 22?

In some situations most of us may come across a problem that we are able to log into the WHM but...

How to setup the root login alert notification email?

Its very important to keep a check on your servers and who logs into the server, From security...

How to enable Open SSL on WHM?

Before enabling open SSL, you can check whether it is already enabled on your server. Please fire...

How to install mod_pagespeed on WHM?

'mod_pagespeed' is module of Apache which is nothing but plugin which is used for compressing the...

How to enable Hot Link Protection in cPanel?

To avoid other sites to use your website's bandwidth, you can enable the hot link protection....

Powered by WHMCompleteSolution