1、修改配置文件my.cnf
vim /etc/my.cnf
在[mysqld]中添加 skip-grant-tables
保存退出。
2、重启mysql服务
service mysqld restart
3、用户登录
mysql -uroot -p (直接点击回车,密码为空)
use mysql;
update user set password=password('root') where user='root';
但是在5.7版本中不存在password字段,所有我们要用以下修改进行重置密码
update user set authentication_string=password('123456') where user='root';
执行
flush privileges;
quit;
4、将最开始修改的配置文件my.cnf中的skip-grant-tables删除
5、重启mysql
6、当你登陆mysql之后你会发现,当你执行命令时会出现
ERROR 1820 (HY000): You must reset your password using ALTER USER statement;
这是提示你需要修改密码
SET PASSWORD = PASSWORD('123456');
如果出现:ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
你需要执行两个参数来把mysql默认的密码强度的取消了才行
set global validate_password_policy=0; set global validate_password_mixed_case_count=2;
这时你再执行
SET PASSWORD = PASSWORD('123456');
完美解决。