在 Ubuntu 18 安装了 MySQL8 一段时间后,忘记密码了,记录一下怎么重置~
编辑配置文件:
1 | sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf |
添加一行:skip-grant-tables
添加后大约如下:
1 | # http://dev.mysql.com/doc/mysql/en/server-system-variables.html |
停止 MySQL 服务、重启 MySQL 服务、查看 MySQL 服务:
1 | systemctl stop mysql |
进入 mysql 后
刷新权限:1
mysql> flush privileges;
切换数据库:1
mysql> use mysql
清空密码:1
mysql> update user set authentication_string='' where user='root';
查看 root 对应 host 的值:1
2
3
4
5
6
7
8
9
10mysql> select user,host from user;
+------------------+-----------+
| user | host |
+------------------+-----------+
| root | % |
| mysql.infoschema | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
+------------------+-----------+
4 rows in set (0.00 sec)
重置密码:1
mysql> ALTER USER 'root'@'%' IDENTIFIED BY '123456';
如果搞错 host 值,那么会报,如:
1 | ERROR 1396 (HY000): Operation ALTER USER failed for 'root'@'localhost' |
去掉 /etc/mysql/mysql.conf.d/mysqld.cnf
中的 skip-grant-tables
重启 MySQL 服务