6.2.7 Access Control, Stage 2: Request Verification访问控制,第2阶段:请求验证

After the server accepts a connection, it enters Stage 2 of access control. For each request that you issue through the connection, the server determines what operation you want to perform, then checks whether your privileges are sufficient. 在服务器接受连接后,它进入访问控制的第二阶段。对于通过连接发出的每个请求,服务器都会确定要执行的操作,然后检查您的权限是否足够。This is where the privilege columns in the grant tables come into play. 这就是授权表中的特权列发挥作用的地方。These privileges can come from any of the user, global_grants, db, tables_priv, columns_priv, or procs_priv tables. 这些特权可以来自任何userglobal_grantsdbtables_privcolumns_privprocs_priv表。(You may find it helpful to refer to Section 6.2.3, “Grant Tables”, which lists the columns present in each grant table.)(您可能会发现参考第6.2.3节,“拨款表”会有所帮助,该节列出了每个拨款表中存在的列。)

The user and global_grants tables grant global privileges. userglobal_grants表授予全局权限。The rows in these tables for a given account indicate the account privileges that apply on a global basis no matter what the default database is. 这些表中给定帐户的行表示在全局基础上应用的帐户权限,无论默认数据库是什么。For example, if the user table grants you the DELETE privilege, you can delete rows from any table in any database on the server host. 例如,如果user表授予您DELETE权限,则可以从服务器主机上任何数据库中的任何表中删除行。It is wise to grant privileges in the user table only to people who need them, such as database administrators. 明智的做法是只向需要权限的人(如数据库管理员)授予user表中的权限。For other users, leave all privileges in the user table set to 'N' and grant privileges at more specific levels only (for particular databases, tables, columns, or routines). 对于其他用户,将user表中的所有权限设置为'N',并仅在更具体的级别授予权限(对于特定的数据库、表、列或例程)。It is also possible to grant database privileges globally but use partial revokes to restrict them from being exercised on specific databases (see Section 6.2.12, “Privilege Restriction Using Partial Revokes”).也可以全局授予数据库权限,但使用部分撤销来限制它们在特定数据库上的行使(参阅第6.2.12节,“使用部分撤销的权限限制”)。

The db table grants database-specific privileges. Values in the scope columns of this table can take the following forms:db表授予数据库特定的权限。此表范围列中的值可以采用以下形式:

The server reads the db table into memory and sorts it at the same time that it reads the user table. 服务器将db表读入内存,并在读取user表的同时对其进行排序。The server sorts the db table based on the Host, Db, and User scope columns. 服务器根据HostDbUser作用域列对db表进行排序。As with the user table, sorting puts the most-specific values first and least-specific values last, and when the server looks for matching rows, it uses the first match that it finds.user表一样,排序将最具体的值放在第一位,最不具体的值排在最后,当服务器查找匹配的行时,它使用找到的第一个匹配项。

The tables_priv, columns_priv, and procs_priv tables grant table-specific, column-specific, and routine-specific privileges. tables_privcolumns_privprocs_priv表授予特定于表、特定于列和特定于例程的权限。Values in the scope columns of these tables can take the following forms:这些表的作用域列中的值可以采用以下形式:

The server sorts the tables_priv, columns_priv, and procs_priv tables based on the Host, Db, and User columns. 服务器根据HostDbUser列对tables_privcolumns_privprocs_priv表进行排序。This is similar to db table sorting, but simpler because only the Host column can contain wildcards.这类似于db表排序,但更简单,因为只有Host列可以包含通配符。

The server uses the sorted tables to verify each request that it receives. 服务器使用排序后的表来验证它收到的每个请求。For requests that require administrative privileges such as SHUTDOWN or RELOAD, the server checks only the user and global_privilege tables because those are the only tables that specify administrative privileges. 对于需要管理权限(如SHUTDOWNRELOAD)的请求,服务器只检查userglobal_privilege表,因为这些表是唯一指定管理权限的表。The server grants access if a row for the account in those tables permits the requested operation and denies access otherwise. 如果这些表中帐户的行允许所请求的操作,则服务器授予访问权限,否则拒绝访问。For example, if you want to execute mysqladmin shutdown but your user table row does not grant the SHUTDOWN privilege to you, the server denies access without even checking the db table. 例如,如果您想执行mysqladmin shutdown,但您的user表行没有授予您SHUTDOWN权限,则服务器会拒绝访问,甚至不会检查db表。(The latter table contains no Shutdown_priv column, so there is no need to check it.)(后一个表不包含Shutdown_priv列,因此不需要检查它。)

For database-related requests (INSERT, UPDATE, and so on), the server first checks the user's global privileges in the user table row (less any privilege restrictions imposed by partial revokes). 对于与数据库相关的请求(INSERTUPDATE等),服务器首先在user表行中检查用户的全局权限(减去部分撤销所施加的任何权限限制)。If the row permits the requested operation, access is granted. 如果该行允许所请求的操作,则授予访问权限。If the global privileges in the user table are insufficient, the server determines the user's database-specific privileges from the db table:如果user表中的全局权限不足,服务器将从db表中确定用户的数据库特定权限:

After determining the database-specific privileges granted by the db table rows, the server adds them to the global privileges granted by the user table. 在确定db表行授予的数据库特定权限后,服务器将它们添加到user表授予的全局权限中。If the result permits the requested operation, access is granted. 如果结果允许所请求的操作,则授予访问权限。Otherwise, the server successively checks the user's table and column privileges in the tables_priv and columns_priv tables, adds those to the user's privileges, and permits or denies access based on the result. 否则,服务器会依次检查用户在tables_privcolumns_priv表中的表和列权限,将其添加到用户的权限中,并根据结果允许或拒绝访问。For stored-routine operations, the server uses the procs_priv table rather than tables_priv and columns_priv.对于存储的例程操作,服务器使用procs_priv表,而不是tables_privcolumns_priv

Expressed in boolean terms, the preceding description of how a user's privileges are calculated may be summarized like this:用布尔值表示,前面关于如何计算用户权限的描述可以总结如下:

global privileges
OR database privileges
OR table privileges
OR column privileges
OR routine privileges

It may not be apparent why, if the global privileges are initially found to be insufficient for the requested operation, the server adds those privileges to the database, table, and column privileges later. 如果最初发现全局权限不足以执行请求的操作,服务器稍后会将这些权限添加到数据库、表和列权限中,这可能不清楚。The reason is that a request might require more than one type of privilege. 原因是一个请求可能需要多种权限。For example, if you execute an INSERT INTO ... SELECT statement, you need both the INSERT and the SELECT privileges. 例如,如果执行INSERT INTO ... SELECT语句,您需要INSERTSELECT权限。Your privileges might be such that the user table row grants one privilege global and the db table row grants the other specifically for the relevant database. 您的权限可能是这样的:user表行授予一个全局权限,db表行专门为相关数据库授予另一个权限。In this case, you have the necessary privileges to perform the request, but the server cannot tell that from either your global or database privileges alone. It must make an access-control decision based on the combined privileges.在这种情况下,您拥有执行请求所需的权限,但服务器无法仅从您的全局或数据库权限中分辨出来。它必须根据组合权限做出访问控制决策。