技术博文 · 2021-05-27 0

忘记root密码怎么解决

忘记root密码怎么办

很多时候我们需要接触的系统太多了,root密码可能会因为没有记录,或者忘记了。这时候怎么处理呢,下面几个情况的root密码忘记了可以这样做。

1、centos 7.9系统的root登录密码忘了

①开机启动,在显示器显示如下画面时按e。进入单用户模式

image-20210415095220859

②进入单用户模式之后,通过键盘的上下左右将光标定位到ro这个位置

image-20210415095619277

③将ro改成rw init=/sysroot/bin/sh,然后按ctrl+x 重新启动

image-20210415095826149

④系统进入如下界面后 输入

:/#chroot /sysroot
:/#passwd root
Changing password for user root
New password:
BAD PASSSWORD:The password contains the user name in som from
Retype new password:
passwd: all authentication token update successful
:/#

⑤设置的密码最好大于8位,包含大小写数字,不然会被提示弱密码。密码修改成功后重启,然后就可以使用设置的root密码进入系统了(再记住了,输入秘密按的时候慢一点。不然开机又忘记了)

⑥如果系统启用了selinux,必须运行以下命令,否则将无法正常启动系统:

:/#touch /.autorelabel
:/#exec /sbin/reboot         #重启

2、mysql 5.7 root密码忘记了

①修改mysql启动时加载的cnf文件,在[mysql]模块添加一行:skip-grant-tables=1

image-20210415102421008

[root@jumpserver ~]# vim /etc/my.cnf

skip-grant-tables    ###一般添加在文末就可以

这一行配置的目的是让 mysqld 启动时不对密码进行验证

②重启mysql服务

[root@jumpserver ~]# systemctl restart mysqld.service 

③使用root进入mysql

[root@jumpserver ~]# mysql -uroot -p
Enter password:                             ######直接回车就可以了
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.33-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> 

④切换到mysql数据库,修改user表,重置mysql的root密码

mysql> update mysql.user set authentication_string=password('XXXXXXX') where user='root';
Query OK, 0 rows affected, 1 warning (0.02 sec)
Rows matched: 1  Changed: 0  Warnings: 1

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

在之前的版本中,密码字段的字段名是 password,5.7版本改为了 authentication_string

如果有密码复杂度要求的话有如下2中模式可以做修改(不建议)

Ⅰ修改配置文件

[mysqld]
validate_password=off

Ⅱ可以修改validate_password插件的值

mysql> SHOW VARIABLES LIKE 'vali%';
+--------------------------------------+--------+
| Variable_name                        | Value  |
+--------------------------------------+--------+
| validate_password_dictionary_file    |        |
| validate_password_length             | 8      |
| validate_password_mixed_case_count   | 1      |
| validate_password_number_count       | 1      |
| validate_password_policy             | MEDIUM |
| validate_password_special_char_count | 1      |
+--------------------------------------+--------+
6 rows in set (0.00 sec)
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
 validate_password_length 8 # 密码的最小长度,此处为8。
 validate_password_mixed_case_count 1 # 至少要包含小写或大写字母的个数,此处为1。
 validate_password_number_count 1 # 至少要包含的数字的个数,此处为1。
 validate_password_policy MEDIUM # 强度等级,其中其值可设置为0、1、2。分别对应:
 【0/LOW】:只检查长度。
 【1/MEDIUM】:在0等级的基础上多检查数字、大小写、特殊字符。
 【2/STRONG】:在1等级的基础上多检查特殊字符字典文件,此处为1。
 validate_password_special_char_count 1 # 至少要包含的个数字符的个数,此处为1。

⑤把password_expired 改成不过期

mysql> update user set password_expired='N' where user='root';
mysql> flush privileges;
mysql> quit

⑥把/etc/my.cnf得skip-grant-tables这行注释掉,重启mysql服务

[root@jumpserver ~]# systemctl restart mysqld.service

⑦使用刚才设置的密码登陆mysql就可以了