MySQL - Fixing access denied for ‘root’@’localhost’

So you’ve locked yourself out of your root MySQL account and MySQL won’t let you in? No problem by following these simple instructions you’re guaranteed to restore access to your root MySQL account: Edit the /etc/my.cnf file Under [mysqld] add skip-grant-tables Restart your MySQL server You should be able to login to mysql now using the below command mysql -u root -p Run flush privileges; inside the MySQL shell Set new password with ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘your_new_password’; Edit /etc/my.cnf and remove the skip-grant-tables line Restart MySQL again Now you can login with your new MySQL password using mysql -u root -p Fixing access denied for ‘user’@’ip_address’ Sometimes you may want to add a remote user to MySQL, you try to connect remotely but your MySQL server returns something like access denied for ‘user’@’192.168.0.10’. The most probable cause for this is that you didn’t grant the host or ip address of the user permission to use the server. You can do that by typing in the following commands in the MySQL shell: grant all privileges on *.test_db to 'user'@'192.168.0.10'; flush privileges; In the example above we gave “all privileges” to the user with ip address 192.168.0.10 on the database test_db. If you do not want to restrict the user to one ip address in MySQL you can use the percentage symbol to specify to the MySQL server that the user can log in from any ip address or host: grant all privileges on *.test_db to 'user'@'%'; flush privileges; Now your user has rights to access the test_db database using any ip address or host. Fixing access denied for ‘user’@’%’ with correct password and privileges This time you have the right permissions and the right password but MySQL still won’t let you in. If you’re trying to login from localhost then this problem arises because you have an anonymous user that matches ‘user’@’%’. The remedy to this problem is to drop the anonymous user and we can do that by issuing either of the following two commands: drop user ''@'localhost'; drop user ''@'127.0.0.1'; You should be able to login successfully after executing either of the two commands above.