mysql忘记root密码

1. 查找密码

Mysql 5.7 在自动初始化数据库的时候,会生成root用户的默认密码。

通过 grep "temporary password" /var/log/mysqld.log 命令,返回结果最后冒号后面的字符串就是root的默认密码。

使用此密码登录后,Mysql 会要求第一件做的事就是改root密码,而且是要求强密码。可以通过set password=password('密码')来更改。

2. 无法找到密码

当然也有可能找不到。说明Mysql 初始化和启动的日志没有保存在 /var/log/mysqld.log 文件中,如果你不知道是哪个文件,那么还有一个有效的方法:

在/etc/my.cnf 中添加 skip-grant-tables 参数。此参数的作用是登录Mysql 数据库不进行用户密码验证。

[mysqld]
skip-grant-tables

保存/etc/my.cnf 后,重启msyql:

systemctl restart mysqld

然后 在console 窗口输入mysql 登录 。

执行以下语句:

  

mysql> update mysql.user set authentication_string=password('密码') where user='root';
mysql> flush privileges;
mysql> exit;
# 将/etc/my.cnf 中 skip-grant-tables 注释
# systemctl restart mysqld
# mysql -uroot -p
Enter password:

  在这里输入刚才设置的密码,就可以登录 啦。妥妥哒~


===================
天行健,君子以自强不息
地势坤,君子以厚德载物
===================
原文地址:https://www.cnblogs.com/halberd-lee/p/8534945.html