1, 修改mysql配置文件/etc/my.cnf
1
2
[mysqld]
skip_grant_tables
2, 重启mysql后直接用root用户登录(不用输入密码)
$ mysql -uroot
3, 进入mysql数据库,更新密码
1
2
3
4
5
6
7
8
mysql > use mysql
mysql > update user set password=password('123') where user='root'
mysql5.7执行时会报错,ERROR 1054 (42S22): Unknown column 'password' in 'field list'
使用下面sql进行更新
mysql > update mysql.user set authentication_string=password('123456') where user='root';
mysql > flush privileges;
4, 恢复配置文件/etc/my.cnf,重启mysql服务,用新密码登录
$ mysql -uroot -p123