0%

mysql忘记root密码

Windows下的mysql忘记密码

mysqld --console --skip-grant-tables --shared-memory
再启动一个cmd 执行:

1
2
3
use mysql;

UPDATE user SET authentication_string ='' WHERE User = 'root';

退出重启mysql后执行

1
2
mysql -uroot -p回车
alter user 'root'@'localhost' identified by 'password'

Linux下

在mysql配置文件my.cnf内的[mysqld]字段下加skip-grant-tables
重启服务后登录数据库
mysql8 用

1
2
3
use mysql;
ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'yourpasswd';
UPDATE user SET authentication_string ='' WHERE User = 'root';

退出mysql, 删除/etc/my.cnf文件最后的 skip-grant-tables 重启mysql服务;
mysql -u root -p无需输入密码即可登入
ALTER user 'root'@'localhost' IDENTIFIED BY 'Qian123#'
远程连接:

1
2
3
4
5
6
use mysql;
select user,host from user;
update user set host = '%' where user = 'root';
flush privileges;
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
flush privileges;