1. 以超级管理员打开cmd,关闭mysql服务
net stop mysql
2. 跳过权限验证登录mysql
mysqld --shared-memory --skip-grant-tables
3. 在新的窗口中登录mysql
mysql -u root -p
无需输入密码,直接回车。
4. 将密码置空。
UPDATE mysql.user SET authentication_string='' WHERE user='root' and host='localhost';
UPDATE mysql.user
:指定要更新的表为mysql.user
,这是 MySQL 系统库中的用户表。SET authentication_string=''
:将authentication_string
列的值设置为空字符串''
。WHERE user='root' and host='localhost'
:这是一个筛选条件,只更新满足user='root'
(用户名为root
)和host='localhost'
(主机为本地主机)的行。
5.刷新权限
flush privileges;
6. 设置加密规则并更新新密码,授权
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456' PASSWORD EXPIRE NEVER;
alter user 'root'@'localhost' identified by '123456';
grant all privileges on *.* to "root"@'localhost';
flush privileges;
7. 设置成功后,重启mysql服务,使用新密码登录
net start mysql