MYSQL5.7忘记root密码

时间:2022-07-04 16:16:13

mysql5.7版本

1、关闭mysql(注:关闭有多种方式,此处是已经注册为服务的情况);

[root@iZ8vbjfnjgynrnv1qfpuw1Z /]# service mysql stop

2、启动mysql服务的时候跳过权限认证表;

[root@iZ8vbjfnjgynrnv1qfpuw1Z /]# mysqld_safe --skip-grant-tables &

3、进入mysql数据库;

[root@iZ8vbjfnjgynrnv1qfpuw1Z /]# mysql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.18-log Source distribution

Copyright (c) 2000, 2017, 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> use mysql;
Database changed
mysql> update user set password=password('root') where usr='root';
ERROR 1054 (42S22): Unknown column 'usr' in 'where clause'
mysql> update mysql.user set authentication_string=password('123456') where user='root' and Host ='localhost';
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> exit;
Bye
[root@iZ8vbjfnjgynrnv1qfpuw1Z /]# service mysql start
Starting MySQL SUCCESS!

上面的主要操作,先是进入mysql。enter后提示输入密码,此处不用管直接enter进入,然后选择mysql数据库,再执行更新密码语句,上面第一句update更新报错是因为sql写错了,更新后就执行flush privileges刷新权限,最后退出,再重启mysql,到此,大功告成!