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

MySQL 8.4 参考手册 · 中文镜像

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

打开官方英文原文 ↗

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

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

8.2.3 授权表8.2.3 Grant Tables

mysql 系统数据库包含几个授权表,这些表包含有关用户账户及其所拥有权限的信息。本节介绍这些表。有关系统数据库中其他表的信息,请参阅 第 7.3 节,“mysql 系统模式”。The mysql system database includes several grant tables that contain information about user accounts and the privileges held by them. This section describes those tables. For information about other tables in the system database, see Section 7.3, “The mysql System Schema”.

这里的讨论描述了授权表的底层结构,以及服务器在与客户端交互时如何使用其内容。然而,通常情况下,你不会直接修改授权表。修改是通过使用账户管理语句间接发生的,比如 CREATE USER、GRANT 和 REVOKE 来设置账户并控制每个账户可拥有的权限。请参见 第 15.7.1 节,“账户管理语句”。当你使用这些语句执行账户操作时,服务器会代表你修改授权表。The discussion here describes the underlying structure of the grant tables and how the server uses their contents when interacting with clients. However, normally you do not modify the grant tables directly. Modifications occur indirectly when you use account-management statements such as CREATE USER, GRANT, and REVOKE to set up accounts and control the privileges available to each one. See Section 15.7.1, “Account Management Statements”. When you use such statements to perform account manipulations, the server modifies the grant tables on your behalf.

Note

不鼓励使用 INSERT、UPDATE 或 DELETE 等语句直接修改授权表,并且这样做风险自负。服务器可以忽略由于此类修改而导致格式错误的行。Direct modification of grant tables using statements such as INSERT, UPDATE, or DELETE is discouraged and done at your own risk. The server is free to ignore rows that become malformed as a result of such modifications.

对于任何修改授权表的操作,服务器会检查表是否具有预期的结构,如果不是,则会产生错误。要将表更新为预期结构,请执行 MySQL 升级过程。请参见 第 3 章,升级 MySQL。For any operation that modifies a grant table, the server checks whether the table has the expected structure and produces an error if not. To update the tables to the expected structure, perform the MySQL upgrade procedure. See Chapter 3, Upgrading MySQL.

授权表概览Grant Table Overview

这些 mysql 表包含授权信息:These mysql database tables contain grant information:

有关静态全局权限和动态全局权限之间差异的信息,请参见 静态与动态权限。For information about the differences between static and dynamic global privileges, see Static Versus Dynamic Privileges.)

授权表使用 InnoDB 存储引擎,并且是事务性的。每条语句要么对所有指定用户成功,要么在发生任何错误时回滚且不产生任何效果。Grant tables use the InnoDB storage engine and are transactional. Each statement either succeeds for all named users or rolls back and has no effect if any error occurs.

每个权限表包含作用域列和权限列:Each grant table contains scope columns and privilege columns:

  • 作用域列决定表中每行的作用范围;也就是该行适用的上下文。例如,一个 user 表行,如果 Host 和 User 的值分别是 'h1.example.net' 和 'bob',则适用于从主机 h1.example.net 发起连接并指定用户名为 bob 的客户端进行身份验证。同样,一个 db 表行,如果 Host、User 和 Db 的值分别为 'h1.example.net'、'bob' 和 'reports',则当 bob 从主机 h1.example.net 连接以访问 reports 数据库时生效。tables_priv 和 columns_priv 表包含作用域列,指示每行适用的表或表/列组合。procs_priv 的作用域列指示每行适用的存储过程。Scope columns determine the scope of each row in the tables; that is, the context in which the row applies. For example, a user table row with Host and User values of 'h1.example.net' and 'bob' applies to authenticating connections made to the server from the host h1.example.net by a client that specifies a user name of bob. Similarly, a db table row with Host, User, and Db column values of 'h1.example.net', 'bob' and 'reports' applies when bob connects from the host h1.example.net to access the reports database. The tables_priv and columns_priv tables contain scope columns indicating tables or table/column combinations to which each row applies. The procs_priv scope columns indicate the stored routine to which each row applies.

  • 特权列指示表行授予哪些特权;也就是说,它允许执行哪些操作。服务器结合各种授权表中的信息来形成对用户特权的完整描述。第 8.2.7 节,“访问控制,第 2 阶段:请求验证”,描述了相关规则。Privilege columns indicate which privileges a table row grants; that is, which operations it permits to be performed. The server combines the information in the various grant tables to form a complete description of a user's privileges. Section 8.2.7, “Access Control, Stage 2: Request Verification”, describes the rules for this.

此外,权限表可能包含用于范围或权限评估以外目的的列。In addition, a grant table may contain columns used for purposes other than scope or privilege assessment.

服务器以以下方式使用权限表:The server uses the grant tables in the following manner:

  • user 表的作用域列决定是否拒绝或允许传入连接。对于允许的连接,在 user 表中授予的任何权限表示用户的静态全局权限。在此表中授予的任何权限适用于服务器上的 所有 数据库。The user table scope columns determine whether to reject or permit incoming connections. For permitted connections, any privileges granted in the user table indicate the user's static global privileges. Any privileges granted in this table apply to all databases on the server.

    Caution

    因为任何静态全局权限都被视为对所有数据库的权限,所以任何静态全局权限都可以使用户通过 SHOW DATABASES 查看所有数据库名称,或通过检查 SCHEMATA 表(位于 INFORMATION_SCHEMA 中)来查看所有数据库名称,但被数据库级别的部分撤销限制的数据库除外。Because any static global privilege is considered a privilege for all databases, any static global privilege enables a user to see all database names with SHOW DATABASES or by examining the SCHEMATA table of INFORMATION_SCHEMA, except databases that have been restricted at the database level by partial revokes.

  • global_grants 表列出动态全局权限当前分配给用户账户的情况。对于每一行,作用域列决定哪个用户拥有权限列中指定的权限。The global_grants table lists current assignments of dynamic global privileges to user accounts. For each row, the scope columns determine which user has the privilege named in the privilege column.

  • db 表的作用域列决定哪些用户可以从哪些主机访问哪些数据库。权限列决定允许的操作。在数据库级别授予的权限适用于该数据库及数据库中的所有对象,例如表和存储程序。The db table scope columns determine which users can access which databases from which hosts. The privilege columns determine the permitted operations. A privilege granted at the database level applies to the database and to all objects in the database, such as tables and stored programs.

  • tables_priv 和 columns_priv 表类似于 db 表,但粒度更细:它们作用于表和列级别,而不是数据库级别。在表级别授予的权限适用于该表及其所有列。在列级别授予的权限仅适用于特定列。The tables_priv and columns_priv tables are similar to the db table, but are more fine-grained: They apply at the table and column levels rather than at the database level. A privilege granted at the table level applies to the table and to all its columns. A privilege granted at the column level applies only to a specific column.

  • procs_priv 表适用于存储过程(存储函数和过程)。在例程级别授予的权限仅适用于单个存储过程或函数。The procs_priv table applies to stored routines (stored procedures and functions). A privilege granted at the routine level applies only to a single procedure or function.

  • proxies_priv 表指示哪些用户可以作为其他用户的代理,以及用户是否可以将 PROXY 权限授予其他用户。The proxies_priv table indicates which users can act as proxies for other users and whether a user can grant the PROXY privilege to other users.

  • default_roles 和 role_edges 表包含角色关系的信息。The default_roles and role_edges tables contain information about role relationships.

  • password_history 表保留先前选择的密码,以实现对密码重用的限制。参见 第 8.2.15 节,“密码管理”。The password_history table retains previously chosen passwords to enable restrictions on password reuse. See Section 8.2.15, “Password Management”.

服务器在启动时会将授权表的内容读取到内存中。你可以通过执行FLUSH PRIVILEGES语句,或者执行mysqladmin flush-privileges或mysqladmin reload命令来让它重新加载表。对授权表的更改会如第 8.2.13 节,“权限更改何时生效”中所述生效。The server reads the contents of the grant tables into memory when it starts. You can tell it to reload the tables by issuing a FLUSH PRIVILEGES statement or executing a mysqladmin flush-privileges or mysqladmin reload command. Changes to the grant tables take effect as indicated in Section 8.2.13, “When Privilege Changes Take Effect”.

当您修改一个账户时,最好验证您的更改是否达到了预期效果。要检查给定账户的权限,请使用SHOW GRANTS语句。例如,要确定用户名和主机名分别为bob和pc84.example.com的账户所拥有的权限,请使用以下语句:When you modify an account, it is a good idea to verify that your changes have the intended effect. To check the privileges for a given account, use the SHOW GRANTS statement. For example, to determine the privileges that are granted to an account with user name and host name values of bob and pc84.example.com, use this statement:

SHOW GRANTS FOR 'bob'@'pc84.example.com';

要显示账户的非权限属性,请使用SHOW CREATE USER:To display nonprivilege properties of an account, use SHOW CREATE USER:

SHOW CREATE USER 'bob'@'pc84.example.com';

用户和数据库授权表The user and db Grant Tables

服务器在访问控制的第一阶段和第二阶段都使用 mysql 数据库中的 user 和 db 表(参见 第 8.2 节,“访问控制和账户管理”)。user 和 db 表中的列如下所示。The server uses the user and db tables in the mysql database at both the first and second stages of access control (see Section 8.2, “Access Control and Account Management”). The columns in the user and db tables are shown here.

表 8.4 user 和 db 表列Table 8.4 user and db Table Columns

表名Table Name useruser dbdb
范围列Scope columns HostHost HostHost
UserUser DbDb
UserUser
权限列Privilege columns Select_priv Select_priv
Insert_priv Insert_priv
Update_privUpdate_priv Update_privUpdate_priv
Delete_priv Delete_priv
Index_priv Index_priv
Alter_priv Alter_priv
Create_privCreate_priv Create_privCreate_priv
Drop_priv Drop_priv
Grant_privGrant_priv Grant_privGrant_priv
Create_view_priv Create_view_priv
Show_view_privShow_view_priv Show_view_privShow_view_priv
Create_routine_privCreate_routine_priv Create_routine_privCreate_routine_priv
Alter_routine_priv Alter_routine_priv
Execute_privExecute_priv Execute_privExecute_priv
Trigger_privTrigger_priv Trigger_privTrigger_priv
Event_privEvent_priv Event_privEvent_priv
Create_tmp_table_privCreate_tmp_table_priv Create_tmp_table_privCreate_tmp_table_priv
Lock_tables_priv Lock_tables_priv
References_priv References_priv
Reload_priv
Shutdown_privShutdown_priv
Process_priv
File_privFile_priv
Show_db_priv
Super_privSuper_priv
Repl_slave_priv
Repl_client_priv
Create_user_privCreate_user_priv
Create_tablespace_privCreate_tablespace_priv
Create_role_privCreate_role_priv
Drop_role_privDrop_role_priv
安全列Security columns ssl_typessl_type
ssl_cipherssl_cipher
x509_issuerx509_issuer
x509_subjectx509_subject
pluginplugin
authentication_stringauthentication_string
password_expiredpassword_expired
password_last_changedpassword_last_changed
password_lifetimepassword_lifetime
account_lockedaccount_locked
Password_reuse_historyPassword_reuse_history
Password_reuse_timePassword_reuse_time
Password_require_currentPassword_require_current
User_attributesUser_attributes
资源控制列Resource control columns max_questionsmax_questions
max_updatesmax_updates
max_connectionsmax_connections
max_user_connectionsmax_user_connections

user 表中的 plugin 和 authentication_string 列存储认证插件和凭证信息。The user table plugin and authentication_string columns store authentication plugin and credential information.

服务器使用账户行中 plugin 列中指定的插件来验证该账户的连接尝试。The server uses the plugin named in the plugin column of an account row to authenticate connection attempts for the account.

plugin 列不能为空。在启动时,以及在执行 FLUSH PRIVILEGES 时,服务器会检查 user 表中的行。对于任何 plugin 列为空的行,服务器会在错误日志中写入如下形式的警告:The plugin column must be nonempty. At startup, and at runtime when FLUSH PRIVILEGES is executed, the server checks user table rows. For any row with an empty plugin column, the server writes a warning to the error log of this form:

[Warning] User entry 'user_name'@'host_name' has an empty plugin
value. The user will be ignored and no one can login with this user
anymore.

要为缺少插件的账户分配插件,请使用 ALTER USER 语句。To assign a plugin to an account that is missing one, use the ALTER USER statement.

password_expired 列允许数据库管理员使账户密码过期,并要求用户重置他们的密码。默认的 password_expired 值为 'N',但可以通过 ALTER USER 语句将其设置为 'Y'。在账户密码过期后,该账户在随后的服务器连接中执行的所有操作都会导致错误,直到用户执行 ALTER USER 语句以设置新的账户密码。The password_expired column permits DBAs to expire account passwords and require users to reset their password. The default password_expired value is 'N', but can be set to 'Y' with the ALTER USER statement. After an account's password has been expired, all operations performed by the account in subsequent connections to the server result in an error until the user issues an ALTER USER statement to establish a new account password.

Note

虽然可以通过将过期密码重置为当前值来“重置”密码,但作为良好策略,更好的做法是选择一个不同的密码。数据库管理员可以通过建立适当的密码重用政策来强制不重复使用。请参阅密码重用政策.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.

password_last_changed 是一个 TIMESTAMP 列,用于表示密码上次更改的时间。仅对于使用 MySQL 内置身份验证插件的账户(已弃用的 mysql_native_password、已弃用的 sha256_password 或 caching_sha2_password)此值才非 NULL。对于其他账户,例如使用外部身份验证系统认证的账户,此值为 NULL。password_last_changed is a TIMESTAMP column indicating when the password was last changed. The value is non-NULL only for accounts that use a MySQL built-in authentication plugin (mysql_native_password which is deprecated, sha256_password which is deprecated, or caching_sha2_password). The value is NULL for other accounts, such as those authenticated using an external authentication system.

password_last_changed 会被 CREATE USER、ALTER USER 和 SET PASSWORD 语句更新,也会被 GRANT 语句更新,这些语句会创建账户或更改账户密码。password_last_changed is updated by the CREATE USER, ALTER USER, and SET PASSWORD statements, and by GRANT statements that create an account or change an account password.

password_lifetime 表示账户密码的有效期(以天为单位)。如果密码已超过其有效期(通过 password_last_changed 列进行评估),当客户端使用该账户连接时,服务器会认为密码已过期。大于零的 N 值意味着密码必须每 N 天更改一次。值为 0 表示禁用自动密码过期。如果值为 NULL(默认值),则应用全局过期策略,该策略由 default_password_lifetime 系统变量定义。password_lifetime indicates the account password lifetime, in days. If the password is past its lifetime (assessed using the password_last_changed column), the server considers the password expired when clients connect using the account. A value of N greater than zero means that the password must be changed every N days. A value of 0 disables automatic password expiration. If the value is NULL (the default), the global expiration policy applies, as defined by the default_password_lifetime system variable.

account_locked 表示账户是否被锁定(见 第 8.2.20 节,“账户锁定”)。account_locked indicates whether the account is locked (see Section 8.2.20, “Account Locking”).

Password_reuse_history 是账户的 PASSWORD HISTORY 选项的值,或者对于默认历史为 NULL。Password_reuse_history is the value of the PASSWORD HISTORY option for the account, or NULL for the default history.

Password_reuse_time 是账户的 PASSWORD REUSE INTERVAL 选项的值,或者对于默认间隔为 NULL。Password_reuse_time is the value of the PASSWORD REUSE INTERVAL option for the account, or NULL for the default interval.

Password_require_current 对应账户的 PASSWORD REQUIRE 选项的值,如下表所示。Password_require_current corresponds to the value of the PASSWORD REQUIRE option for the account, as shown by the following table.

表 8.5 允许的 Password_require_current 值Table 8.5 Permitted Password_require_current Values

密码_需要当前值Password_require_current Value 对应的密码要求选项Corresponding PASSWORD REQUIRE Option
'Y' PASSWORD REQUIRE CURRENT
'N' PASSWORD REQUIRE CURRENT OPTIONAL
NULL PASSWORD REQUIRE CURRENT DEFAULT

User_attributes 是一个 JSON 格式的列,用于存储未存储在其他列中的账户属性。INFORMATION_SCHEMA 通过 USER_ATTRIBUTES 表公开这些属性。User_attributes is a JSON-format column that stores account attributes not stored in other columns. The INFORMATION_SCHEMA exposes these attributes through the USER_ATTRIBUTES table.

User_attributes 列可能包含以下属性:The User_attributes column may contain these attributes:

  • additional_password:次要密码(如有)。请参阅 双重密码支持。additional_password: The secondary password, if any. See Dual Password Support.

  • Restrictions:限制列表(如有)。限制是通过部分撤销操作添加的。该属性的值是一个元素数组,每个元素具有 Database 和 Restrictions 键,指示受限制数据库的名称及其适用的限制(参见 第 8.2.12 节,“使用部分撤销的权限限制”)。Restrictions: Restriction lists, if any. Restrictions are added by partial-revoke operations. The attribute value is an array of elements that each have Database and Restrictions keys indicating the name of a restricted database and the applicable restrictions on it (see Section 8.2.12, “Privilege Restriction Using Partial Revokes”).

  • Password_locking:用于跟踪登录失败和临时锁定账户的条件(如果有)(请参见 登录失败跟踪和临时账户锁定)。Password_locking 属性根据 CREATE USER 和 ALTER USER 语句中的 FAILED_LOGIN_ATTEMPTS 和 PASSWORD_LOCK_TIME 选项进行更新。该属性值为一个哈希,其中包含 failed_login_attempts 和 password_lock_time_days 键,表示为该账户指定的选项值。如果某个键缺失,则其值默认为 0。如果键值隐式或显式为 0,则对应功能被禁用。Password_locking: The conditions for failed-login tracking and temporary account locking, if any (see Failed-Login Tracking and Temporary Account Locking). The Password_locking attribute is updated according to the FAILED_LOGIN_ATTEMPTS and PASSWORD_LOCK_TIME options of the CREATE USER and ALTER USER statements. The attribute value is a hash with failed_login_attempts and password_lock_time_days keys indicating the value of such options as have been specified for the account. If a key is missing, its value is implicitly 0. If a key value is implicitly or explicitly 0, the corresponding capability is disabled.

  • multi_factor_authentication:mysql.user 系统表中的行有一个 plugin 列,用于指示身份验证插件。对于单因素认证,该插件就是唯一的身份验证因素。对于双因素或三因素形式的多因素认证,该插件对应第一个身份验证因素,但第二和第三因素的附加信息必须存储。multi_factor_authentication 属性保存这些信息。multi_factor_authentication: Rows in the mysql.user system table have a plugin column that indicates an authentication plugin. For single-factor authentication, that plugin is the only authentication factor. For two-factor or three-factor forms of multifactor authentication, that plugin corresponds to the first authentication factor, but additional information must be stored for the second and third factors. The multi_factor_authentication attribute holds this information.

    multi_factor_authentication 值是一个数组,其中每个数组元素是一个哈希表,使用以下属性描述一个身份验证因素:The multi_factor_authentication value is an array, where each array element is a hash that describes an authentication factor using these attributes:

    • plugin:身份验证插件的名称。plugin: The name of the authentication plugin.

    • authentication_string:身份验证字符串的值。authentication_string: The authentication string value.

    • passwordless:一个标志,表示用户是否打算在没有密码的情况下使用(以安全令牌作为唯一的认证方法)。passwordless: A flag that denotes whether the user is meant to be used without a password (with a security token as the only authentication method).

    • requires_registration:一个标志,用于定义用户账户是否已注册安全令牌。requires_registration: a flag that defines whether the user account has registered a security token.

    第一个和第二个数组元素描述多因素认证因素 2 和 3。The first and second array elements describe multifactor authentication factors 2 and 3.

如果没有适用的属性,User_attributes 为 NULL。If no attributes apply, User_attributes is NULL.

示例:一个拥有二级密码且部分撤销数据库权限的账户,其列值中包含 additional_password 和 Restrictions 属性:Example: An account that has a secondary password and partially revoked database privileges has additional_password and Restrictions attributes in the column value:

mysql> SELECT User_attributes FROM mysql.User WHERE User = 'u'\G
*************************** 1. row ***************************
User_attributes: {"Restrictions":
                   [{"Database": "mysql", "Privileges": ["SELECT"]}],
                  "additional_password": "hashed_credentials"}

要确定存在哪些属性,请使用 JSON_KEYS() 函数:To determine which attributes are present, use the JSON_KEYS() function:

SELECT User, Host, JSON_KEYS(User_attributes)
FROM mysql.user WHERE User_attributes IS NOT NULL;

要提取特定属性,例如 Restrictions,可以这样做:To extract a particular attribute, such as Restrictions, do this:

SELECT User, Host, User_attributes->>'$.Restrictions'
FROM mysql.user WHERE User_attributes->>'$.Restrictions' <> '';

以下是存储 multi_factor_authentication 的信息示例:Here is an example of the kind of information stored for multi_factor_authentication:

{
  "multi_factor_authentication": [
    {
      "plugin": "authentication_ldap_simple",
      "passwordless": 0,
      "authentication_string": "ldap auth string",
      "requires_registration": 0
    },
    {
      "plugin": "authentication_webauthn",
      "passwordless": 0,
      "authentication_string": "",
      "requires_registration": 1
    }
  ]
}

tables_priv 和 columns_priv 授权表The tables_priv and columns_priv Grant Tables

在访问控制的第二阶段,服务器会执行请求验证,以确保每个客户端对其发出的每个请求具有足够的权限。除了user和db授权表之外,对于涉及表的请求,服务器还可能查询tables_priv和columns_priv表。后者表在表和列级别提供了更精细的权限控制。它们具有如下表所示的列。During the second stage of access control, the server performs request verification to ensure that each client has sufficient privileges for each request that it issues. In addition to the user and db grant tables, the server may also consult the tables_priv and columns_priv tables for requests that involve tables. The latter tables provide finer privilege control at the table and column levels. They have the columns shown in the following table.

表 8.6 tables_priv 和 columns_priv 表列Table 8.6 tables_priv and columns_priv Table Columns

表名Table Name tables_priv columns_priv
范围列Scope columns HostHost HostHost
DbDb DbDb
UserUser UserUser
Table_name Table_name
Column_name
权限列Privilege columns Table_priv Column_priv
Column_priv
其他列Other columns TimestampTimestamp TimestampTimestamp
GrantorGrantor

Timestamp 和 Grantor 列分别设置为当前时间戳和 CURRENT_USER 值,但在其他方面未被使用。The Timestamp and Grantor columns are set to the current timestamp and the CURRENT_USER value, respectively, but are otherwise unused.

procs_priv 授权表The procs_priv Grant Table

为了验证涉及存储例程的请求,服务器可能会查询 procs_priv 表,该表具有如下表中所示的列。For verification of requests that involve stored routines, the server may consult the procs_priv table, which has the columns shown in the following table.

表 8.7 procs_priv 表列Table 8.7 procs_priv Table Columns

表名Table Name procs_priv
范围列Scope columns HostHost
DbDb
UserUser
Routine_nameRoutine_name
Routine_typeRoutine_type
权限列Privilege columns Proc_priv
其他列Other columns TimestampTimestamp
GrantorGrantor

Routine_type 列是一个 ENUM 列,其值为 'FUNCTION' 或 'PROCEDURE',用于指示该行所指的例程类型。此列允许对具有相同名称的函数和存储过程分别授予权限。The Routine_type column is an ENUM column with values of 'FUNCTION' or 'PROCEDURE' to indicate the type of routine the row refers to. This column enables privileges to be granted separately for a function and a procedure with the same name.

Timestamp 和 Grantor 列未被使用。The Timestamp and Grantor columns are unused.

proxies_priv 授权表The proxies_priv Grant Table

proxies_priv 表记录有关代理账户的信息。它具有以下列:The proxies_priv table records information about proxy accounts. It has these columns:

  • Host,User:代理账户;即拥有被代理账户的 PROXY 权限的账户。Host, User: The proxy account; that is, the account that has the PROXY privilege for the proxied account.

  • Proxied_host,Proxied_user:被代理账户。Proxied_host, Proxied_user: The proxied account.

  • Grantor,Timestamp:未使用。Grantor, Timestamp: Unused.

  • With_grant:代理账户是否可以将 PROXY 权限授予其他账户。With_grant: Whether the proxy account can grant the PROXY privilege to other accounts.

为了使一个账户能够将 PROXY 权限授予其他账户,该账户必须在 proxies_priv 表中有一行,并且 With_grant 设置为 1,同时 Proxied_host 和 Proxied_user 设置为指示可以授予权限的账户或账户组。例如,在 MySQL 安装过程中创建的 'root'@'localhost' 账户在 proxies_priv 表中有一行,它允许为 ''@'' 授予 PROXY 权限,也就是对所有用户和所有主机。这使得 root 可以设置代理用户,并且可以将设置代理用户的权限委托给其他账户。参见 第 8.2.19 节,“代理用户”。For an account to be able to grant the PROXY privilege to other accounts, it must have a row in the proxies_priv table with With_grant set to 1 and Proxied_host and Proxied_user set to indicate the account or accounts for which the privilege can be granted. For example, the 'root'@'localhost' account created during MySQL installation has a row in the proxies_priv table that enables granting the PROXY privilege for ''@'', that is, for all users and all hosts. This enables root to set up proxy users, as well as to delegate to other accounts the authority to set up proxy users. See Section 8.2.19, “Proxy Users”.

global_grants 授权表The global_grants Grant Table

global_grants 表列出了动态全局权限当前分配给用户帐户的情况。该表具有以下列:The global_grants table lists current assignments of dynamic global privileges to user accounts. The table has these columns:

  • USER, HOST:授予权限的帐户的用户名和主机名。USER, HOST: The user name and host name of the account to which the privilege is granted.

  • PRIV:权限名称。PRIV: The privilege name.

  • WITH_GRANT_OPTION:该帐户是否可以将权限授予其他帐户。WITH_GRANT_OPTION: Whether the account can grant the privilege to other accounts.

default_roles 授权表The default_roles Grant Table

default_roles 表列出了默认用户角色。它具有以下列:The default_roles table lists default user roles. It has these columns:

  • HOST, USER:默认角色适用的帐户或角色。HOST, USER: The account or role to which the default role applies.

  • DEFAULT_ROLE_HOST, DEFAULT_ROLE_USER:默认角色。DEFAULT_ROLE_HOST, DEFAULT_ROLE_USER: The default role.

role_edges 授权表The role_edges Grant Table

role_edges 表列出了角色子图的边。它有以下列:The role_edges table lists edges for role subgraphs. It has these columns:

  • FROM_HOST, FROM_USER:被授予角色的账户。FROM_HOST, FROM_USER: The account that is granted a role.

  • TO_HOST, TO_USER:授予账户的角色。TO_HOST, TO_USER: The role that is granted to the account.

  • WITH_ADMIN_OPTION:该账户是否可以使用 WITH ADMIN OPTION 将角色授予或撤销其他账户。WITH_ADMIN_OPTION: Whether the account can grant the role to and revoke it from other accounts by using WITH ADMIN OPTION.

password_history 授权表The password_history Grant Table

password_history 表包含密码更改信息。它有以下列:The password_history table contains information about password changes. It has these columns:

  • Host, User:进行密码更改的账户。Host, User: The account for which the password change occurred.

  • Password_timestamp:密码更改发生的时间。Password_timestamp: The time when the password change occurred.

  • Password:新的密码哈希值。Password: The new password hash value.

password_history 表为每个账户累积足够数量的非空密码,以使 MySQL 能够检查账户密码历史长度和重用间隔。在发生密码更改尝试时,对超出两个限制的条目会自动进行裁剪。The password_history table accumulates a sufficient number of nonempty passwords per account to enable MySQL to perform checks against both the account password history length and reuse interval. Automatic pruning of entries that are outside both limits occurs when password-change attempts occur.

Note

空密码不计入密码历史记录,可以随时重复使用。The empty password does not count in the password history and is subject to reuse at any time.

如果一个账户被重命名,它的条目也会被重命名以匹配。如果一个账户被删除或者其认证插件被更改,它的条目将被移除。If an account is renamed, its entries are renamed to match. If an account is dropped or its authentication plugin is changed, its entries are removed.

授权表范围列属性Grant Table Scope Column Properties

授权表中的范围列包含字符串。每列的默认值是空字符串。下表显示了每列允许的字符数。Scope columns in the grant tables contain strings. The default value for each is the empty string. The following table shows the number of characters permitted in each column.

表 8.8 授权表范围列长度Table 8.8 Grant Table Scope Column Lengths

列名Column Name 允许的最大字符数Maximum Permitted Characters
Host, Proxied_hostHost, Proxied_host 255
User, Proxied_userUser, Proxied_user 32
DbDb 64
Table_name 64
Column_name 64
Routine_nameRoutine_name 64

Host 和 Proxied_host 的值在存储到授权表之前会被转换为小写。Host and Proxied_host values are converted to lowercase before being stored in the grant tables.

为了访问检查的目的,User、Proxied_user、authentication_string、Db 和 Table_name 值的比较是区分大小写的。Host、Proxied_host、Column_name 和 Routine_name 值的比较不区分大小写。For access-checking purposes, comparisons of User, Proxied_user, authentication_string, Db, and Table_name values are case-sensitive. Comparisons of Host, Proxied_host, Column_name, and Routine_name values are not case-sensitive.

授权表权限列属性Grant Table Privilege Column Properties

user 和 db 表在各自的列中列出了每个权限,这些列声明为 ENUM('N','Y') DEFAULT 'N'。换句话说,每个权限可以被禁用或启用,默认是禁用的。The user and db tables list each privilege in a separate column that is declared as ENUM('N','Y') DEFAULT 'N'. In other words, each privilege can be disabled or enabled, with the default being disabled.

tables_priv、columns_priv 和 procs_priv 表将权限列声明为 SET 类型的列。这些列中的值可以包含该表控制的权限的任何组合。只有列值中列出的权限才是启用的。The tables_priv, columns_priv, and procs_priv tables declare the privilege columns as SET columns. Values in these columns can contain any combination of the privileges controlled by the table. Only those privileges listed in the column value are enabled.

表 8.9 Set 类型权限列值Table 8.9 Set-Type Privilege Column Values

表名Table Name 列名Column Name 可能的集合元素Possible Set Elements
tables_priv Table_priv 'Select', 'Insert', 'Update', 'Delete', 'Create', 'Drop', 'Grant', 'References', 'Index', 'Alter', 'Create View', 'Show view', 'Trigger''Select', 'Insert', 'Update', 'Delete', 'Create', 'Drop', 'Grant', 'References', 'Index', 'Alter', 'Create View', 'Show view', 'Trigger'
tables_priv Column_priv 'Select', 'Insert', 'Update', 'References''Select', 'Insert', 'Update', 'References'
columns_priv Column_priv 'Select', 'Insert', 'Update', 'References''Select', 'Insert', 'Update', 'References'
procs_priv Proc_priv 'Execute', 'Alter Routine', 'Grant''Execute', 'Alter Routine', 'Grant'

只有 user 和 global_grants 表指定管理权限,例如 RELOAD、SHUTDOWN 和 SYSTEM_VARIABLES_ADMIN。管理操作是针对服务器本身的操作,而不是特定数据库的操作,因此没有必要在其他授权表中列出这些权限。因此,服务器只需要查询 user 和 global_grants 表,就可以确定用户是否可以执行管理操作。Only the user and global_grants tables specify administrative privileges, such as RELOAD, SHUTDOWN, and SYSTEM_VARIABLES_ADMIN. Administrative operations are operations on the server itself and are not database-specific, so there is no reason to list these privileges in the other grant tables. Consequently, the server need consult only the user and global_grants tables to determine whether a user can perform an administrative operation.

FILE 权限也仅在 user 表中指定。它本身不是管理权限,但用户在服务器主机上读取或写入文件的能力与所访问的数据库无关。The FILE privilege also is specified only in the user table. It is not an administrative privilege as such, but a user's ability to read or write files on the server host is independent of the database being accessed.

授权表并发性Grant Table Concurrency

为了允许在 MySQL 授予表上同时进行 DML 和 DDL 操作,之前在 MySQL 授予表上获得行锁的读取操作将作为非锁定读取执行。在 MySQL 授予表上执行为非锁定读取的操作包括:To permit concurrent DML and DDL operations on MySQL grant tables, read operations that previously acquired row locks on MySQL grant tables are executed as non-locking reads. Operations that are performed as non-locking reads on MySQL grant tables include:

  • SELECT 语句以及其他通过连接列表和子查询从授权表中读取数据的只读语句,包括 SELECT ... FOR SHARE 语句,可使用任何事务隔离级别。SELECT statements and other read-only statements that read data from grant tables through join lists and subqueries, including SELECT ... FOR SHARE statements, using any transaction isolation level.

  • DML 操作从权限表中读取数据(通过连接列表或子查询),但不修改它们,使用任何事务隔离级别。DML operations that read data from grant tables (through join lists or subqueries) but do not modify them, using any transaction isolation level.

当使用基于语句的复制执行时,从权限表读取数据的语句如果不再获取行锁,将报告警告。Statements that no longer acquire row locks when reading data from grant tables report a warning if executed while using statement-based replication.

当使用-binlog_format=mixed时,从授权表读取数据的 DML 操作会以行事件的形式写入二进制日志,以确保操作在混合模式复制下的安全性。When using -binlog_format=mixed, DML operations that read data from grant tables are written to the binary log as row events to make the operations safe for mixed-mode replication.

SELECT ... FOR SHARE语句读取授权表的数据时会报告警告。使用FOR SHARE 子句时,不支持对授权表加读锁。SELECT ... FOR SHARE statements that read data from grant tables report a warning. With the FOR SHARE clause, read locks are not supported on grant tables.

使用 SERIALIZABLE 隔离级别执行从授权表读取数据的 DML 操作会报告警告。通常在使用 SERIALIZABLE 隔离级别时会获取的读取锁在授权表上不受支持。DML operations that read data from grant tables and are executed using the SERIALIZABLE isolation level report a warning. Read locks that would normally be acquired when using the SERIALIZABLE isolation level are not supported on grant tables.