我想在MySQL中创建一个新用户,语法如下:

create user 'demo'@'localhost' identified by 'password';

但是它会返回一个错误:

您的密码不符合当前策略要求。

我试过很多密码,但都没用。我该如何解决这个问题?


当前回答


this information is important to all answers above : When you restart mysql server all policy config will be RESET and will be LOST, so to make your policy configuration PERSISTENT, you should follow these instructions: (tested on ubuntu server version 20.10)

用mysql应用这些变化后,洪阮回答 Sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf 加上最后一行:

[mysqld]
#
# * Basic Settings
#
..
..
..
validate_password.policy = LOW   # <== this line
..
..
# IMPORTANT notes: 
# - you can add more policy commands each by your needs.
# - notice the "." before "policy" , in some mysql versions is "_" , so be aware of that.

保存文件并重新启动mysql server: sudo service mysql restart 连接并检查配置的持久性: Sudo mysql -u your_username -p 显示像"validate_pass%"这样的变量; 结果应该是这样的:

+--------------------------------------+-------+
| Variable_name                        | Value |
+--------------------------------------+-------+
| validate_password.check_user_name    | ON    |
| validate_password.dictionary_file    |       |
| validate_password.length             | 6     |
| validate_password.mixed_case_count   | 1     |
| validate_password.number_count       | 0     |
| validate_password.policy             | LOW   |
| validate_password.special_char_count | 1     |
+--------------------------------------+-------+

我希望这能帮助到人们。

编辑:感谢@ahmednawazbutt:不要忘记FLUSH特权;

其他回答


this information is important to all answers above : When you restart mysql server all policy config will be RESET and will be LOST, so to make your policy configuration PERSISTENT, you should follow these instructions: (tested on ubuntu server version 20.10)

用mysql应用这些变化后,洪阮回答 Sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf 加上最后一行:

[mysqld]
#
# * Basic Settings
#
..
..
..
validate_password.policy = LOW   # <== this line
..
..
# IMPORTANT notes: 
# - you can add more policy commands each by your needs.
# - notice the "." before "policy" , in some mysql versions is "_" , so be aware of that.

保存文件并重新启动mysql server: sudo service mysql restart 连接并检查配置的持久性: Sudo mysql -u your_username -p 显示像"validate_pass%"这样的变量; 结果应该是这样的:

+--------------------------------------+-------+
| Variable_name                        | Value |
+--------------------------------------+-------+
| validate_password.check_user_name    | ON    |
| validate_password.dictionary_file    |       |
| validate_password.length             | 6     |
| validate_password.mixed_case_count   | 1     |
| validate_password.number_count       | 0     |
| validate_password.policy             | LOW   |
| validate_password.special_char_count | 1     |
+--------------------------------------+-------+

我希望这能帮助到人们。

编辑:感谢@ahmednawazbutt:不要忘记FLUSH特权;

MySQL 8*

SET GLOBAL validate_password.policy=LOW

参考链接解释政策-点击这里

步骤1:检查默认的身份验证插件

SHOW VARIABLES LIKE 'default_authentication_plugin';

步骤2:veryfing您的密码验证要求

SHOW VARIABLES LIKE 'validate_password%';

步骤3:使用正确的密码要求设置用户

CREATE USER '<your_user>'@'localhost' IDENTIFIED WITH '<your_default_auth_plugin>' BY 'password';

此错误消息与表中存储的密码无关。如果键入(在SQL控制台上),也会出现这种情况

“选择密码(“123456789”)”

或者,如果

“select密码(’A123456789’)”

或者,如果

“select a ! 123456789’密码()”

如果你输入

“选择密码(A a123456789 !)”

那么它就会起作用。只需使用大+小字母,特殊字符和数字创建您的密码。

您可以在my.cnf中禁用这些检查,但这样您就会有安全风险!

在[mysqld]中添加:

validate_password_policy=LOW
validate_password_special_char_count=0
validate_password_length=0
validate_password_mixed_case_count=0
validate_password_number_count=0

对于MySQL 8,你可以使用下面的脚本:

SET GLOBAL validate_password.LENGTH = 4;
SET GLOBAL validate_password.policy = 0;
SET GLOBAL validate_password.mixed_case_count = 0;
SET GLOBAL validate_password.number_count = 0;
SET GLOBAL validate_password.special_char_count = 0;
SET GLOBAL validate_password.check_user_name = 0;
ALTER USER 'user'@'localhost' IDENTIFIED BY 'pass';
FLUSH PRIVILEGES;