1.修改mysql的配置文件
vi /etc/my.cnf
在[mysqld]下添加
skip-grant-tables #跳过授权表
2.重启MySQL服务
service mysql restart
mysql -uroor -p 按两次回车进入
mysql>use mysql;
mysql>update user set authentication_string='' where user='root'; #将密码置空
mysql>exit
3.再次编辑mysql配置文件将添加的
#skip-grant-tables #注释掉
4.再次重启MySQL服务
service mysql restart
mysql -uroot -p 即可进行免密登录
mysql>use mysql;
mysql>alter user 'root'@'localhost' identified by 'root';
mysql>flush privileges;
至此密码修改成功。
ps:mysql5.7版本root忘记密码
执行
update user set authentication_string=password('root') where user='root';