MySQL 忘记 root 密码了怎么办?

1、修改mysql配置文件,实现免密码登陆

在mysql配置文件中添加以下参数:
[mysqld]
skip-grant-tables

2、重启MySQL服务

service mysqld restart

3、免密登陆

mysql -uroot -p (直接点击回车,密码为空)

4、修改root密码

1.切换数据库
use mysql;
2.执行命令修改密码
update user set password=password('root') where user='root';
//5.7 以后版本执行上面命令报错可以尝试以下语句:
update user set authentication_string=password('123456') where user='root';
3.刷新权限
flush privileges;
//可能会出现的异常
ERROR 1820 (HY000): You must reset your password using ALTER USER statement;
//解决办法(执行以下命令):
alter user user() identified by "root";

5、还原配置文件,重启Mysql服务

[mysqld]
#skip-grant-tables
//重启服务
service mysqld restart

6、使用新密码登陆

mysql -uroot -p (回车输入密码)
发布了4 篇原创文章 · 获赞 6 · 访问量 1760

猜你喜欢

转载自blog.csdn.net/m0_46422300/article/details/104685630