MySQL5.7 使用者設定密碼解決方法

Xneat發表於2024-08-20

1:執行修改使用者的密碼時,一直報錯:即修改密碼命令:

mysql> alter user 'root'@'localhost' identified by '123456';
報錯如下:

mysql> alter user 'root'@'localhost' identified by '123456';
ERROR 1396 (HY000): Operation ALTER USER failed for 'root'@'localhost'

2:百度查詢說是設定:MySQL的密碼策略,即命令如下:

set global validate_password_policy=LOW;
set global validate_password_length=6;

mysql> set global validate_password_length=6;
ERROR 1193 (HY000): Unknown system variable 'validate_password_length'

分別報錯如下,意思是不知道哪個欄位:
mysql> set global validate_password_policy=LOW;
ERROR 1193 (HY000): Unknown system variable 'validate_password_policy'

3:然後查詢了 MySQL密碼初始設定策略,發現欄位不對:

mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+--------+
| Variable_name                        | Value  |
+--------------------------------------+--------+
| validate_password.check_user_name    | ON     |
| 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.02 sec)

然後有分別重新設定了一下:

備註:validate_password.policy=LOW 設定為LOW值意思是:只驗證密碼的長度。及長度為6(global validate_password.length=6; )

mysql> set global validate_password.policy=LOW;
Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password.length=6;
Query OK, 0 rows affected (0.00 sec)

4:執行以下命令:如果沒有報錯。那就ok了。如果報如圖錯誤,請繼續往下看;

alter user 'root'@'localhost' identified by '123456';
ERROR 1396 (HY000): Operation ALTER USER failed for 'root'@'localhost'

報上圖錯誤有可能是你執行了:以下的命令(第三條)

mysql> use mysql;

mysql> select host,user,authentication_string,plugin from user;

mysql> update user set host='%' where user='root';

mysql> flush privileges;

因此,你需要換執行命令,就OK了:

mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.01 sec)

第3步擴充套件:

(1):validate_password.dictionary_file 指定密碼驗證的檔案路徑;
(2):validate_password.length 密碼固定的長度;
(3):validate_password.mixed_case_count 密碼中至少包含大小寫字母的個數;
(4):validate_password.number_count 密碼中至少包含的數字個數;
(5):validate_password.special_char_count 密碼中至少包含特殊字元的個數;
(6):validate_password.policy 密碼強度驗證,預設為 MEDIUM;

關於 validate_password.policy 的取值:
0/LOW:只驗證長度;
1/MEDIUM:驗證長度、數字、大小寫、特殊字元;
2/STRONG:驗證長度、數字、大小寫、特殊字元、字典檔案;

相關文章