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
Enter fullscreen mode Exit fullscreen mode

3.2 set the password to null

mysql> update user set authentication_string='' where user='root';
Enter fullscreen mode Exit fullscreen mode

3.3 refresh permissions

flush privileges;
Enter fullscreen mode Exit fullscreen mode

3.4

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'youPassword';" 
Enter fullscreen mode Exit fullscreen mode

3.5 exit and login MySQl again

exit
Enter fullscreen mode Exit fullscreen mode
mysql -u root -p 
Enter fullscreen mode Exit fullscreen mode

I hope it is helpful for you.

Top comments (0)