Sunday 12 April 2015

Simple fix for mysql " Access denied for user 'root'@'localhost' " issue

There are numerous instances where this issue can be faced. In my case, I faced this while installing mysql in my RHEL machine . I installed rpm as root user, and after installation , I faced this issue while logging into mysql .
ERROR 1045(28000) : Access denied for user 'root@localhost' (using password: no )

Even
 mysql -u root -p ***
produced the same error.

This issue can be fixed in a very quick and easy solution .

If you have started mysql , stop it.
Now restart your server with  --skip-grant-tables option
eg : in my case  I used the command
sudo /etc/init.d/mysql start --skip-grant-tables

Now you dont need a username password combination to open mysql.
simply, use mysql to login to mysql prompt. 
 shell> mysql

now load the grant tables :

mysql> FLUSH PRIVILEGES;

mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('HelloWorld');

This command will help you set your new password.

Dont forget to restart your mysql server with the new user-name and password combination, as opening mysql with --skip-grant-tables is insecure.

hope this helps you guys out :) Good luck.