8.0以上的版本
1.使用管理员权限打开命令提示符,在命令行中输入: net stop mysql
2.待mysql服务停止后,输入: mysqld --shared-memory --skip-grant-tables ,此时命令提示符窗口处于锁定状态,我们重新以管理员权限打开新的命令提示符窗口。
3.在新的命令提示符窗口输入: mysql -uroot -p 后回车,提示输入密码时直接按回车进入。
4.输入: use mysql;
5.输入: update user set authentication_string='' where user='root'; ,将authentication_string置空。
注:在mysql8.0以上版本,
update mysql.user set password='newpassword' where user='root';
和
update mysql.user set password=PASSWORD('newpassword') where User='root'; 这两条命令已经不起作用了。
6.输入: alter user 'root'@'localhost' identified by 'newpassword'; newpassword是要设的新密码。
我的操作到这一步出现error;
后来先执行:
flush privileges;
再执行:
alter user 'root'@'localhost' identified by 'newpassword';
则执行成功。
再从SQLyog用新密码登录就可以了