文档首页
MySQL 8.4 参考手册
8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

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

打开官方英文原文 ↗

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

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

8.2.18 多因素认证8.2.18 Multifactor Authentication

认证涉及一方向另一方证明其身份以获得对方的认可。多因素认证(MFA)是在认证过程中使用多种认证值(或“因素”)。多因素认证比单因素认证(1FA/SFA)提供更高的安全性,单因素认证仅使用一种认证方法,例如密码。多因素认证允许使用额外的认证方法,例如使用多个密码进行认证,或者使用智能卡、安全密钥和生物识别设备等设备进行认证。Authentication involves one party establishing its identity to the satisfaction of a second party. Multifactor authentication (MFA) is the use of multiple authentication values (or “factors”) during the authentication process. MFA provides greater security than one-factor/single-factor authentication (1FA/SFA), which uses only one authentication method such as a password. MFA enables additional authentication methods, such as authentication using multiple passwords, or authentication using devices like smart cards, security keys, and biometric readers.

MySQL 包括对多因素认证的支持。此功能包括需要多达三个认证值的 MFA 形式。也就是说,MySQL 账户管理支持使用 2FA 或 3FA 的账户,除了现有的 1FA 支持之外。MySQL includes support for multifactor authentication. This capability includes forms of MFA that require up to three authentication values. That is, MySQL account management supports accounts that use 2FA or 3FA, in addition to the existing 1FA support.

当客户端尝试使用单因素账户连接 MySQL 服务器时,服务器会调用账户定义中指定的认证插件,并根据插件报告的成功或失败来接受或拒绝连接。When a client attempts a connection to the MySQL server using a single-factor account, the server invokes the authentication plugin indicated by the account definition and accepts or rejects the connection depending on whether the plugin reports success or failure.

对于具有多个认证因素的账户,过程类似。服务器按照账户定义中列出的顺序调用认证插件。如果插件报告成功,如果该插件是最后一个,则服务器接受连接;如果还有剩余插件,则继续调用下一个插件。如果任何插件报告失败,服务器将拒绝连接。For an account that has multiple authentication factors, the process is similar. The server invokes authentication plugins in the order listed in the account definition. If a plugin reports success, the server either accepts the connection if the plugin is the last one, or proceeds to invoke the next plugin if any remain. If any plugin reports failure, the server rejects the connection.

以下部分将更详细地介绍 MySQL 中的多因素认证。The following sections cover multifactor authentication in MySQL in more detail.

多因素身份验证支持的要素Elements of Multifactor Authentication Support

身份验证因素通常包括以下类型的信息:Authentication factors commonly include these types of information:

  • 你知道的东西,例如秘密密码或口令短语。Something you know, such as a secret password or passphrase.

  • 你拥有的东西,例如安全密钥或智能卡。Something you have, such as a security key or smart card.

  • 你自身的特征;也就是说,生物特征,例如指纹或面部扫描。Something you are; that is, a biometric characteristic such as a fingerprint or facial scan.

该“你知道的事情”因子类型依赖于在身份验证过程双方保密的信息。不幸的是,秘密可能会被泄露:有人可能会看到你输入密码,或者通过钓鱼攻击欺骗你,服务器端存储的密码可能因安全漏洞而暴露,等等。通过使用多个密码可以提高安全性,但每个密码仍可能被泄露。使用其他因子类型可以在降低泄露风险的情况下提高安全性。The “something you know” factor type relies on information that is kept secret on both sides of the authentication process. Unfortunately, secrets may be subject to compromise: Someone might see you enter your password or fool you with a phishing attack, a password stored on the server side might be exposed by a security breach, and so forth. Security can be improved by using multiple passwords, but each may still be subject to compromise. Use of the other factor types enables improved security with less risk of compromise.

在 MySQL 中实施多因素认证包括以下几个要素:Implementation of multifactor authentication in MySQL comprises these elements:

  • authentication_policy 系统变量控制可以使用多少种身份验证因素以及每个因素允许的身份验证类型。也就是说,它对 CREATE USER 和 ALTER USER 语句在多因素身份验证方面施加约束。The authentication_policy system variable controls how many authentication factors can be used and the types of authentication permitted for each factor. That is, it places constraints on CREATE USER and ALTER USER statements with respect to multifactor authentication.

  • CREATE USER 和 ALTER USER 的语法允许为新账户指定多种身份验证方法,以及为现有账户添加、修改或删除身份验证方法。如果账户使用 2FA 或 3FA,mysql.user 系统表会在 User_attributes 列中存储关于额外身份验证因素的信息。CREATE USER and ALTER USER have syntax enabling multiple authentication methods to be specified for new accounts, and for adding, modifying, or dropping authentication methods for existing accounts. If an account uses 2FA or 3FA, the mysql.user system table stores information about the additional authentication factors in the User_attributes column.

  • 为了使用需要多个密码的账户对 MySQL 服务器进行身份验证,客户端程序提供了--password1、--password2和--password3选项,允许指定最多三个密码。对于使用 C API 的应用程序,mysql_options4() C API 函数的MYSQL_OPT_USER_PASSWORD选项也支持相同的功能。To enable authentication to the MySQL server using accounts that require multiple passwords, client programs have --password1, --password2, and --password3 options that permit up to three passwords to be specified. For applications that use the C API, the MYSQL_OPT_USER_PASSWORD option for the mysql_options4() C API function enables the same capability.

  • 服务器端的 authentication_webauthn 插件支持使用设备进行身份验证。这个服务器端的基于设备的身份验证插件仅包含在 MySQL 企业版发行版中。在 MySQL 社区版发行版中不包含该插件。然而,客户端的 authentication_webauthn_client 插件包含在所有发行版中,包括社区版发行版。这使得来自任何发行版的客户端都可以连接到使用 authentication_webauthn 在加载该插件的服务器上进行身份验证的账户。参见 第 8.4.1.11 节,“WebAuthn 可插拔身份验证”。The server-side authentication_webauthn plugin enables authentication using devices. This server-side, device-based authentication plugin is included only in MySQL Enterprise Edition distributions. It is not included in MySQL community distributions. However, the client-side authentication_webauthn_client plugin is included in all distributions, including community distributions. This enables clients from any distribution to connect to accounts that use authentication_webauthn to authenticate on a server that has that plugin loaded. See Section 8.4.1.11, “WebAuthn Pluggable Authentication”.

  • authentication_webauthn 还可以启用无密码认证,如果它是账户唯一使用的认证插件。请参阅 WebAuthn 无密码认证。authentication_webauthn also enables passwordless authentication, if it is the only authentication plugin used by an account. See WebAuthn Passwordless Authentication.

  • 多因素身份验证可以使用非 WebAuthn 的 MySQL 身份验证方法、WebAuthn 身份验证方法,或两者的组合。Multifactor authentication can use non-WebAuthn MySQL authentication methods, the WebAuthn authentication method, or a combination of both.

  • 这些权限使用户能够执行某些受限的多因素身份验证相关操作:These privileges enable users to perform certain restricted multifactor authentication-related operations:

配置多因素认证策略Configuring the Multifactor Authentication Policy

authentication_policy 系统变量定义了多因素认证策略。具体来说,它定义了账户可以拥有(或需要拥有)的认证因素数量,以及每个因素可以使用的认证方法。The authentication_policy system variable defines the multifactor authentication policy. Specifically, it defines how many authentication factors accounts may have (or are required to have) and the authentication methods that can be used for each factor.

authentication_policy 的值是由 1、2 或 3 个逗号分隔的元素组成的列表。列表中的每个元素对应一个身份验证因素,可以是身份验证插件名称、星号(*)、空的或缺失的。(例外:元素 1 不能是空的或缺失的。)整个列表用单引号括起来。例如,以下 authentication_policy 值包括一个星号、一个身份验证插件名称以及一个空元素:The value of authentication_policy is a list of 1, 2, or 3 comma-separated elements. Each element in the list corresponds to an authentication factor and can be an authentication plugin name, an asterisk (*), empty, or missing. (Exception: Element 1 cannot be empty or missing.) The entire list is enclosed in single quotes. For example, the following authentication_policy value includes an asterisk, an authentication plugin name, and an empty element:

authentication_policy = '*,authentication_webauthn,'

星号(*)表示需要认证方法,但允许使用任何方法。空元素表示认证方法是可选的,且允许使用任何方法。缺失元素(没有星号、空元素或认证插件名称)表示不允许使用认证方法。当指定插件名称时,在创建或修改账户时,相应因素必须使用该认证方法。An asterisk (*) indicates that an authentication method is required but any method is permitted. An empty element indicates that an authentication method is optional and any method is permitted. A missing element (no asterisk, empty element, or authentication plugin name) indicates that an authentication method is not permitted. When a plugin name is specified, that authentication method is required for the respective factor when creating or modifying an account.

默认的 authentication_policy 值是 '*,,'(一个星号和两个空元素),这要求一个第一因素,并可选择地允许第二和第三因素。因此,默认的 authentication_policy 值与现有的 1FA 账户向后兼容,同时也允许创建或修改账户以使用 2FA 或 3FA。The default authentication_policy value is '*,,' (an asterisk and two empty elements), which requires a first factor, and optionally permits second and third factors. The default authentication_policy value is thus backward compatible with existing 1FA accounts, but also permits creation or modification of accounts to use 2FA or 3FA.

具有 AUTHENTICATION_POLICY_ADMIN 权限的用户不受 authentication_policy 设置施加的限制。(对于原本不允许的语句,将显示警告。)A user who has the AUTHENTICATION_POLICY_ADMIN privilege is not subject to the constraints imposed by the authentication_policy setting. (A warning occurs for statements that otherwise would not be permitted.)

authentication_policy 的值可以在选项文件中定义,或者使用 SET GLOBAL 语句指定:authentication_policy values can be defined in an option file or specified using a SET GLOBAL statement:

SET GLOBAL authentication_policy='*,*,';

有几条规则用来规范 authentication_policy 值的定义方式。有关这些规则的完整说明,请参阅 authentication_policy 系统变量描述。下表提供了一些 authentication_policy 示例值以及每个值所建立的策略。There are several rules that govern how the authentication_policy value can be defined. Refer to the authentication_policy system variable description for a compete account of those rules. The following table provides several authentication_policy example values and the policy established by each.

表 8.11 authentication_policy 示例值Table 8.11 Example authentication_policy Values

认证策略值authentication_policy Value 有效策略Effective Policy
'*' 仅允许使用单因素创建或更改账户。Permit only creating or altering accounts with one factor.
'*,*' 仅允许使用双因素创建或更改账户。Permit only creating or altering accounts with two factors.
'*,*,*' 仅允许使用三因素创建或更改账户。Permit only creating or altering accounts with three factors.
'*,' 允许使用一种或两种因素创建或更改账户。Permit creating or altering accounts with one or two factors.
'*,,' 允许使用一、二或三种因素创建或更改账户。Permit creating or altering accounts with one, two, or three factors.
'*,*,' 允许使用两种或三种因素创建或更改账户。Permit creating or altering accounts with two or three factors.
'*,auth_plugin' 允许使用两因素创建或修改账户,其中第一因素可以是任何认证方法,第二因素必须是指定的插件。Permit creating or altering accounts with two factors, where the first factor can be any authentication method, and the second factor must be the named plugin.
'auth_plugin,*,' 允许创建或更改具有两三个因素的帐户,其中第一个因素必须是指定的插件。Permit creating or altering accounts with two or three factors, where the first factor must be the named plugin.
'auth_plugin,' 允许创建或更改具有一因素或两因素的账户,其中第一个因素必须是指定的插件。Permit creating or altering accounts with one or two factors, where the first factor must be the named plugin.
'auth_plugin,auth_plugin,auth_plugin' 允许创建或更改具有三个因素的账户,这些因素必须使用指定的插件。Permits creating or altering accounts with three factors, where the factors must use the named plugins.

多因素认证入门Getting Started with Multifactor Authentication

默认情况下,MySQL 使用多因素认证策略,该策略允许第一因素使用任何认证插件,并可选择性地允许第二和第三认证因素。此策略是可配置的;有关详细信息,请参见 配置多因素认证策略。By default, MySQL uses a multifactor authentication policy that permits any authentication plugin for the first factor, and optionally permits second and third authentication factors. This policy is configurable; for details, see Configuring the Multifactor Authentication Policy.

Note

不允许使用任何内部凭证存储插件(caching_sha2_password 或 mysql_native_password)作为第二或第三因素。It is not permitted to use any internal credential storage plugins (caching_sha2_password or mysql_native_password) for factor 2 or 3.

假设您希望一个账户首先使用 caching_sha2_password 插件进行身份验证,然后使用 authentication_ldap_sasl SASL LDAP 插件进行身份验证。(这假设 LDAP 身份验证已经按照 第 8.4.1.7 节,“LDAP 可插拔认证” 中的描述设置完毕,并且该用户在 LDAP 目录中有与示例中显示的身份验证字符串相对应的条目。)使用如下语句创建账户:Suppose that you want an account to authenticate first using the caching_sha2_password plugin, then using the authentication_ldap_sasl SASL LDAP plugin. (This assumes that LDAP authentication is already set up as described in Section 8.4.1.7, “LDAP Pluggable Authentication”, and that the user has an entry in the LDAP directory corresponding to the authentication string shown in the example.) Create the account using a statement like this:

CREATE USER 'alice'@'localhost'
  IDENTIFIED WITH caching_sha2_password
    BY 'sha2_password'
  AND IDENTIFIED WITH authentication_ldap_sasl
    AS 'uid=u1_ldap,ou=People,dc=example,dc=com';

要连接,用户必须提供两个密码。为了使用需要多个密码的账户进行 MySQL 服务器认证,客户端程序提供了 --password1、--password2 和 --password3 选项,允许最多指定三个密码。这些选项类似于 --password 选项,可以在命令行中跟随选项提供密码值(不安全),或者如果没有提供密码值,则会提示用户输入密码。对于刚创建的账户,因素 1 和 2 需要密码,因此使用 mysql 客户端,并加上 --password1 和 --password2 选项。mysql 会依次提示输入每个密码:To connect, the user must supply two passwords. To enable authentication to the MySQL server using accounts that require multiple passwords, client programs have --password1, --password2, and --password3 options that permit up to three passwords to be specified. These options are similar to the --password option in that they can take a password value following the option on the command line (which is insecure) or if given without a password value cause the user to be prompted for one. For the account just created, factors 1 and 2 take passwords, so invoke the mysql client with the --password1 and --password2 options. mysql prompts for each password in turn:

$> mysql --user=alice --password1 --password2
Enter password: (enter factor 1 password)
Enter password: (enter factor 2 password)

假设您想添加第三个身份验证因素。这可以通过删除并重新创建带有第三个因素的用户来实现,或者通过使用 ALTER USER user ADD factor 语法来实现。以下展示了两种方法:Suppose you want to add a third authentication factor. This can be achieved by dropping and recreating the user with a third factor or by using ALTER USER user ADD factor syntax. Both methods are shown below:

DROP USER 'alice'@'localhost';
  
CREATE USER 'alice'@'localhost'
  IDENTIFIED WITH caching_sha2_password
    BY 'sha2_password'
  AND IDENTIFIED WITH authentication_ldap_sasl
    AS 'uid=u1_ldap,ou=People,dc=example,dc=com'
  AND IDENTIFIED WITH authentication_webauthn;

ADD factor 语法包括因素编号和 FACTOR 关键字:ADD factor syntax includes the factor number and FACTOR keyword:

ALTER USER 'alice'@'localhost' ADD 3 FACTOR IDENTIFIED WITH authentication_webauthn;

ALTER USER user DROP factor 语法允许删除一个因子。下面的例子删除了在前一个例子中添加的第三个因子(authentication_webauthn):ALTER USER user DROP factor syntax permits dropping a factor. The following example drops the third factor (authentication_webauthn) that was added in the previous example:

ALTER USER 'alice'@'localhost' DROP 3 FACTOR;

ALTER USER user MODIFY factor 语法允许更改特定因子的插件或认证字符串,前提是该因子存在。下面的例子修改了第二个因子,将认证方法从 authentication_ldap_sasl 改为 authetication_webauthn:ALTER USER user MODIFY factor syntax permits changing the plugin or authentication string for a particular factor, provided that the factor exists. The following example modifies the second factor, changing the authentication method from authentication_ldap_sasl to authetication_webauthn:

ALTER USER 'alice'@'localhost' MODIFY 2 FACTOR IDENTIFIED WITH authentication_webauthn;

使用 SHOW CREATE USER 查看为某个帐户定义的认证方法:Use SHOW CREATE USER to view the authentication methods defined for an account:

SHOW CREATE USER 'u1'@'localhost'\G
*************************** 1. row ***************************
CREATE USER for u1@localhost: CREATE USER `u1`@`localhost` 
IDENTIFIED WITH 'caching_sha2_password' AS 'sha2_password' 
AND IDENTIFIED WITH 'authentication_authn' REQUIRE NONE 
PASSWORD EXPIRE DEFAULT ACCOUNT UNLOCK PASSWORD HISTORY 
DEFAULT PASSWORD REUSE INTERVAL DEFAULT PASSWORD REQUIRE 
CURRENT DEFAULT