Server 層混雜信息字典表 | 全方位認識 information_schema(下)

上期《Server 層混雜信息字典表 | 全方位認識 information_schema(中)》繼續爲大家介紹了部分關於Server層混雜信息字典表的知識,本期“Server層混雜信息字典表(下)”將繼續爲大家介紹。

10、SCHEMA_PRIVILEGES

該表提供查詢關於庫級別的權限信息,每種類型的庫級別權限記錄一行信息

  • 該表中的信息來自mysql.db表

  • 該表爲Memory引擎臨時表

下面是該表中存儲的信息內容

# 創建一個庫級別權限的帳號
root@localhost : information_schema 06:50:42> grant all on sbtest.* to test@'%' identified by 'test';
Query OK, 0 rows affected, 1 warning (0.01 sec)

# 查詢這個帳號的庫級別權限
root@localhost : information_schema 06:52:31>  select * from SCHEMA_PRIVILEGES where GRANTEE="'test'@'%'";
+------------+---------------+--------------+-------------------------+--------------+
| GRANTEE    | TABLE_CATALOG | TABLE_SCHEMA | PRIVILEGE_TYPE          | IS_GRANTABLE |
+------------+---------------+--------------+-------------------------+--------------+
| 'test'@'%' | def          | sbtest      | SELECT                  | NO          |
| 'test'@'%' | def          | sbtest      | INSERT                  | NO          |
| 'test'@'%' | def          | sbtest      | UPDATE                  | NO          |
| 'test'@'%' | def          | sbtest      | DELETE                  | NO          |
| 'test'@'%' | def          | sbtest      | CREATE                  | NO          |
| 'test'@'%' | def          | sbtest      | DROP                    | NO          |
| 'test'@'%' | def          | sbtest      | REFERENCES              | NO          |
| 'test'@'%' | def          | sbtest      | INDEX                  | NO          |
| 'test'@'%' | def          | sbtest      | ALTER                  | NO          |
| 'test'@'%' | def          | sbtest      | CREATE TEMPORARY TABLES | NO          |
| 'test'@'%' | def          | sbtest      | LOCK TABLES            | NO          |
| 'test'@'%' | def          | sbtest      | EXECUTE                | NO          |
| 'test'@'%' | def          | sbtest      | CREATE VIEW            | NO          |
| 'test'@'%' | def          | sbtest      | SHOW VIEW              | NO          |
| 'test'@'%' | def          | sbtest      | CREATE ROUTINE          | NO          |
| 'test'@'%' | def          | sbtest      | ALTER ROUTINE          | NO          |
| 'test'@'%' | def          | sbtest      | EVENT                  | NO          |
| 'test'@'%' | def          | sbtest      | TRIGGER                | NO          |
+------------+---------------+--------------+-------------------------+--------------+
18 rows in set (0.00 sec)

字段含義如下(該表中所有字段爲 "MySQL extension" 列)

  • GRANTEE:表示權限對應的賬戶名稱

  • TABLE_CATALOG:該字段總是爲def

  • TABLE_SCHEMA:表示權限對應的數據庫名稱

  • PRIVILEGE_TYPE:表示權限列別,如:SELECT、TRIGGER等

  • IS_GRANTABLE:表示擁有該權限的賬戶是否可給其他人授予該權限,注意:只有帳號擁有grant option權限時才爲YES,否則該字段爲NO

PS:該表中記錄的權限也可以通過show語句查詢或者查詢mysql.db表

# show語句查詢
root@localhost : information_schema 06:53:19> show grants for test@'%';
+--------------------------------------------------+
| Grants for test@%                                |
+--------------------------------------------------+
| GRANT USAGE ON *.* TO 'test'@'%'                |
| GRANT ALL PRIVILEGES ON `sbtest`.* TO 'test'@'%' |
+--------------------------------------------------+
2 rows in set (0.00 sec)

# 查詢mysql.db表
root@localhost : information_schema 06:52:37> select * from mysql.db where user='test'\G;
*************************** 1. row ***************************
            Host: %
              Db: sbtest
            User: test
      Select_priv: Y
      Insert_priv: Y
      Update_priv: Y
      Delete_priv: Y
      Create_priv: Y
        Drop_priv: Y
      Grant_priv: N
  References_priv: Y
      Index_priv: Y
      Alter_priv: Y
Create_tmp_table_priv: Y
Lock_tables_priv: Y
Create_view_priv: Y
  Show_view_priv: Y
Create_routine_priv: Y
Alter_routine_priv: Y
    Execute_priv: Y
      Event_priv: Y
    Trigger_priv: Y
1 row in set (0.00 sec)

11、TABLE_PRIVILEGES

該表提供查詢關於表級別權限信息,該表中的內容來自mysql.tables_priv

  • 該表爲Memory引擎臨時表

下面是該表中存儲的信息內容

# 創建測試帳號
root@localhost : information_schema 12:17:33> grant all on sbtest.sbtest1 to 'test_table'@'%' identified by 'test';
Query OK, 0 rows affected, 1 warning (0.00 sec)

# 查詢該帳號的表級別權限
root@localhost : information_schema 12:18:20> select * from TABLE_PRIVILEGES where GRANTEE="'test_table'@'%'";
+------------------+---------------+--------------+------------+----------------+--------------+
| GRANTEE          | TABLE_CATALOG | TABLE_SCHEMA | TABLE_NAME | PRIVILEGE_TYPE | IS_GRANTABLE |
+------------------+---------------+--------------+------------+----------------+--------------+
| 'test_table'@'%' | def          | sbtest      | sbtest1    | SELECT        | NO          |
| 'test_table'@'%' | def          | sbtest      | sbtest1    | INSERT        | NO          |
| 'test_table'@'%' | def          | sbtest      | sbtest1    | UPDATE        | NO          |
| 'test_table'@'%' | def          | sbtest      | sbtest1    | DELETE        | NO          |
| 'test_table'@'%' | def          | sbtest      | sbtest1    | CREATE        | NO          |
| 'test_table'@'%' | def          | sbtest      | sbtest1    | DROP          | NO          |
| 'test_table'@'%' | def          | sbtest      | sbtest1    | REFERENCES    | NO          |
| 'test_table'@'%' | def          | sbtest      | sbtest1    | INDEX          | NO          |
| 'test_table'@'%' | def          | sbtest      | sbtest1    | ALTER          | NO          |
| 'test_table'@'%' | def          | sbtest      | sbtest1    | CREATE VIEW    | NO          |
| 'test_table'@'%' | def          | sbtest      | sbtest1    | SHOW VIEW      | NO          |
| 'test_table'@'%' | def          | sbtest      | sbtest1    | TRIGGER        | NO          |
+------------------+---------------+--------------+------------+----------------+--------------+
12 rows in set (0.00 sec)

表字段含義:

  • GRANTEE:權限擁有者,即賬戶名稱

  • TABLE_CATALOG:該字段總是爲def

  • TABLE_SCHEMA:表級別權限涉及的表所屬的數據庫名稱

  • TABLE_NAME:表級別權限涉及的表名稱

  • PRIVILEGE_TYPE:權限類型,有效值爲:SELECT、INSERT、UPDATE、DELETE、CREATE、DROP、REFERENCES、ALTER、INDEX、CREATE VIEW、SHOW VIEW、TRIGGER

  • IS_GRANTABLE:擁有該權限的用戶是否可以將此權限授予給其他人,注意:只有帳號擁有grant option權限時才爲YES,否則該字段爲NO

PS:該表中的內容還可以使用show語句和mysql.tables_priv表查看

# show語句
root@localhost : information_schema 12:19:14> show grants for test_table@'%';
+----------------------------------------------------------------+
| Grants for test_table@%                                        |
+----------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'test_table'@'%'                        |
| GRANT ALL PRIVILEGES ON `sbtest`.`sbtest1` TO 'test_table'@'%' |
+----------------------------------------------------------------+
2 rows in set (0.00 sec)

# mysql.tables_priv表
root@localhost : information_schema 12:21:01> select * from mysql.tables_priv where user='test_table'\G;
*************************** 1. row ***************************
  Host: %
    Db: sbtest
  User: test_table
Table_name: sbtest1
Grantor: root@localhost
Timestamp: 0000-00-00 00:00:00
Table_priv: Select,Insert,Update,Delete,Create,Drop,References,Index,Alter,Create View,Show view,Trigger
Column_priv: 
1 row in set (0.00 sec)

12、USER_PRIVILEGES

該表提供查詢全局權限的信息,該表中的信息來自mysql.user表

  • 該表爲Memory引擎臨時表

下面是該表中存儲的信息內容

# 創建測試帳號
root@localhost : information_schema 12:42:02> grant all on *.* to test_user@'%' identified by 'test';
Query OK, 0 rows affected, 1 warning (0.00 sec)

# 查看USER_PRIVILEGES 表
root@localhost : information_schema 12:44:59>  select * from USER_PRIVILEGES where GRANTEE="'test_user'@'%'";
+-----------------+---------------+-------------------------+--------------+
| GRANTEE        | TABLE_CATALOG | PRIVILEGE_TYPE          | IS_GRANTABLE |
+-----------------+---------------+-------------------------+--------------+
| 'test_user'@'%' | def          | SELECT                  | NO          |
| 'test_user'@'%' | def          | INSERT                  | NO          |
| 'test_user'@'%' | def          | UPDATE                  | NO          |
| 'test_user'@'%' | def          | DELETE                  | NO          |
| 'test_user'@'%' | def          | CREATE                  | NO          |
| 'test_user'@'%' | def          | DROP                    | NO          |
| 'test_user'@'%' | def          | RELOAD                  | NO          |
| 'test_user'@'%' | def          | SHUTDOWN                | NO          |
| 'test_user'@'%' | def          | PROCESS                | NO          |
| 'test_user'@'%' | def          | FILE                    | NO          |
| 'test_user'@'%' | def          | REFERENCES              | NO          |
| 'test_user'@'%' | def          | INDEX                  | NO          |
| 'test_user'@'%' | def          | ALTER                  | NO          |
| 'test_user'@'%' | def          | SHOW DATABASES          | NO          |
| 'test_user'@'%' | def          | SUPER                  | NO          |
| 'test_user'@'%' | def          | CREATE TEMPORARY TABLES | NO          |
| 'test_user'@'%' | def          | LOCK TABLES            | NO          |
| 'test_user'@'%' | def          | EXECUTE                | NO          |
| 'test_user'@'%' | def          | REPLICATION SLAVE      | NO          |
| 'test_user'@'%' | def          | REPLICATION CLIENT      | NO          |
| 'test_user'@'%' | def          | CREATE VIEW            | NO          |
| 'test_user'@'%' | def          | SHOW VIEW              | NO          |
| 'test_user'@'%' | def          | CREATE ROUTINE          | NO          |
| 'test_user'@'%' | def          | ALTER ROUTINE          | NO          |
| 'test_user'@'%' | def          | CREATE USER            | NO          |
| 'test_user'@'%' | def          | EVENT                  | NO          |
| 'test_user'@'%' | def          | TRIGGER                | NO          |
| 'test_user'@'%' | def          | CREATE TABLESPACE      | NO          |
+-----------------+---------------+-------------------------+--------------+
28 rows in set (0.00 sec)

字段含義如下(該表中所有的字段都爲 "MySQL extension" 列)

  • GRANTEE:權限擁有者,即賬戶名稱

  • TABLE_CATALOG:該字段總是爲def

  • PRIVILEGE_TYPE:權限類型

  • IS_GRANTABLE:擁有該權限的賬戶是否可以把該權限授予其他人,注意:只有帳號擁有grant option權限時才爲YES,否則該字段爲NO

該表中的信息還可以使用show語句或者mysql.user表查看

# show語句
root@localhost : information_schema 12:45:35> show grants for test_user@'%';
+------------------------------------------------+
| Grants for test_user@%                        |
+------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'test_user'@'%' |
+------------------------------------------------+
1 row in set (0.00 sec)

# 查詢mysql.user表
root@localhost : information_schema 12:46:18> select * from mysql.user where user='test_user'\G;
*************************** 1. row ***************************
              Host: %
              User: test_user
      Select_priv: Y
      Insert_priv: Y
      Update_priv: Y
      Delete_priv: Y
      Create_priv: Y
        Drop_priv: Y
      Reload_priv: Y
    Shutdown_priv: Y
      Process_priv: Y
        File_priv: Y
        Grant_priv: N
  References_priv: Y
        Index_priv: Y
        Alter_priv: Y
      Show_db_priv: Y
        Super_priv: Y
Create_tmp_table_priv: Y
  Lock_tables_priv: Y
      Execute_priv: Y
  Repl_slave_priv: Y
  Repl_client_priv: Y
  Create_view_priv: Y
    Show_view_priv: Y
Create_routine_priv: Y
Alter_routine_priv: Y
  Create_user_priv: Y
        Event_priv: Y
      Trigger_priv: Y
Create_tablespace_priv: Y
          ssl_type: 
        ssl_cipher: 
      x509_issuer: 
      x509_subject: 
    max_questions: 0
      max_updates: 0
  max_connections: 0
max_user_connections: 0
            plugin: mysql_native_password
authentication_string: *94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29
  password_expired: N
password_last_changed: 2018-01-29 00:44:59
password_lifetime: NULL
    account_locked: N
1 row in set (0.00 sec)

本期內容就介紹到這裏,本期內容參考鏈接如下:

  • https://dev.mysql.com/doc/refman/5.7/en/status-table.html

  • https://dev.mysql.com/doc/refman/5.7/en/variables-table.html

  • https://dev.mysql.com/doc/refman/5.7/en/optimizer-trace-table.html

  • https://dev.mysql.com/doc/refman/5.7/en/plugins-table.html

  • https://dev.mysql.com/doc/refman/5.7/en/processlist-table.html

  • https://dev.mysql.com/doc/refman/5.7/en/profiling-table.html

  • https://dev.mysql.com/doc/refman/5.7/en/character-sets-table.html

  • https://dev.mysql.com/doc/refman/5.7/en/collations-table.html

  • https://dev.mysql.com/doc/refman/5.7/en/column-privileges-table.html

  • https://dev.mysql.com/doc/refman/5.7/en/schema-privileges-table.html

  • https://dev.mysql.com/doc/refman/5.7/en/table-privileges-table.html

  • https://dev.mysql.com/doc/refman/5.7/en/user-privileges-table.html

| 作者簡介

羅小波·數據庫技術專家

《千金良方——MySQL性能優化金字塔法則》、《數據生態:MySQL複製技術與生產實踐》作者之一。熟悉MySQL體系結構,擅長數據庫的整體調優,喜好專研開源技術,並熱衷於開源技術的推廣,在線上線下做過多次公開的數據庫專題分享,發表過近100篇數據庫相關的研究文章。

全文完。

Enjoy MySQL :)

葉老師的「MySQL核心優化」大課已升級到MySQL 8.0,掃碼開啓MySQL 8.0修行之旅吧

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章