这几天在遇到Mysql时遇到密码忘记问题和修改密码不成功,为此解决思路如下;
1.安装MySQL时的初始密码,忘记MySQL的初始密码时,可以查看MySQL的初始密码;
(1)可以查看到初始root登陆密码
[root@localhost ~]# tmp_pwd=`awk '/temporary password/ {print $NF}' /var/log/mysqld.log`
[root@localhost ~]# echo $tmp_pwd
a&ek+Md)z17p
(2)MySQL的登陆:
[root@localhost ~]# mysql -uroot -pa&ek+Md)z17p
(3)可以通过查看MySQL的初始密码和修改过的密码
[root@localhost ~]# grep password /var/log/mysqld.log
2022-03-26T15:42:40.045397Z 1 [Note] A temporary password is generated for root@localhost: a&ek+Md)z17p
2022-03-26T15:43:32.512355Z 2 [Note] Unknown database 'password'
2022-03-26T15:43:42.436435Z 3 [Note] Unknown database 'password'
2022-03-26T15:44:52.617375Z 5 [Note] Unknown database 'password'
2022-03-26T15:45:06.915842Z 6 [Note] Unknown database 'password'
2022-03-26T15:45:46.370385Z 7 [Note] Unknown database 'passwordMySQL@123'
2022-03-26T15:45:59.527413Z 8 [Note] Access denied for user 'root'@'localhost' (using password: YES)
2.修改密码时一直显示不成功,这时候应该查看参数;
(1)此处修改密码一直报错
[root@localhost ~]# mysqladmin -uroot -p'MySQL@123' password
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.
mysqladmin: unable to change password; error: 'Your password does not satisfy the current policy requirements'
(2)进入MySQL数据库修改密码也一直报错不成功时
[root@localhost ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 28
Server version: 5.7.37 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> ALTER USER root@localhost identified by '123456';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql> set password for root@localhost = password('mysql@123');
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
(3)查看MySQL的密码设置参数
mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+--------+
| Variable_name | Value |
+--------------------------------------+--------+
| validate_password_check_user_name | OFF |
| 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 |
+--------------------------------------+--------+
7 rows in set (0.23 sec)
(4)此时需要修改两条参数信息
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> set global validate_password_length=6;
Query OK, 0 rows affected (0.00 sec)
(5)再次查看数据库参数信息
mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------+-------+
| validate_password_check_user_name | OFF |
| validate_password_dictionary_file | |
| validate_password_length | 6 |
| validate_password_mixed_case_count | 1 |
| validate_password_number_count | 1 |
| validate_password_policy | LOW |
| validate_password_special_char_count | 1 |
+--------------------------------------+-------+
7 rows in set (0.00 sec)
(6)此时可以通过修改数据库的密码
mysql> alter user 'root'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.04 sec)
[root@localhost ~]# mysqladmin -uroot -p'123456' password
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.