MySQL Error: : ‘Access denied for user ‘root’@’localhost’

To fix MySQL Error: : ‘Access denied for user ‘root’@’localhost’, you should follow these steps:

Step 1: Open and Edit /etc/my.cnf or /etc/mysql/my.cnf, according to your distro.

Step 2: Add skip-grant-tables which is under [mysqld]

Step 3: Now, Restart Mysql

You must be able to login to mysql now by using the below command mysql -u root -p

Run mysql> flush privileges;

Now, set new password by ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘NewPassword’; and go back to /etc/my.cnf and remove/comment skip-grant-tables

Again, Restart Mysql, now you can login with the new password 

mysql -u root -p