10bet网址
MySQL 5.7 Reference Manual
Related Documentation Download this Manual
PDF (US Ltr)- 36.3Mb
PDF (A4)- 36.3Mb
Man Pages (TGZ)- 235.4Kb
Man Pages (Zip)- 347.1Kb
Info (Gzip)- 3.3Mb
Info (Zip)- 3.3Mb
Excerpts from this Manual

MySQL 5.7 Reference Manual/.../ Access Control, Stage 2: Request Verification

6.2.6 Access Control, Stage 2: Request Verification

服务器接受一个连接后,它进入tage 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 theuser,db,tables_priv,columns_priv, orprocs_privtables. (You may find it helpful to refer toSection 6.2.3, “Grant Tables”, which lists the columns present in each grant table.)

Theusertable grants global privileges. Theusertable row for an account indicates the account privileges that apply on a global basis no matter what the default database is. For example, if theusertable grants you theDELETEprivilege, you can delete rows from any table in any database on the server host. It is wise to grant privileges in theusertable only to people who need them, such as database administrators. For other users, leave all privileges in theusertable set to“N”and grant privileges at more specific levels only (for particular databases, tables, columns, or routines).

Thedbtable grants database-specific privileges. Values in the scope columns of this table can take the following forms:

  • A blankUservalue matches the anonymous user. A nonblank value matches literally; there are no wildcards in user names.

  • The wildcard characters%and_can be used in theHostandDbcolumns. These have the same meaning as for pattern-matching operations performed with theLIKEoperator. If you want to use either character literally when granting privileges, you must escape it with a backslash. For example, to include the underscore character (_) as part of a database name, specify it as\_in theGRANTstatement.

  • A'%'or blankHostvalue meansany host.

  • A'%'or blankDbvalue meansany database.

The server reads thedbtable into memory and sorts it at the same time that it reads theusertable. The server sorts thedbtable based on theHost,Db, andUserscope columns. As with theusertable, 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.

Thetables_priv,columns_priv, andprocs_privtables grant table-specific, column-specific, and routine-specific privileges. Values in the scope columns of these tables can take the following forms:

  • The wildcard characters%and_can be used in theHostcolumn. These have the same meaning as for pattern-matching operations performed with theLIKEoperator.

  • A'%'or blankHostvalue meansany host.

  • TheDb,Table_name,Column_name, andRoutine_namecolumns cannot contain wildcards or be blank.

The server sorts thetables_priv,columns_priv, andprocs_privtables based on theHost,Db, andUsercolumns. This is similar todbtable sorting, but simpler because only theHostcolumn can contain wildcards.

The server uses the sorted tables to verify each request that it receives. For requests that require administrative privileges such asSHUTDOWNorRELOAD, the server checks only theusertable row because that is the only table that specifies administrative privileges. The server grants access if the row permits the requested operation and denies access otherwise. For example, if you want to executemysqladmin shutdownbut yourusertable row does not grant theSHUTDOWNprivilege to you, the server denies access without even checking thedbtable. (The latter table contains noShutdown_privcolumn, so there is no need to check it.)

For database-related requests (INSERT,UPDATE, and so on), the server first checks the user's global privileges in theusertable row. If the row permits the requested operation, access is granted. If the global privileges in theusertable are insufficient, the server determines the user's database-specific privileges from thedbtable:

  • The server looks in thedbtable for a match on theHost,Db, andUsercolumns.

  • TheHostandUsercolumns are matched to the connecting user's host name and MySQL user name.

  • TheDbcolumn is matched to the database that the user wants to access.

  • If there is no row for theHostandUser, access is denied.

After determining the database-specific privileges granted by thedbtable rows, the server adds them to the global privileges granted by theusertable. If the result permits the requested operation, access is granted. Otherwise, the server successively checks the user's table and column privileges in thetables_privandcolumns_privtables, adds those to the user's privileges, and permits or denies access based on the result. For stored-routine operations, the server uses theprocs_privtable rather thantables_privandcolumns_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 anINSERT INTO ... SELECTstatement, you need both theINSERTand theSELECTprivileges. Your privileges might be such that theusertable row grants one privilege global and thedbtable row grants the other specifically for the relevant database. 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.