1、停掉MySql服务
2、跳过密码授权登录
(1)MySql8.0版本
windows管理命令窗口输入:
mysqld --console --skip-grants-tables --shared-memory
3、重置密码
(1)打开一个新的管理员命令窗口输入:
mysql # 跳过密码认证,连接服务
user mysql
# 重置密码
update user set password=password('new password') where user='root' and host='localhost';
flush privileges; # 更新配置
quit
(2)停掉mysql服务,重启输入命令:
net start mysql
(3)输入新的密码登录
mysql -uroot -p '新密码'
4、其他方法,针对unix
修改配置文件 /etc/mysql/mysql.conf.d/
,在mysqld.cnf下添加skip-grant-tables
,
重启mysql服务sudo service mysql restart
5.7版本以下
# 将authentication_string 置空
update user set authentication_string='' where user='root';
# newpassword为要修改的新密码
alter user 'root'@'localhost' identified by 'newpassword';
# 提示错误,执行以下操作
flush privileges;
# 再执行
alter user 'root'@'localhost' identified by 'newpassword';
# 停掉服务,注释 skip-grant-tables,重启mysql
异常情况
1.版本5.7以下的修改密码语句有变动
update user set authentication_string=password('新密码') where user='root' and host='localhost';