Mac - MySQL初始密码忘记重置MySQL root密码

Posted 公羽寒

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mac - MySQL初始密码忘记重置MySQL root密码相关的知识,希望对你有一定的参考价值。

 

在什么情况下,需要重置root密码呢?那就是我们忘记了。还有一种比较坑的,那就是笔者的这种情况。按照正常的情况下,mysql安装完之后,会弹出一个对话框,显示着一个临时的root密码,但无论笔者如何重装MySQL,始终不给我一个临时的root的密码,导致笔者一直无法登陆MySQL,没办法,只能用硬的,那就是重置密码。

 

停止MySQL的服务,打开系统的偏好设置,找到MySQL 进去后,点击Stop MySQL Server即可。

step1:
苹果->系统偏好设置->最下边点mysql 在弹出页面中 关闭mysql服务(点击stop mysql server)
step2:
进入终端输入:cd /usr/local/mysql/bin/
回车后 登录管理员权限 sudo su
回车后输入以下命令来禁止mysql验证功能 ./mysqld_safe --skip-grant-tables &
回车后mysql会自动重启(偏好设置中mysql的状态会变成running)

 

 

开启两个终端,在第一个终端输入sudo /usr/local/mysql/bin/mysqld_safe --skip-grant-tables,输入当前用户的密码:

ERROR 1045 (28000): Access denied for user \'root\'@\'localhost\' (using password: NO)

那么就换下面这种:

macdeMacBook-Pro:~ mac$ mysql -uroot -h127.0.0.1 -p
Enter password: 
ERROR 1045 (28000): Access denied for user \'root\'@\'localhost\' (using password: YES)

 

然后在第二个终端输入sudo /usr/local/mysql/bin/mysql -u root,然后输入当前用户的密码后,出现以下的界面

macdeMacBook-Pro:~ mac$ sudo /usr/local/mysql/bin/mysql -u root
Password:
Welcome to the MySQL monitor.  Commands end with ; or \\g.
Your MySQL connection id is 3
Server version: 5.7.22 Homebrew

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type \'help;\' or \'\\h\' for help. Type \'\\c\' to clear the current input statement.

mysql> 

 

然后输入命令UPDATE mysql.user SET authentication_string=PASSWORD(\'新密码\') WHERE User=\'root\';回车,出现以下的界面,说明修改成功。
mysql> UPDATE mysql.user SET authentication_string=PASSWORD(\'123456\') WHERE User=\'root\';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1

 
接下来输入FLUSH PRIVILEGES;回车,出现下面的界面
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)

 
最后,输入\\q,退出。关闭第一个终端,回到系统的偏好设置,重新开启MySQL即可。
mysql> \\q
Bye

 

 

macdeMacBook-Pro:~ mac$ mysql -uroot -h127.0.0.1 -p
Enter password: 
ERROR 1045 (28000): Access denied for user \'root\'@\'localhost\' (using password: YES)
macdeMacBook-Pro:~ mac$ sudo /usr/local/mysql/bin/mysql -u root
Password:
Welcome to the MySQL monitor.  Commands end with ; or \\g.
Your MySQL connection id is 3
Server version: 5.7.22 Homebrew

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type \'help;\' or \'\\h\' for help. Type \'\\c\' to clear the current input statement.

mysql> UPDATE mysql.user SET authentication_string=PASSWORD(\'123456\') WHERE User=\'root\';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)

mysql> \\q
Bye
macdeMacBook-Pro:~ mac$ 

 

 

以上是关于Mac - MySQL初始密码忘记重置MySQL root密码的主要内容,如果未能解决你的问题,请参考以下文章

MySQL忘记密码后重置密码(Mac )

Mac下MySQL密码忘记后重置密码

mysql忘记密码重置(mac)

Mac下忘记Mysql root密码重置

Mac brew安装MySQL8.0.18后忘记密码(重置密码篇)

Mac MySQL重置Root密码