MySQL 支持以下密码管理功能:MySQL supports these password-management capabilities:
密码过期,要求定期更改密码。Password expiration, to require passwords to be changed periodically.
密码重用限制,防止再次选择旧密码。Password reuse restrictions, to prevent old passwords from being chosen again.
密码验证,要求在更改密码时也指定要替换的当前密码。Password verification, to require that password changes also specify the current password to be replaced.
双重密码,使客户端可以使用主密码或次密码连接。Dual passwords, to enable clients to connect using either a primary or secondary password.
密码强度评估,要求使用强密码。Password strength assessment, to require strong passwords.
随机密码生成,作为不要求显式管理员指定明文密码的替代方案。Random password generation, as an alternative to requiring explicit administrator-specified literal passwords.
密码失败跟踪,在连续多次密码错误登录失败后启用临时账户锁定。Password failure tracking, to enable temporary account locking after too many consecutive incorrect-password login failures.
以下各节描述了这些功能,但密码强度评估除外,该功能是通过 validate_password 组件实现的,并在 第 8.4.3 节,“密码验证组件” 中进行了说明。The following sections describe these capabilities, except
password strength assessment, which is implemented using the
validate_password component and is described in
Section 8.4.3, “The Password Validation Component”.
MySQL 使用 mysql 系统数据库中的表来实现密码管理功能。如果您将 MySQL 从早期版本升级,系统表可能不是最新的。在这种情况下,服务器在启动过程中将向错误日志写入类似于以下的消息(具体数字可能有所不同):MySQL implements password-management capabilities using tables
in the mysql system database. If you upgrade
MySQL from an earlier version, your system tables might not be
up to date. In that case, the server writes messages similar to
these to the error log during the startup process (the exact
numbers may vary):
[ERROR] Column count of mysql.user is wrong. Expected 49, found 47. The table is probably corrupted [Warning] ACL table mysql.password_history missing. Some operations may fail.
要解决此问题,请执行 MySQL 升级程序。参见 第 3 章,升级 MySQL。在完成此操作之前,无法更改密码。To correct the issue, perform the MySQL upgrade procedure. See Chapter 3, Upgrading MySQL. Until this is done, password changes are not possible.
某些身份验证插件将账户凭据存储在 MySQL 内部的 mysql.user 系统表中:Some authentication plugins store account credentials internally
to MySQL, in the mysql.user system table:
caching_sha2_passwordmysql_native_password(已弃用)mysql_native_password(deprecated)sha256_password(已弃用)sha256_password(deprecated)
本节中的大部分讨论适用于此类认证插件,因为这里描述的大多数密码管理功能都是基于 MySQL 自身处理的内部凭据存储。其他认证插件则将账户凭据存储在 MySQL 外部。对于使用外部凭据系统进行认证的插件账户,密码管理也必须在该系统外部进行。Most discussion in this section applies to such authentication plugins because most password-management capabilities described here are based on internal credentials storage handled by MySQL itself. Other authentication plugins store account credentials externally to MySQL. For accounts that use plugins that perform authentication against an external credentials system, password management must be handled externally against that system as well.
例外情况是,针对失败登录跟踪和临时账户锁定的选项适用于所有账户,而不仅仅是使用内部凭据存储的账户,因为无论账户使用内部还是外部凭据存储,MySQL 都能够评估任何账户的登录尝试状态。The exception is that the options for failed-login tracking and temporary account locking apply to all accounts, not just accounts that use internal credentials storage, because MySQL is able to assess the status of login attempts for any account no matter whether it uses internal or external credentials storage.
有关各个身份验证插件的信息,请参见 第 8.4.1 节,“身份验证插件”。For information about individual authentication plugins, see Section 8.4.1, “Authentication Plugins”.
MySQL 允许数据库管理员手动让账户密码过期,并建立自动密码过期的策略。过期策略可以全局设定,单个账户可以选择遵循全局策略,或通过特定的每账户行为覆盖全局策略。MySQL enables database administrators to expire account passwords manually, and to establish a policy for automatic password expiration. Expiration policy can be established globally, and individual accounts can be set to either defer to the global policy or override the global policy with specific per-account behavior.
要手动使账户密码过期,请使用 ALTER USER 语句:To expire an account password manually, use the
ALTER USER statement:
ALTER USER 'jeffrey'@'localhost' PASSWORD EXPIRE;
此操作会在 mysql.user 系统表中的相应行标记密码为已过期。This operation marks the password expired in the corresponding
row in the mysql.user system table.
根据策略的密码过期是自动进行的,并且基于密码的年龄,对于某个账户来说,其年龄是从最近一次密码更改的日期和时间开始计算的。mysql.user 系统表指示每个账户密码的最后更改时间,如果密码的年龄超过其允许的使用期,服务器会在客户端连接时自动将密码视为已过期。这在不需显式手动过期密码的情况下也能生效。Password expiration according to policy is automatic and is
based on password age, which for a given account is assessed
from the date and time of its most recent password change. The
mysql.user system table indicates for each
account when its password was last changed, and the server
automatically treats the password as expired at client
connection time if its age is greater than its permitted
lifetime. This works with no explicit manual password
expiration.
要在全局范围内建立自动密码过期策略,请使用 default_password_lifetime 系统变量。其默认值为 0,这会禁用自动密码过期。如果 default_password_lifetime 的值为正整数 N,则表示允许的密码使用期限,即密码必须每 N 天更改一次。To establish automatic password-expiration policy globally, use
the default_password_lifetime
system variable. Its default value is 0, which disables
automatic password expiration. If the value of
default_password_lifetime is a
positive integer N, it indicates the
permitted password lifetime, such that passwords must be changed
every N days.
示例:Examples:
要建立一个密码有效期约为六个月的全局策略,请在服务器的
my.cnf文件中使用以下行启动服务器:To establish a global policy that passwords have a lifetime of approximately six months, start the server with these lines in a servermy.cnffile:[mysqld] default_password_lifetime=180
要建立一个全球策略,使密码永不过期,请将
default_password_lifetime设置为 0:To establish a global policy such that passwords never expire, setdefault_password_lifetimeto 0:[mysqld] default_password_lifetime=0
default_password_lifetime也可以在运行时设置并持久化:default_password_lifetimecan also be set and persisted at runtime:SET PERSIST default_password_lifetime = 180; SET PERSIST default_password_lifetime = 0;
SET PERSIST为正在运行的 MySQL 实例设置一个值。它还会保存该值,以便在后续服务器重启时继续使用;参见 第 15.7.6.1 节,“变量赋值的 SET 语法”。要更改正在运行的 MySQL 实例的值而不在后续重启时保留,请使用GLOBAL关键字,而不是PERSIST。SET PERSISTsets a value for the running MySQL instance. It also saves the value to carry over to subsequent server restarts; see Section 15.7.6.1, “SET Syntax for Variable Assignment”. To change the value for the running MySQL instance without having it carry over to subsequent restarts, use theGLOBALkeyword rather thanPERSIST.
The global password-expiration policy applies to all accounts
that have not been set to override it. To establish policy for
individual accounts, use the PASSWORD EXPIRE
option of the CREATE USER以及ALTER USER statements. See
Section 15.7.1.3, “CREATE USER Statement”,以及Section 15.7.1.1, “ALTER USER Statement”.The global password-expiration policy applies to all accounts
that have not been set to override it. To establish policy for
individual accounts, use the PASSWORD EXPIRE
option of the CREATE USER and
ALTER USER statements. See
Section 15.7.1.3, “CREATE USER Statement”, and Section 15.7.1.1, “ALTER USER Statement”.
账户特定示例说明:Example account-specific statements:
要求每 90 天更改一次密码:Require the password to be changed every 90 days:
CREATE USER 'jeffrey'@'localhost' PASSWORD EXPIRE INTERVAL 90 DAY; ALTER USER 'jeffrey'@'localhost' PASSWORD EXPIRE INTERVAL 90 DAY;
此过期选项会覆盖该说明中指定的所有账户的全局策略。This expiration option overrides the global policy for all accounts named by the statement.
禁用密码过期:Disable password expiration:
CREATE USER 'jeffrey'@'localhost' PASSWORD EXPIRE NEVER; ALTER USER 'jeffrey'@'localhost' PASSWORD EXPIRE NEVER;
此过期选项会覆盖该说明中指定的所有账户的全局策略。This expiration option overrides the global policy for all accounts named by the statement.
遵循该说明中指定的所有账户的全局过期策略:Defer to the global expiration policy for all accounts named by the statement:
CREATE USER 'jeffrey'@'localhost' PASSWORD EXPIRE DEFAULT; ALTER USER 'jeffrey'@'localhost' PASSWORD EXPIRE DEFAULT;
当客户端成功连接时,服务器会确定账户密码是否已过期:When a client successfully connects, the server determines whether the account password has expired:
服务器检查密码是否已被手动设为过期。The server checks whether the password has been manually expired.
否则,服务器会检查密码的使用时间是否已超过根据自动密码过期策略允许的期限。如果是,服务器会将密码视为已过期。Otherwise, the server checks whether the password age is greater than its permitted lifetime according to the automatic password expiration policy. If so, the server considers the password expired.
如果密码已过期(无论是手动还是自动),服务器要么断开客户端连接,要么限制其允许执行的操作(参见 第 8.2.16 节,“服务器处理过期密码”)。受限客户端执行的操作会导致错误,直到用户设置新的帐户密码:If the password is expired (whether manually or automatically), the server either disconnects the client or restricts the operations permitted to it (see Section 8.2.16, “Server Handling of Expired Passwords”). Operations performed by a restricted client result in an error until the user establishes a new account password:
mysql>SELECT 1;ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement. mysql>ALTER USER USER() IDENTIFIED BY 'Query OK, 0 rows affected (0.01 sec) mysql>password';SELECT 1;+---+ | 1 | +---+ | 1 | +---+ 1 row in set (0.00 sec)
在客户端重置密码后,服务器会恢复该会话的正常访问,以及使用该账户的后续连接。管理用户也可以重置账户密码,但该账户的任何已存在的受限会话仍然保持受限。使用该账户的客户端必须断开连接并重新连接,然后才能成功执行语句。After the client resets the password, the server restores normal access for the session, as well as for subsequent connections that use the account. It is also possible for an administrative user to reset the account password, but any existing restricted sessions for that account remain restricted. A client using the account must disconnect and reconnect before statements can be executed successfully.
虽然可以通过将过期密码重置为当前值来“重置”密码,但作为良好策略,更好的做法是选择一个不同的密码。数据库管理员可以通过建立适当的密码重用政策来强制不重复使用。请参阅密码重用政策.Although it is possible to “reset” an expired password by setting it to its current value, it is preferable, as a matter of good policy, to choose a different password. DBAs can enforce non-reuse by establishing an appropriate password-reuse policy. See Password Reuse Policy.
MySQL 允许对以前使用过的密码设置重复使用的限制。重复使用限制可以基于密码更改次数、经过的时间或两者同时设定。重复使用策略可以在全局范围内设定,单个账户可以选择遵循全局策略,或者通过特定的每账户行为覆盖全局策略。MySQL enables restrictions to be placed on reuse of previous passwords. Reuse restrictions can be established based on number of password changes, time elapsed, or both. Reuse policy can be established globally, and individual accounts can be set to either defer to the global policy or override the global policy with specific per-account behavior.
账户的密码历史由其过去被分配的密码组成。MySQL 可以限制新密码不能从这些历史密码中选择:The password history for an account consists of passwords it has been assigned in the past. MySQL can restrict new passwords from being chosen from this history:
如果账户被基于密码更改次数进行限制,则新密码不能从指定数量的最近密码中选择。例如,如果密码最少更改次数设置为 3,则新密码不能与最近的 3 个密码中的任何一个相同。If an account is restricted on the basis of number of password changes, a new password cannot be chosen from a specified number of the most recent passwords. For example, if the minimum number of password changes is set to 3, a new password cannot be the same as any of the most recent 3 passwords.
如果账户因时间限制而受限,则不能从历史密码中选择比指定天数更新的密码。例如,如果密码重用间隔设置为 60,则新密码不得为过去 60 天内使用过的密码之一。If an account is restricted based on time elapsed, a new password cannot be chosen from passwords in the history that are newer than a specified number of days. For example, if the password reuse interval is set to 60, a new password must not be among those previously chosen within the last 60 days.
空密码不计入密码历史记录,可以随时重复使用。The empty password does not count in the password history and is subject to reuse at any time.
要在全局范围内建立密码重用策略,请使用 password_history 和 password_reuse_interval 系统变量。To establish password-reuse policy globally, use the
password_history and
password_reuse_interval system
variables.
示例:Examples:
要禁止重复使用最近 6 个密码或最近 365 天内的密码,请将以下行添加到服务器的
my.cnf文件中:To prohibit reusing any of the last 6 passwords or passwords newer than 365 days, put these lines in the servermy.cnffile:[mysqld] password_history=6 password_reuse_interval=365
要在运行时设置并持久化变量,请使用如下语句:To set and persist the variables at runtime, use statements like this:
SET PERSIST password_history = 6; SET PERSIST password_reuse_interval = 365;
SET PERSIST为正在运行的 MySQL 实例设置一个值。它还会保存该值,以便在后续服务器重启时继续使用;参见 第 15.7.6.1 节,“变量赋值的 SET 语法”。要更改正在运行的 MySQL 实例的值而不在后续重启时保留,请使用GLOBAL关键字,而不是PERSIST。SET PERSISTsets a value for the running MySQL instance. It also saves the value to carry over to subsequent server restarts; see Section 15.7.6.1, “SET Syntax for Variable Assignment”. To change the value for the running MySQL instance without having it carry over to subsequent restarts, use theGLOBALkeyword rather thanPERSIST.
全球密码重用策略适用于所有未设置为覆盖此策略的账户。要为单个账户建立策略,请使用 PASSWORD HISTORY 和 PASSWORD REUSE INTERVAL 选项,这些选项可在 CREATE USER 和 ALTER USER 语句中使用。请参阅 第 15.7.1.3 节,“CREATE USER 语句” 和 第 15.7.1.1 节,“ALTER USER 语句”。The global password-reuse policy applies to all accounts that
have not been set to override it. To establish policy for
individual accounts, use the PASSWORD HISTORY
and PASSWORD REUSE INTERVAL options of the
CREATE USER and
ALTER USER statements. See
Section 15.7.1.3, “CREATE USER Statement”, and Section 15.7.1.1, “ALTER USER Statement”.
账户特定示例说明:Example account-specific statements:
在允许重复使用之前,要求至少更改 5 次密码:Require a minimum of 5 password changes before permitting reuse:
CREATE USER 'jeffrey'@'localhost' PASSWORD HISTORY 5; ALTER USER 'jeffrey'@'localhost' PASSWORD HISTORY 5;
此历史长度选项会覆盖语句中指定的所有帐户的全局策略。This history-length option overrides the global policy for all accounts named by the statement.
在允许重复使用之前,要求至少经过 365 天:Require a minimum of 365 days elapsed before permitting reuse:
CREATE USER 'jeffrey'@'localhost' PASSWORD REUSE INTERVAL 365 DAY; ALTER USER 'jeffrey'@'localhost' PASSWORD REUSE INTERVAL 365 DAY;
此已过时间选项会覆盖语句中指定的所有帐户的全局策略。This time-elapsed option overrides the global policy for all accounts named by the statement.
要结合两种类型的重用限制,请同时使用
PASSWORD HISTORY和PASSWORD REUSE INTERVAL:To combine both types of reuse restrictions, usePASSWORD HISTORYandPASSWORD REUSE INTERVALtogether:CREATE USER 'jeffrey'@'localhost' PASSWORD HISTORY 5 PASSWORD REUSE INTERVAL 365 DAY; ALTER USER 'jeffrey'@'localhost' PASSWORD HISTORY 5 PASSWORD REUSE INTERVAL 365 DAY;
这些选项会覆盖声明中所列所有账户的全局策略重用限制。These options override both global policy reuse restrictions for all accounts named by the statement.
对于两种类型的重用限制,遵循全局策略:Defer to the global policy for both types of reuse restrictions:
CREATE USER 'jeffrey'@'localhost' PASSWORD HISTORY DEFAULT PASSWORD REUSE INTERVAL DEFAULT; ALTER USER 'jeffrey'@'localhost' PASSWORD HISTORY DEFAULT PASSWORD REUSE INTERVAL DEFAULT;
可以要求在尝试更改账户密码时,必须通过指定当前要被替换的密码来进行验证。这使得数据库管理员可以防止用户在未证明知道当前密码的情况下更改密码。否则,这类更改可能会发生,例如,当某个用户暂时离开终端会话而没有注销时,恶意用户可能会利用该会话更改原用户的 MySQL 密码。这可能导致不良后果:It is possible to require that attempts to change an account password be verified by specifying the current password to be replaced. This enables DBAs to prevent users from changing a password without proving that they know the current password. Such changes could otherwise occur, for example, if one user walks away from a terminal session temporarily without logging out, and a malicious user uses the session to change the original user's MySQL password. This can have unfortunate consequences:
原用户在管理员重置账户密码之前将无法访问 MySQL。The original user becomes unable to access MySQL until the account password is reset by an administrator.
在密码重置之前,恶意用户可以使用被更改的凭据访问 MySQL。Until the password reset occurs, the malicious user can access MySQL with the benign user's changed credentials.
可以在全局范围内建立密码验证策略,并且可以为各个账户设置以遵循全局策略或通过特定的每账户行为覆盖全局策略。Password-verification policy can be established globally, and individual accounts can be set to either defer to the global policy or override the global policy with specific per-account behavior.
对于每个账户,其 mysql.user 行指示是否存在需要在尝试更改密码时验证当前密码的账户特定设置。该设置由 PASSWORD
REQUIRE 和 ALTER USER 语句中的 CREATE
USER 选项建立:For each account, its mysql.user row
indicates whether there is an account-specific setting requiring
verification of the current password for password change
attempts. The setting is established by the PASSWORD
REQUIRE option of the CREATE
USER and ALTER USER
statements:
如果账户设置为
PASSWORD REQUIRE CURRENT,则更改密码时必须指定当前密码。If the account setting isPASSWORD REQUIRE CURRENT, password changes must specify the current password.如果账户设置为
PASSWORD REQUIRE CURRENT OPTIONAL,则更改密码时可以但不必指定当前密码。If the account setting isPASSWORD REQUIRE CURRENT OPTIONAL, password changes may but need not specify the current password.如果账户设置为
PASSWORD REQUIRE CURRENT DEFAULT,则password_require_current系统变量决定账户的验证要求策略:If the account setting isPASSWORD REQUIRE CURRENT DEFAULT, thepassword_require_currentsystem variable determines the verification-required policy for the account:如果
password_require_current已启用,更改密码时必须指定当前密码。Ifpassword_require_currentis enabled, password changes must specify the current password.如果
password_require_current已禁用,更改密码时可以也可以不指定当前密码。Ifpassword_require_currentis disabled, password changes may but need not specify the current password.
换句话说,如果账户设置不是 PASSWORD
REQUIRE CURRENT DEFAULT,则账户设置优先于由 password_require_current 系统变量建立的全局策略。否则,账户会遵循 password_require_current 设置。In other words, if the account setting is not PASSWORD
REQUIRE CURRENT DEFAULT, the account setting takes
precedence over the global policy established by the
password_require_current system
variable. Otherwise, the account defers to the
password_require_current
setting.
默认情况下,密码验证是可选的:password_require_current 被禁用,并且创建时未使用 PASSWORD
REQUIRE 选项的账户默认设置为 PASSWORD REQUIRE
CURRENT DEFAULT。By default, password verification is optional:
password_require_current is
disabled and accounts created with no PASSWORD
REQUIRE option default to PASSWORD REQUIRE
CURRENT DEFAULT.
下表显示了每个账户的设置如何与 password_require_current 系统变量值交互,以确定账户密码验证所需的策略。The following table shows how per-account settings interact with
password_require_current system
variable values to determine account password
verification-required policy.
表 8.10 密码验证策略Table 8.10 Password-Verification Policy
| 每个账户设置Per-Account Setting | password_require_current 系统变量password_require_current System Variable | 密码更改是否需要当前密码?Password Changes Require Current Password? |
|---|---|---|
PASSWORD REQUIRE CURRENT |
OFFOFF |
是Yes |
PASSWORD REQUIRE CURRENT |
ONON |
是Yes |
PASSWORD REQUIRE CURRENT OPTIONAL |
OFFOFF |
否No |
PASSWORD REQUIRE CURRENT OPTIONAL |
ONON |
否No |
PASSWORD REQUIRE CURRENT DEFAULT |
OFFOFF |
否No |
PASSWORD REQUIRE CURRENT DEFAULT |
ONON |
是Yes |
特权用户可以在不指定当前密码的情况下更改任何账户密码,无论是否要求验证策略。特权用户是指具有全局 CREATE USER 权限或 UPDATE 权限用于 mysql 系统数据库的用户。Privileged users can change any account password without
specifying the current password, regardless of the
verification-required policy. A privileged user is one who has
the global CREATE USER
privilege or the UPDATE
privilege for the mysql system database.
要在全局范围内建立密码验证策略,请使用 password_require_current 系统变量。其默认值为 OFF,因此在更改账户密码时不需要指定当前密码。To establish password-verification policy globally, use the
password_require_current system
variable. Its default value is OFF, so it is
not required that account password changes specify the current
password.
示例:Examples:
要建立一个全局策略,要求密码更改必须指定当前密码,请在服务器的
my.cnf文件中加入以下行启动服务器:To establish a global policy that password changes must specify the current password, start the server with these lines in a servermy.cnffile:[mysqld] password_require_current=ON
要在运行时设置并持久化
password_require_current,请使用如下语句之一:To set and persistpassword_require_currentat runtime, use a statement such as one of these:SET PERSIST password_require_current = ON; SET PERSIST password_require_current = OFF;
SET PERSIST为正在运行的 MySQL 实例设置一个值。它还会保存该值,以便在后续服务器重启时继续使用;参见 第 15.7.6.1 节,“变量赋值的 SET 语法”。要更改正在运行的 MySQL 实例的值而不在后续重启时保留,请使用GLOBAL关键字,而不是PERSIST。SET PERSISTsets a value for the running MySQL instance. It also saves the value to carry over to subsequent server restarts; see Section 15.7.6.1, “SET Syntax for Variable Assignment”. To change the value for the running MySQL instance without having it carry over to subsequent restarts, use theGLOBALkeyword rather thanPERSIST.
全局密码验证必需策略适用于所有未设置覆盖该策略的账户。要为单个账户建立策略,请使用 PASSWORD
REQUIRE 和 ALTER USER 语句中的 CREATE
USER 选项。参见 第 15.7.1.3 节,“CREATE USER 语句” 和 第 15.7.1.1 节,“ALTER USER 语句”。The global password verification-required policy applies to all
accounts that have not been set to override it. To establish
policy for individual accounts, use the PASSWORD
REQUIRE options of the CREATE
USER and ALTER USER
statements. See Section 15.7.1.3, “CREATE USER Statement”, and
Section 15.7.1.1, “ALTER USER Statement”.
账户特定示例说明:Example account-specific statements:
要求密码更改时必须指定当前密码:Require that password changes specify the current password:
CREATE USER 'jeffrey'@'localhost' PASSWORD REQUIRE CURRENT; ALTER USER 'jeffrey'@'localhost' PASSWORD REQUIRE CURRENT;
此验证选项会覆盖语句中指定的所有帐户的全局策略。This verification option overrides the global policy for all accounts named by the statement.
不要求密码更改时指定当前密码(可以提供当前密码,也可以不提供):Do not require that password changes specify the current password (the current password may but need not be given):
CREATE USER 'jeffrey'@'localhost' PASSWORD REQUIRE CURRENT OPTIONAL; ALTER USER 'jeffrey'@'localhost' PASSWORD REQUIRE CURRENT OPTIONAL;
此验证选项会覆盖语句中指定的所有帐户的全局策略。This verification option overrides the global policy for all accounts named by the statement.
对于语句中指定的所有帐户,遵循全局密码验证要求策略:Defer to the global password verification-required policy for all accounts named by the statement:
CREATE USER 'jeffrey'@'localhost' PASSWORD REQUIRE CURRENT DEFAULT; ALTER USER 'jeffrey'@'localhost' PASSWORD REQUIRE CURRENT DEFAULT;
当用户使用ALTER
USER或SET PASSWORD语句更改密码时,会涉及当前密码的验证。示例中使用ALTER
USER语句,它比SET
PASSWORD更为推荐,但此处描述的原则对两种语句都是相同的。Verification of the current password comes into play when a user
changes a password using the ALTER
USER or SET PASSWORD
statement. The examples use ALTER
USER, which is preferred over SET
PASSWORD, but the principles described here are the
same for both statements.
在更改密码的语句中,REPLACE子句指定要被替换的当前密码。示例:In password-change statements, a REPLACE
clause specifies the current password to be replaced. Examples:
更改当前用户的密码:Change the current user's password:
ALTER USER USER() IDENTIFIED BY '
auth_string' REPLACE 'current_auth_string';更改指定用户的密码:Change a named user's password:
ALTER USER 'jeffrey'@'localhost' IDENTIFIED BY '
auth_string' REPLACE 'current_auth_string';更改指定用户的认证插件和密码:Change a named user's authentication plugin and password:
ALTER USER 'jeffrey'@'localhost' IDENTIFIED WITH caching_sha2_password BY '
auth_string' REPLACE 'current_auth_string';
REPLACE 子句的工作原理如下:The REPLACE clause works like this:
如果账号的密码更改需要指定当前密码以验证尝试更改密码的用户确实知道当前密码,则必须提供
REPLACE。REPLACEmust be given if password changes for the account are required to specify the current password, as verification that the user attempting to make the change actually knows the current password.如果账号的密码更改可以但不必指定当前密码,则
REPLACE是可选的。REPLACEis optional if password changes for the account may but need not specify the current password.如果指定了
REPLACE,则必须指定正确的当前密码,否则会发生错误。即使REPLACE是可选的,这一点仍然适用。IfREPLACEis specified, it must specify the correct current password, or an error occurs. This is true even ifREPLACEis optional.REPLACE仅能在更改当前用户的账号密码时指定。(这意味着在刚才显示的示例中,明确指定jeffrey账号的语句会失败,除非当前用户是jeffrey。)即便是特权用户尝试为其他用户更改密码,这一规则仍然成立;然而,此类用户可以在不指定REPLACE的情况下更改任何密码。REPLACEcan be specified only when changing the account password for the current user. (This means that in the examples just shown, the statements that explicitly name the account forjeffreyfail unless the current user isjeffrey.) This is true even if the change is attempted for another user by a privileged user; however, such a user can change any password without specifyingREPLACE.REPLACE会从二进制日志中省略,以避免将明文密码写入其中。REPLACEis omitted from the binary log to avoid writing cleartext passwords to it.
用户账户允许拥有双重密码,分别指定为主密码和次密码。双重密码功能使在以下情况下无缝执行凭证更改成为可能:User accounts are permitted to have dual passwords, designated as primary and secondary passwords. Dual-password capability makes it possible to seamlessly perform credential changes in scenarios like this:
系统有大量的 MySQL 服务器,可能涉及复制。A system has a large number of MySQL servers, possibly involving replication.
多个应用程序连接到不同的 MySQL 服务器。Multiple applications connect to different MySQL servers.
必须定期更改应用程序用于连接服务器的账户的凭证。Periodic credential changes must be made to the account or accounts used by the applications to connect to the servers.
考虑在前述类型的场景中,当一个账户仅允许单个密码时,凭证更改必须如何执行。在这种情况下,账户密码更改的时间及其在所有服务器上的传播与所有使用该账户的应用程序更新为新密码之间必须紧密配合。此过程可能涉及服务器或应用程序不可用的停机时间。Consider how a credential change must be performed in the preceding type of scenario when an account is permitted only a single password. In this case, there must be close cooperation in the timing of when the account password change is made and propagated throughout all servers, and when all applications that use the account are updated to use the new password. This process may involve downtime during which servers or applications are unavailable.
使用双重密码,可以更轻松地分阶段更改凭证,无需紧密协作,也无需停机:With dual passwords, credential changes can be made more easily, in phases, without requiring close cooperation, and without downtime:
对于每个受影响的账户,在服务器上建立新的主密码,同时保留当前密码作为次要密码。这使得服务器可以识别每个账户的主密码或次要密码,而应用程序可以继续使用先前相同的密码(现在为次要密码)连接到服务器。For each affected account, establish a new primary password on the servers, retaining the current password as the secondary password. This enables servers to recognize either the primary or secondary password for each account, while applications can continue to connect to the servers using the same password as previously (which is now the secondary password).
在密码更改传播到所有服务器后,修改使用任何受影响账户的应用程序,使其使用账户主密码进行连接。After the password change has propagated to all servers, modify applications that use any affected account to connect using the account primary password.
在所有应用程序已从次要密码迁移到主要密码后,次要密码不再需要,可以被丢弃。在此更改传播到所有服务器后,每个账户只能使用主要密码进行连接。凭证更改现已完成。After all applications have been migrated from the secondary passwords to the primary passwords, the secondary passwords are no longer needed and can be discarded. After this change has propagated to all servers, only the primary password for each account can be used to connect. The credential change is now complete.
MySQL 使用语法实现双密码功能,可保存和丢弃次要密码:MySQL implements dual-password capability with syntax that saves and discards secondary passwords:
RETAIN CURRENT PASSWORD子句用于ALTER USER和SET PASSWORD语句,当你分配新主密码时,会将账户当前密码保存为其次要密码。TheRETAIN CURRENT PASSWORDclause for theALTER USERandSET PASSWORDstatements saves an account current password as its secondary password when you assign a new primary password.DISCARD OLD PASSWORD子句用于ALTER USER,会丢弃账户的次要密码,仅保留主密码。TheDISCARD OLD PASSWORDclause forALTER USERdiscards an account secondary password, leaving only the primary password.
假设在前面描述的凭据更改场景中,一个名为'appuser1'@'host1.example.com'的账户被应用程序用来连接服务器,并且该账户的密码要从'更改为password_a''。Suppose that, for the previously described credential-change
scenario, an account named
password_b''appuser1'@'host1.example.com' is used by
applications to connect to servers, and that the account
password is to be changed from
' to
password_a''.password_b'
要执行此凭据更改,请使用ALTER
USER如下:To perform this change of credentials, use ALTER
USER as follows:
在每个非副本服务器上,将
'设置为password_b'appuser1的新主密码,同时保留当前密码作为备用密码:On each server that is not a replica, establish'as the newpassword_b'appuser1primary password, retaining the current password as the secondary password:ALTER USER 'appuser1'@'host1.example.com' IDENTIFIED BY '
password_b' RETAIN CURRENT PASSWORD;等待密码更改在整个系统中的所有副本中同步。Wait for the password change to replicate throughout the system to all replicas.
修改每个使用
appuser1帐户的应用程序,使其使用'而不是password_b''来连接到服务器。Modify each application that uses thepassword_a'appuser1account so that it connects to the servers using a password of'rather thanpassword_b''.password_a'此时,不再需要二级密码。在每个不是副本的服务器上,丢弃二级密码:At this point, the secondary password is no longer needed. On each server that is not a replica, discard the secondary password:
ALTER USER 'appuser1'@'host1.example.com' DISCARD OLD PASSWORD;
在丢弃密码的更改复制到所有副本后,凭据更改即完成。After the discard-password change has replicated to all replicas, the credential change is complete.
RETAIN CURRENT PASSWORD 和 DISCARD OLD PASSWORD 子句具有以下效果:The RETAIN CURRENT PASSWORD and
DISCARD OLD PASSWORD clauses have the
following effects:
RETAIN CURRENT PASSWORD会将账户的当前密码保留为其二级密码,替换任何现有的二级密码。新密码成为主密码,但客户端可以使用账户通过主密码或二级密码连接到服务器。(例外:如果ALTER USER或SET PASSWORD语句中指定的新密码为空,即使给出了RETAIN CURRENT PASSWORD,二级密码也会为空。)RETAIN CURRENT PASSWORDretains an account current password as its secondary password, replacing any existing secondary password. The new password becomes the primary password, but clients can use the account to connect to the server using either the primary or secondary password. (Exception: If the new password specified by theALTER USERorSET PASSWORDstatement is empty, the secondary password becomes empty as well, even ifRETAIN CURRENT PASSWORDis given.)如果你为一个主密码为空的账户指定
RETAIN CURRENT PASSWORD,该语句将执行失败。If you specifyRETAIN CURRENT PASSWORDfor an account that has an empty primary password, the statement fails.如果一个账户有次要密码,并且你在没有指定
RETAIN CURRENT PASSWORD的情况下更改其主密码,则次要密码保持不变。If an account has a secondary password and you change its primary password without specifyingRETAIN CURRENT PASSWORD, the secondary password remains unchanged.对于
ALTER USER,如果你更改账户分配的认证插件,次要密码将被丢弃。如果你更改认证插件且同时指定RETAIN CURRENT PASSWORD,语句将执行失败。ForALTER USER, if you change the authentication plugin assigned to the account, the secondary password is discarded. If you change the authentication plugin and also specifyRETAIN CURRENT PASSWORD, the statement fails.对于
ALTER USER,DISCARD OLD PASSWORD会丢弃次要密码(如果存在)。该账户仅保留主要密码,客户端只能使用主要密码连接到服务器。ForALTER USER,DISCARD OLD PASSWORDdiscards the secondary password, if one exists. The account retains only its primary password, and clients can use the account to connect to the server only with the primary password.
修改二级密码的语句需要这些权限:Statements that modify secondary passwords require these privileges:
使用
APPLICATION_PASSWORD_ADMIN权限才能在适用于您自己的账户的RETAIN CURRENT PASSWORD和DISCARD OLD PASSWORD语句中使用ALTER USER或SET PASSWORD子句。因为大多数用户只需要一个密码,所以操作您自己的次要密码也需要此权限。TheAPPLICATION_PASSWORD_ADMINprivilege is required to use theRETAIN CURRENT PASSWORDorDISCARD OLD PASSWORDclause forALTER USERandSET PASSWORDstatements that apply to your own account. The privilege is required to manipulate your own secondary password because most users require only one password.如果一个账户被允许操作所有账户的次要密码,它应该被授予
CREATE USER权限,而不是APPLICATION_PASSWORD_ADMIN权限。If an account is to be permitted to manipulate secondary passwords for all accounts, it should be granted theCREATE USERprivilege rather thanAPPLICATION_PASSWORD_ADMIN.
CREATE USER、ALTER USER 和 SET PASSWORD 语句可以为用户账户生成随机密码,作为替代要求管理员显式指定字面密码的方法。有关语法的详细信息,请参阅每个语句的说明。本节描述了生成的随机密码的共同特性。The CREATE USER,
ALTER USER, and
SET PASSWORD statements have the
capability of generating random passwords for user accounts, as
an alternative to requiring explicit administrator-specified
literal passwords. See the description of each statement for
details about the syntax. This section describes the
characteristics common to generated random passwords.
默认情况下,生成的随机密码长度为 20 个字符。此长度由 generated_random_password_length 系统变量控制,其范围为 5 到 255。By default, generated random passwords have a length of 20
characters. This length is controlled by the
generated_random_password_length
system variable, which has a range from 5 to 255.
对于每个通过语句生成随机密码的账户,语句会将密码存储在 mysql.user 系统表中,并根据账户认证插件适当进行哈希处理。该语句还会在结果集的一行中返回明文密码,以便执行语句的用户或应用程序使用。结果集的列名为 user、host、generated password 和 auth_factor,分别表示用于标识 mysql.user 系统表中受影响行的用户名和主机名、生成的明文密码,以及显示的密码值适用的认证因子。For each account for which a statement generates a random
password, the statement stores the password in the
mysql.user system table, hashed appropriately
for the account authentication plugin. The statement also
returns the cleartext password in a row of a result set to make
it available to the user or application executing the statement.
The result set columns are named user,
host, generated password,
and auth_factor indicating the user name and
host name values that identify the affected row in the
mysql.user system table, the cleartext
generated password, and the authentication factor the displayed
password value applies to.
mysql>CREATE USER'u1'@'localhost' IDENTIFIED BY RANDOM PASSWORD,'u2'@'%.example.com' IDENTIFIED BY RANDOM PASSWORD,'u3'@'%.org' IDENTIFIED BY RANDOM PASSWORD;+------+---------------+----------------------+-------------+ | user | host | generated password | auth_factor | +------+---------------+----------------------+-------------+ | u1 | localhost | iOeqf>Mh9:;XD&qn(Hl} | 1 | | u2 | %.example.com | sXTSAEvw3St-R+_-C3Vb | 1 | | u3 | %.org | nEVe%Ctw/U/*Md)Exc7& | 1 | +------+---------------+----------------------+-------------+ mysql>ALTER USER'u1'@'localhost' IDENTIFIED BY RANDOM PASSWORD,'u2'@'%.example.com' IDENTIFIED BY RANDOM PASSWORD;+------+---------------+----------------------+-------------+ | user | host | generated password | auth_factor | +------+---------------+----------------------+-------------+ | u1 | localhost | Seiei:&cw}8]@3OA64vh | 1 | | u2 | %.example.com | j@&diTX80l8}(NiHXSae | 1 | +------+---------------+----------------------+-------------+ mysql>SET PASSWORD FOR 'u3'@'%.org' TO RANDOM;+------+-------+----------------------+-------------+ | user | host | generated password | auth_factor | +------+-------+----------------------+-------------+ | u3 | %.org | n&cz2xF;P3!U)+]Vw52H | 1 | +------+-------+----------------------+-------------+
生成账户随机密码的 CREATE USER、ALTER USER 或 SET PASSWORD 语句在二进制日志中被写成 CREATE USER 或 ALTER USER 语句,并带有 IDENTIFIED WITH auth_plugin
AS 'auth_string' 子句,其中 auth_plugin 是账户的认证插件,' 是账户的哈希密码值。A auth_string'CREATE USER,
ALTER USER, or
SET PASSWORD statement that
generates a random password for an account is written to the
binary log as a CREATE USER or
ALTER USER statement with an
IDENTIFIED WITH , clause,
where auth_plugin
AS 'auth_string'auth_plugin is the account
authentication plugin and
' is
the account hashed password value.auth_string'
如果安装了validate_password组件,它所实施的策略对生成的密码没有影响。(密码验证的目的是帮助人类创建更好的密码。)If the validate_password component is
installed, the policy that it implements has no effect on
generated passwords. (The purpose of password validation is to
help humans create better passwords.)
管理员可以配置用户账户,使得连续过多的登录失败会导致账户暂时锁定。Administrators can configure user accounts such that too many consecutive login failures cause temporary account locking.
“登录失败” 在此上下文中指客户端在尝试连接时未能提供正确密码。这不包括由于用户不存在或网络问题等原因导致的连接失败。对于拥有双重密码的账户(请参阅 双重密码支持),任一账户密码均视为正确。“Login failure” in this context means failure of the client to provide a correct password during a connection attempt. It does not include failure to connect for reasons such as unknown user or network issues. For accounts that have dual passwords (see Dual Password Support), either account password counts as correct.
每个账户所需的登录失败次数及锁定时间可以配置,使用 CREATE USER 和 ALTER USER 语句的 FAILED_LOGIN_ATTEMPTS 和 PASSWORD_LOCK_TIME 选项。示例:The required number of login failures and the lock time are
configurable per account, using the
FAILED_LOGIN_ATTEMPTS and
PASSWORD_LOCK_TIME options of the
CREATE USER and
ALTER USER statements. Examples:
CREATE USER 'u1'@'localhost' IDENTIFIED BY 'password'
FAILED_LOGIN_ATTEMPTS 3 PASSWORD_LOCK_TIME 3;
ALTER USER 'u2'@'localhost'
FAILED_LOGIN_ATTEMPTS 4 PASSWORD_LOCK_TIME UNBOUNDED;
当连续登录失败次数过多时,客户端会收到如下错误:When too many consecutive login failures occur, the client receives an error that looks like this:
ERROR 3957 (HY000): Access denied for useruser. Account is blocked forDday(s) (Rday(s) remaining) due toNconsecutive failed logins.
按如下方式使用选项:Use the options as follows:
FAILED_LOGIN_ATTEMPTSN此选项用于指示是否跟踪输入错误密码的账户登录尝试。数字
N指定连续输错密码的次数,达到该次数后账户将被暂时锁定。This option indicates whether to track account login attempts that specify an incorrect password. The numberNspecifies how many consecutive incorrect passwords cause temporary account locking.PASSWORD_LOCK_TIME {N| UNBOUNDED}此选项指示在连续多次登录尝试提供错误密码后锁定账户的时长。该值是一个数字
N,用于指定账户保持锁定状态的天数,或者使用UNBOUNDED来指定当账户进入暂时锁定状态时,该状态的持续时间是无限的,直到账户被解锁为止。解锁的条件将在后面描述。This option indicates how long to lock the account after too many consecutive login attempts provide an incorrect password. The value is a numberNto specify the number of days the account remains locked, orUNBOUNDEDto specify that when an account enters the temporarily locked state, the duration of that state is unbounded and does not end until the account is unlocked. The conditions under which unlocking occurs are described later.
每个选项允许的 N 值范围是 0 到 32767。值为 0 会禁用此选项。Permitted values of N for each option
are in the range from 0 to 32767. A value of 0 disables the
option.
失败登录跟踪和临时账户锁定具有以下特点:Failed-login tracking and temporary account locking have these characteristics:
要对账户进行失败登录跟踪和临时锁定,其
FAILED_LOGIN_ATTEMPTS和PASSWORD_LOCK_TIME选项都必须为非零。For failed-login tracking and temporary locking to occur for an account, itsFAILED_LOGIN_ATTEMPTSandPASSWORD_LOCK_TIMEoptions both must be nonzero.对于
CREATE USER,如果没有指定FAILED_LOGIN_ATTEMPTS或PASSWORD_LOCK_TIME,那么该语句所命名的所有账户都会使用隐式默认值 0。这意味着失败登录跟踪和临时账户锁定被禁用。ForCREATE USER, ifFAILED_LOGIN_ATTEMPTSorPASSWORD_LOCK_TIMEis not specified, its implicit default value is 0 for all accounts named by the statement. This means that failed-login tracking and temporary account locking are disabled.对于
ALTER USER,如果没有指定FAILED_LOGIN_ATTEMPTS或PASSWORD_LOCK_TIME,那么该语句所命名的所有账户的值保持不变。ForALTER USER, ifFAILED_LOGIN_ATTEMPTSorPASSWORD_LOCK_TIMEis not specified, its value remains unchanged for all accounts named by the statement.要发生临时账户锁定,密码错误必须是连续的。在达到
FAILED_LOGIN_ATTEMPTS失败登录次数之前的任何成功登录都会导致失败计数重置。例如,如果FAILED_LOGIN_ATTEMPTS是 4,并且已经发生了三次连续的密码错误,则需要再发生一次错误才能开始锁定。但是,如果下一次登录成功,账户的失败登录计数将被重置,因此再次需要四次连续失败才能锁定。For temporary account locking to occur, password failures must be consecutive. Any successful login that occurs prior to reaching theFAILED_LOGIN_ATTEMPTSvalue for failed logins causes failure counting to reset. For example, ifFAILED_LOGIN_ATTEMPTSis 4 and three consecutive password failures have occurred, one more failure is necessary for locking to begin. But if the next login succeeds, failed-login counting for the account is reset so that four consecutive failures are again required for locking.一旦开始临时锁定,即使输入正确密码,也无法成功登录,直到锁定期限过或通过以下讨论中列出的其中一种账户重置方法解锁账户。Once temporary locking begins, successful login cannot occur even with the correct password until either the lock duration has passed or the account is unlocked by one of the account-reset methods listed in the following discussion.
当服务器读取授权表时,会为每个账户初始化状态信息,包括是否启用失败登录跟踪、账户当前是否处于临时锁定状态以及如果锁定,锁定开始时间,以及如果账户未锁定,则临时锁定前的失败次数。When the server reads the grant tables, it initializes state information for each account regarding whether failed-login tracking is enabled, whether the account is currently temporarily locked and when locking began if so, and the number of failures before temporary locking occurs if the account is not locked.
账户的状态信息可以重置,这意味着失败登录计数被重置,并且如果账户当前处于临时锁定状态,将被解锁。账户重置可以是针对所有账户的全局重置,也可以是针对单个账户的重置:An account's state information can be reset, which means that failed-login counting is reset, and the account is unlocked if currently temporarily locked. Account resets can be global for all accounts or per account:
在以下任何情况下,所有帐户将进行全球重置:A global reset of all accounts occurs for any of these conditions:
服务器重启。A server restart.
执行
FLUSH PRIVILEGES。(使用--skip-grant-tables启动服务器会导致授权表不被读取,从而禁用失败登录跟踪。在这种情况下,第一次执行FLUSH PRIVILEGES会使服务器读取授权表并启用失败登录跟踪,同时重置所有账户。)Execution ofFLUSH PRIVILEGES. (Starting the server with--skip-grant-tablescauses the grant tables not to be read, which disables failed-login tracking. In this case, the first execution ofFLUSH PRIVILEGEScauses the server to read the grant tables and enable failed-login tracking, in addition to resetting all accounts.)
对于以下任何情况,将发生每个账户的重置:A per-account reset occurs for any of these conditions:
账户成功登录。Successful login for the account.
锁定期限过去。在这种情况下,失败登录计数将在下一次登录尝试时重置。The lock duration passes. In this case, failed-login counting resets at the time of the next login attempt.
执行针对账户的
ALTER USER语句,将FAILED_LOGIN_ATTEMPTS或PASSWORD_LOCK_TIME(或两者)设置为任意值(包括当前选项值),或者执行针对该账户的ALTER USER ... UNLOCK语句。Execution of anALTER USERstatement for the account that sets eitherFAILED_LOGIN_ATTEMPTSorPASSWORD_LOCK_TIME(or both) to any value (including the current option value), or execution of anALTER USER ... UNLOCKstatement for the account.账户的其他
ALTER USER语句不会影响其当前的失败登录次数或锁定状态。OtherALTER USERstatements for the account have no effect on its current failed-login count or its locking state.
失败登录的跟踪与用于检查凭据的登录账户相关。如果使用了用户代理,跟踪会针对代理用户,而不是被代理用户。也就是说,跟踪与由USER()指示的账户相关,而不是由CURRENT_USER()指示的账户相关。有关代理用户和被代理用户之间区别的信息,请参见第 8.2.19 节,“代理用户”。Failed-login tracking is tied to the login account that is used
to check credentials. If user proxying is in use, tracking
occurs for the proxy user, not the proxied user. That is,
tracking is tied to the account indicated by
USER(), not the account indicated
by CURRENT_USER(). For
information about the distinction between proxy and proxied
users, see Section 8.2.19, “Proxy Users”.