MySQL忘记root密码解决方案

发布时间 2023-09-04 14:19:38作者: xiaoyaozhe

Linux系统-部署-运维系列导航

 

场景一:重置root密码

mysql登录密码为password()算法加密,解密成本太高,以下为通用方案;
原理:mysql提供了特殊启动方式,即跳过权限表验证,启动后,登录不需要提供密码;
登录后,即可修改mysql数据库的user表,重置密码,然后刷新权限,重启mysql服务即可;
注意:此时mysql服务将面临高风险,请在合适时间执行;
 
#停止正在运行的mysql服务
service mysqld stop

 

#以--skip-grant-tables选项启动服务,跳过权限表验证,有2种方式
方式1:指定运行选项,只在本次启动生效
./bin/mysqld_safe --skip-grant-tables --user=root &
如果本机没有mysqld_safe服务,运行mysqld效果相同

 

方式2:修改配置文件,使用service、systemctl启动均生效
修改配置文件my.cnf,添加 skip-grant-tables
my.cnf可能存在多个,请使用  sudo mysql --help | grep my.cnf   或   mysql --help | grep 'Default options' -A 1 确认加载顺序

 

#root账号登录mysql,此时不需要提供密码
mysql -uroot

#切换到mysql数据库,登录账号与权限在此数据库中
use mysql;

#查看mysql版本
select version();

#查看当前账户信息,根据mysql版本执行
#5.7+版本密码为authentication_string(生效),password;
#5.7-版本密码为password 
#user=用户名,host=登录IP,即允许该账户登录的IP地址,每个IP一条user表记录,%表示任意IP
select user,host,authentication_string,password from user where user='root';

#5.7+设置密码
update user set authentication_string=password('password') where user='root';
--and Host='localhost';

#5.7-设置密码
update mysql.user set password=password('password') where user='root';
--host='localhost';

#5.7+支持2个密码字段,直接设置2个,生效为authentication_string
update user set authentication_string=password('password'),password=password('password') where user='root' ;
--and Host='localhost';

#刷新权限表
flush privileges;

#退出mysql连接
quit;

#重启mysql服务
service mysqld restart

 

场景二:增加账号与授权

以上--skip-grant-tables模式不验证权限,同时无法增加账号授权,所以增加账号的登录IP,需要以正常模式启动登录
#密码登录重启后的mysql服务
mysql -u root -p
 
#切换mysql数据库,账号和权限在此数据库
use mysql;
 
#增加账号授权
grant all privileges on *.* to "root"@"ip" identified by "password" with grant option;
 
#刷新权限表
flush privileges;
 
#退出mysql连接
quit;
 
#无需重启服务

 

场景三:修改登录密码

1> 更新mysql.user表,需要登录MySQL执行,需要刷新权限列表生效
mysql> use mysql; #5.7前后版本密码字段不一致,且 user 表同时存在2个字段
# mysql5.7之前
mysql> update user set password=password('123456') where user='root' and host='localhost';
# mysql5.7之后
mysql> update user set authentication_string=password('123456') where user='root' and host='localhost';
mysql> flush privileges; #刷新权限列表

 

2> 用set password命令,需要登录MySQL执行,自动刷新权限列表
语法:set password for '用户名'@'域'=password(‘密码’)
mysql> set password for 'root'@'localhost'=password('123456');

 

3> alter user命令,需要登录MySQL执行,自动刷新权限列表
语法:ALTER USER '用户名'@'域' IDENTIFIED BY 'xxxx'; 
#初始化时root账号只有localhost
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
 
4> grant命令,需要登录MySQL执行,自动刷新权限列表
语法:GRANT 权限列表(逗号分隔) ON 数据库.数据表 TO '用户'@'域' IDENTIFIED BY '密码';
#grant语句自动创建用户以及设置密码
#权限支持 create、update、select、lete、drop、execute等,也可以指定 all privileges 授权所有权限
#grant语句最后可以指定 WITH GRANT OPTION 指定用户可以将权限传递授权给其他用户。
#数据库与数据表支持 * 指定全部,如 testdb.* 或 *.*,其他情况只能一条授权一个数据表
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.11.31' IDENTIFIED BY 'Mysql.pass.123' WITH GRANT OPTION;

 

5> mysqladmin,无需登录MySQL执行,自动刷新权限列表
语法:mysqladmin -u用户名 -p旧的密码 password 新密码
#该方式为明文传输密码,不安全
[root@localhost ~]# mysqladmin -uroot -p123456 password 1234abcd
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
New password: 
Confirm new password: 
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.