DEV Community

Jennie
Jennie

Posted on

How to solve ERROR 1045 (28000): Access denied for user 'root'@'localhost' (MAC)

I have not been using MySQL on my laptop for a long time. Today I have to start it, but I forgot the password. And I searched for it on the Internet, I tried a lot of ways to solve it, but I failed.

And then I found an effective solution.

I guess you tried this command before:



mysql -u root -p 


Enter fullscreen mode Exit fullscreen mode

Next, you tried to remember your password. Unfortunately, it shows like this:



Enter password: ***
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)


Enter fullscreen mode Exit fullscreen mode

Then we are going to solve it step by step.

  1. Stop MySQL Preferences -> Search 'MySQL' in the top right corner (or find it on the panel) -> Click 'Stop MySQL Server'

Image description

2.Start MySQL Safe Mode
2.1 cd bin directory



cd /usr/local/mysql/bin


Enter fullscreen mode Exit fullscreen mode

2.2 get permission



sudo su


Enter fullscreen mode Exit fullscreen mode

2.3 enter the password of your laptop



Password:***


Enter fullscreen mode Exit fullscreen mode

2.4 enter this command after sh-3.2#



./mysqld_safe --skip-grant-tables &


Enter fullscreen mode Exit fullscreen mode

ATTENTION: START A NEW TERMINAL NOW

  1. Reset password 3.1 execute the command to skip the verification step ```

mysql -u -root

3.2 set the password to null
Enter fullscreen mode Exit fullscreen mode

mysql> update user set authentication_string='' where user='root';

3.3 refresh permissions 
Enter fullscreen mode Exit fullscreen mode

flush privileges;

3.4 
Enter fullscreen mode Exit fullscreen mode

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'youPassword';"

3.5 exit and login MySQl again
Enter fullscreen mode Exit fullscreen mode

exit

Enter fullscreen mode Exit fullscreen mode

mysql -u root -p


I hope it is helpful for you.






Enter fullscreen mode Exit fullscreen mode

Top comments (0)