忘记了MySQL8密码解决办法

今天搞得有点晚,连MySQL进入办法都已经忘了。于是坑死自己了,下面把这个问题整理出来,防坑也做个记录,以后碰到类似的问题别再掉坑里面了。

首选网上有很多教程,因为内容有些大同小异,所以也极易出错。

首先,不要拿低版本的方法来解决问题。MySQL8相比以往的版本有较大的改变,修改user对应的表有所不同;

其次,存进去的东西是有加密的。你把对应帐户与密码改成十进制的数字,最后还是识别不出来,登陆不了 ;

第三,MySQL8在Mac OS里面安装目录有时会变化,系统配置文件有时候没有启动。单纯的MySQL命令在Terminal当中无法识别,sodu命令是要加的。

下面是过程:

1、进入目录:

cd /usr/local/mysql/bin/

2、取得权限:

sudo su

3、安全模式下打开MySQL

SH-3.2#./mysqld_safe –skip-grant-tables &

终端出现:

sh-3.2# 2020-06-20T16:47:34.6NZ mysqld_safe Logging to ‘/usr/local/mysql-8.0.19-macos10.15-x86_64/data/jiangdeMacBook-Pro.local.err’.

2020-06-20T16:47:34.6NZ mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql-8.0.19-macos10.15-x86_64/data

4、再进入数据库

./mysql

成功进入数据库显示:

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 7

Server version: 8.0.19 MySQL Community Server – GPL

Copyright (c) 2000, 2020, 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> 

5、这个时候可以在里面为所欲为,但要注意别高兴太早。密码是加密的,修改方法不同于其它低版本。

(1)先进入MySQL

mysql> use mysql

Database changed

(2)把原来加密的密码清空

mysql> update user set authentication_string=” where user=’root’;

Query OK, 0 rows affected (0.00 sec)

Rows matched: 1  Changed: 0  Warnings: 0

(3)刷新一下:

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

(4)真正设置密码:

mysql> alter user ‘root’@’localhost’ identified by ‘123456’;

Query OK, 0 rows affected (0.01 sec)

(5)刷新一下:

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

(6)退出来:

mysql> quit;

Bye

sh-3.2# exit;

exit

6、可以重新进一下:

jiangchen@jiangdeMacBook-Pro bin % sudo /usr/local/mysql/bin/mysql -u root -p

Enter password: 123456

Welcome to the MySQL monitor. ……

总结:最后,

mysql> select user,authentication_string,password_last_changed from user;

同样的密码改动后看到root帐户里面的密码是动态加密的。

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注