8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

正文为中文,英文原文默认隐藏。点击任意中文段落即可展开 / 收起对应英文原文。

打开官方英文原文 ↗

译文由机器翻译生成,仅供理解参考;有疑问请以官方英文原文为准。

中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

8.4.3 密码验证组件8.4.3 The Password Validation Component

validate_password 组件用于通过要求账户密码并启用潜在密码的强度测试来提高安全性。该组件提供系统变量,使您能够配置密码策略,以及用于组件监控的状态变量。The validate_password component serves to improve security by requiring account passwords and enabling strength testing of potential passwords. This component exposes system variables that enable you to configure password policy, and status variables for component monitoring.

validate_password 组件实现以下功能:The validate_password component implements these capabilities:

  • 对于以明文值分配密码的 SQL 语句,validate_password 会根据当前的密码策略检查密码,并在密码过弱时拒绝该密码(该语句将返回 ER_NOT_VALID_PASSWORD 错误)。这适用于 ALTER USER、CREATE USER 和 SET PASSWORD 语句。For SQL statements that assign a password supplied as a cleartext value, validate_password checks the password against the current password policy and rejects the password if it is weak (the statement returns an ER_NOT_VALID_PASSWORD error). This applies to the ALTER USER, CREATE USER, and SET PASSWORD statements.

  • 对于 CREATE USER 语句,validate_password 要求必须提供密码,并且该密码必须符合密码策略。即使帐户最初被锁定,也同样适用,否则以后解锁帐户时将导致可以访问而无需符合策略的密码。For CREATE USER statements, validate_password requires that a password be given, and that it satisfies the password policy. This is true even if an account is locked initially because otherwise unlocking the account later would cause it to become accessible without a password that satisfies the policy.

  • validate_password 实现了一个 VALIDATE_PASSWORD_STRENGTH() SQL 函数,用于评估潜在密码的强度。该函数接受一个密码参数并返回一个从 0(弱)到 100(强)的整数。validate_password implements a VALIDATE_PASSWORD_STRENGTH() SQL function that assesses the strength of potential passwords. This function takes a password argument and returns an integer from 0 (weak) to 100 (strong).

Note

对于分配或修改账户密码的语句(ALTER USER、CREATE USER 和 SET PASSWORD),此处描述的 validate_password 功能仅适用于使用在 MySQL 内部存储凭据的认证插件的账户。对于使用针对 MySQL 外部凭据系统进行认证的插件的账户,密码管理也必须针对该系统在外部进行。有关内部凭据存储的更多信息,请参见 第 8.2.15 节,“密码管理”。For statements that assign or modify account passwords (ALTER USER, CREATE USER, and SET PASSWORD), the validate_password capabilities described here apply only to accounts that use an authentication plugin that stores credentials internally to MySQL. For accounts that use plugins that perform authentication against a credentials system external to MySQL, password management must be handled externally against that system as well. For more information about internal credentials storage, see Section 8.2.15, “Password Management”.

前述限制不适用于使用 VALIDATE_PASSWORD_STRENGTH() 函数,因为它不会直接影响账户。The preceding restriction does not apply to use of the VALIDATE_PASSWORD_STRENGTH() function because it does not affect accounts directly.

示例:Examples:

  • validate_password 在以下语句中检查明文密码。在默认密码策略下,该策略要求密码至少为 8 个字符,该密码较弱,因此语句会产生错误:validate_password checks the cleartext password in the following statement. Under the default password policy, which requires passwords to be at least 8 characters long, the password is weak and the statement produces an error:

    mysql> ALTER USER USER() IDENTIFIED BY 'abc';
    ERROR 1819 (HY000): Your password does not satisfy the current
    policy requirements
    
  • 作为哈希值指定的密码不会被检查,因为原始密码值不可用于检查:Passwords specified as hashed values are not checked because the original password value is not available for checking:

    mysql> ALTER USER 'jeffrey'@'localhost'
           IDENTIFIED WITH mysql_native_password
           AS '*0D3CED9BEC10A777AEC23CCC353A8C08A633045E';
    Query OK, 0 rows affected (0.01 sec)
    
  • 即使账户最初被锁定,该账户创建语句也会失败,因为它不包含满足当前密码策略的密码:This account-creation statement fails, even though the account is locked initially, because it does not include a password that satisfies the current password policy:

    mysql> CREATE USER 'juanita'@'localhost' ACCOUNT LOCK;
    ERROR 1819 (HY000): Your password does not satisfy the current
    policy requirements
    
  • 要检查密码,请使用 VALIDATE_PASSWORD_STRENGTH() 函数:To check a password, use the VALIDATE_PASSWORD_STRENGTH() function:

    mysql> SELECT VALIDATE_PASSWORD_STRENGTH('weak');
    +------------------------------------+
    | VALIDATE_PASSWORD_STRENGTH('weak') |
    +------------------------------------+
    |                                 25 |
    +------------------------------------+
    mysql> SELECT VALIDATE_PASSWORD_STRENGTH('lessweak$_@123');
    +----------------------------------------------+
    | VALIDATE_PASSWORD_STRENGTH('lessweak$_@123') |
    +----------------------------------------------+
    |                                           50 |
    +----------------------------------------------+
    mysql> SELECT VALIDATE_PASSWORD_STRENGTH('N0Tweak$_@123!');
    +----------------------------------------------+
    | VALIDATE_PASSWORD_STRENGTH('N0Tweak$_@123!') |
    +----------------------------------------------+
    |                                          100 |
    +----------------------------------------------+
    

要配置密码检查,请修改名称形式为 validate_password.xxx 的系统变量;这些是控制密码策略的参数。请参阅 第 8.4.3.2 节,“密码验证选项和变量”。To configure password checking, modify the system variables having names of the form validate_password.xxx; these are the parameters that control password policy. See Section 8.4.3.2, “Password Validation Options and Variables”.

如果未安装validate_password,则validate_password.xxx 系统变量不可用,语句中的密码不会被检查,并且VALIDATE_PASSWORD_STRENGTH() 函数总是返回 0。例如,如果没有安装该插件,可以为账户设置少于 8 个字符的密码,或者根本不设置密码。If validate_password is not installed, the validate_password.xxx system variables are not available, passwords in statements are not checked, and the VALIDATE_PASSWORD_STRENGTH() function always returns 0. For example, without the plugin installed, accounts can be assigned passwords shorter than 8 characters, or no password at all.

假设已经安装了validate_password,它实现了三个级别的密码检查:LOW、MEDIUM和STRONG。默认值是MEDIUM;要更改此设置,请修改validate_password.policy的值。各政策实现了逐渐严格的密码测试。以下描述涉及默认参数值,可以通过更改相应的系统变量来修改。Assuming that validate_password is installed, it implements three levels of password checking: LOW, MEDIUM, and STRONG. The default is MEDIUM; to change this, modify the value of validate_password.policy. The policies implement increasingly strict password tests. The following descriptions refer to default parameter values, which can be modified by changing the appropriate system variables.

此外,validate_password 支持拒绝与当前会话有效用户账户的用户名部分匹配的密码,无论是正向还是反向。为了对该功能进行控制,validate_password 提供了一个 validate_password.check_user_name 系统变量,该变量默认启用。In addition, validate_password supports the capability of rejecting passwords that match the user name part of the effective user account for the current session, either forward or in reverse. To provide control over this capability, validate_password exposes a validate_password.check_user_name system variable, which is enabled by default.