關於mysql.db和數據庫層權限

1# 數據庫層權限記錄位置
表級別的權限記錄在mysql.tables_priv表中。mysql

(root@localhost)[mysql]> (root@localhost)[mysql]> desc tables_priv;
+-------------+-----------------------------------------------------------------------------------------------------------------------------------+------+-----+-------------------+-----------------------------+
| Field       | Type                                                                                                                              | Null | Key | Default           | Extra                       |
+-------------+-----------------------------------------------------------------------------------------------------------------------------------+------+-----+-------------------+-----------------------------+
| Host        | char(60)                                                                                                                          | NO   | PRI |                   |                             |
| Db          | char(64)                                                                                                                          | NO   | PRI |                   |                             |
| User        | char(16)                                                                                                                          | NO   | PRI |                   |                             |
| Table_name  | char(64)                                                                                                                          | NO   | PRI |                   |                             |
| Grantor     | char(77)                                                                                                                          | NO   | MUL |                   |                             |
| Timestamp   | timestamp                                                                                                                         | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| Table_priv  | set('Select','Insert','Update','Delete','Create','Drop','Grant','References','Index','Alter','Create View','Show view','Trigger') | NO   |     |                   |                             |
| Column_priv | set('Select','Insert','Update','References')                                                                                      | NO   |     |                   |                             |
+-------------+-----------------------------------------------------------------------------------------------------------------------------------+------+-----+-------------------+-----------------------------+
8 rows in set (0.00 sec)

然而關於數據庫層面的權限記錄在mysql.db表中sql

(root@localhost)[mysql]> desc db;
+-----------------------+---------------+------+-----+---------+-------+
| Field                 | Type          | Null | Key | Default | Extra |
+-----------------------+---------------+------+-----+---------+-------+
| Host                  | char(60)      | NO   | PRI |         |       |
| Db                    | char(64)      | NO   | PRI |         |       |
| User                  | char(16)      | NO   | PRI |         |       |
| Select_priv           | enum('N','Y') | NO   |     | N       |       |
| Insert_priv           | enum('N','Y') | NO   |     | N       |       |
| Update_priv           | enum('N','Y') | NO   |     | N       |       |
| Delete_priv           | enum('N','Y') | NO   |     | N       |       |
| Create_priv           | enum('N','Y') | NO   |     | N       |       |
| Drop_priv             | enum('N','Y') | NO   |     | N       |       |
| Grant_priv            | enum('N','Y') | NO   |     | N       |       |
| References_priv       | enum('N','Y') | NO   |     | N       |       |
| Index_priv            | enum('N','Y') | NO   |     | N       |       |
| Alter_priv            | enum('N','Y') | NO   |     | N       |       |
| Create_tmp_table_priv | enum('N','Y') | NO   |     | N       |       |
| Lock_tables_priv      | enum('N','Y') | NO   |     | N       |       |
| Create_view_priv      | enum('N','Y') | NO   |     | N       |       |
| Show_view_priv        | enum('N','Y') | NO   |     | N       |       |
| Create_routine_priv   | enum('N','Y') | NO   |     | N       |       |
| Alter_routine_priv    | enum('N','Y') | NO   |     | N       |       |
| Execute_priv          | enum('N','Y') | NO   |     | N       |       |
| Event_priv            | enum('N','Y') | NO   |     | N       |       |
| Trigger_priv          | enum('N','Y') | NO   |     | N       |       |
+-----------------------+---------------+------+-----+---------+-------+
22 rows in set (0.00 sec)

(root@localhost)[mysql]> select * from db\G
*************************** 1. row ***************************
                 Host: %
                   Db: sample
                 User: test1
          Select_priv: Y
          Insert_priv: N
          Update_priv: N
          Delete_priv: N
          Create_priv: Y
            Drop_priv: N
           Grant_priv: N
      References_priv: N
           Index_priv: N
           Alter_priv: N
Create_tmp_table_priv: N
     Lock_tables_priv: N
     Create_view_priv: N
       Show_view_priv: N
  Create_routine_priv: N
   Alter_routine_priv: N
         Execute_priv: N
           Event_priv: N
         Trigger_priv: N
1 row in set (0.00 sec)

這條對應的grant語句是:數據庫

(root@localhost)[mysql]> show grants for test1;
+------------------------------------------------------------------------------------------------------+
| Grants for test1@%                                                                                   |
+------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'test1'@'%' IDENTIFIED BY PASSWORD '*CFA887C680E792C2DCF622D56FB809E3F8BE63CC' |
| GRANT SELECT, CREATE ON `sample`.* TO 'test1'@'%'                                                    |
| GRANT ALL PRIVILEGES ON `sample`.`smp` TO 'test1'@'%'                                                |
| GRANT SELECT ON `mysql`.`user` TO 'test1'@'%'                                                        |
+------------------------------------------------------------------------------------------------------+
4 rows in set (0.00 sec)

第二條:
GRANT SELECT, CREATE ON sample.* TO 'test1'@'%' ide

嘗試再增長權限:ui

(root@localhost)[mysql]> grant all privileges on sample.* to test1;
Query OK, 0 rows affected (0.00 sec)

(root@localhost)[mysql]> 
(root@localhost)[mysql]> 
(root@localhost)[mysql]> select * from db\G
*************************** 1. row ***************************
                 Host: %
                   Db: sample
                 User: test1
          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)

授予all privileges權限。注意點是grant option並不包含在all privileges裏面。能夠用with子句this

(root@localhost)[mysql]> grant all privileges on sample.* to test1 with grant option;
Query OK, 0 rows affected (0.00 sec)

(root@localhost)[mysql]> select * from db\G
*************************** 1. row ***************************
                 Host: %
                   Db: sample
                 User: test1
          Select_priv: Y
          Insert_priv: Y
          Update_priv: Y
          Delete_priv: Y
          Create_priv: Y
            Drop_priv: Y
           Grant_priv: Y
      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)

回收all privileges權限, 錯誤寫法,revoke並不能帶with grant option來回收grant optioncode

(root@localhost)[mysql]> revoke all privileges on sample.* from test1 with grant option;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'grant option' at line 1
(root@localhost)[mysql]>

這樣寫仍是不對:orm

revoke all privileges, grant option  on sample.* from test1;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'on sample.* from test1' at line 1

分開寫就能夠了:server

(root@localhost)[mysql]> revoke all privileges on sample.* from test1;
Query OK, 0 rows affected (0.00 sec)

(root@localhost)[mysql]> revoke grant option on sample.* from test1;
Query OK, 0 rows affected (0.00 sec)

(root@localhost)[mysql]>

grant option在授予的時候是用with子句,回收的時候須要單獨回收。對象

2#通常用戶可訪問的數據庫:
在test1用戶下,查看能夠訪問的數據庫:

(test1@localhost)[(none)]> show databases
    -> ;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| sample             |
+--------------------+
3 rows in set (0.00 sec)

(test1@localhost)[(none)]> 
(test1@localhost)[(none)]> 
(test1@localhost)[(none)]> 
(test1@localhost)[(none)]> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

(test1@localhost)[mysql]> show tables;
+-----------------+
| Tables_in_mysql |
+-----------------+
| user            |                #因爲只有user表上被授予了select權限,因此show tables只能看到一個表
+-----------------+
1 row in set (0.00 sec)

(test1@localhost)[mysql]> show grants;
+---------------------------------------------------------------------+
| Grants for test1@%                                                  |
+---------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'test1'@'%' IDENTIFIED BY PASSWORD <secret>   |
| GRANT ALL PRIVILEGES ON `sample`.* TO 'test1'@'%' WITH GRANT OPTION |
| GRANT ALL PRIVILEGES ON `sample`.`smp` TO 'test1'@'%'               |
| GRANT SELECT ON `mysql`.`user` TO 'test1'@'%'                       |
+---------------------------------------------------------------------+
4 rows in set (0.00 sec)-----------------------------------------------------------------+
| Grants for test1@%                                                |
+-------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'test1'@'%' IDENTIFIED BY PASSWORD <secret> |
| GRANT ALL PRIVILEGES ON `sample`.`smp` TO 'test1'@'%'             |
| GRANT SELECT ON `mysql`.`user` TO 'test1'@'%'                     |
+-------------------------------------------------------------------+
3 rows in set (0.00 sec)

(test1@localhost)[mysql]>

sample數據庫是被授予的all privileges,因此全庫的表對象都是可用唄test1用戶訪問。至於mysql,只有user表上有一個select權限,因此也被歸類在可用訪問的數據庫之列,可是實際上用show tables只能看到一個表。

3# 數據庫層權限的從無到有,給一個用戶授予權限後,db表的變化:
a# 創建一個新的數據庫sample2

(root@localhost)[(none)]> create database sample2;
Query OK, 1 row affected (0.00 sec)

這個時候查看用戶test1所擁有的數據庫權限,是隻有sample數據庫。MYSQL數據庫並不顯示。

(root@localhost)[mysql]> select * from db\G
*************************** 1. row ***************************
                 Host: %
                   Db: sample
                 User: test1
          Select_priv: Y
          Insert_priv: Y
          Update_priv: Y
          Delete_priv: Y
          Create_priv: Y
            Drop_priv: Y
           Grant_priv: Y
      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)

(root@localhost)[mysql]>
#這個時候test1用戶去嘗試訪問sample2數據庫也是失敗的:
(test1@localhost)[sample]> use sample2
ERROR 1044 (42000): Access denied for user 'test1'@'%' to database 'sample2'
(test1@localhost)[sample]>

運行受權語句,對test1受援sample2的select權限,這個時候mysql.db數據庫中出現了2行數據,多了一行關於sample2的記錄:

(root@localhost)[mysql]> select * from db\G
*************************** 1. row ***************************
                 Host: %
                   Db: sample2
                 User: test1
          Select_priv: Y
          Insert_priv: N
          Update_priv: N
          Delete_priv: N
          Create_priv: N
            Drop_priv: N
           Grant_priv: N
      References_priv: N
           Index_priv: N
           Alter_priv: N
Create_tmp_table_priv: N
     Lock_tables_priv: N
     Create_view_priv: N
       Show_view_priv: N
  Create_routine_priv: N
   Alter_routine_priv: N
         Execute_priv: N
           Event_priv: N
         Trigger_priv: N
*************************** 2. row ***************************
                 Host: %
                   Db: sample
                 User: test1
          Select_priv: Y
          Insert_priv: Y
          Update_priv: Y
          Delete_priv: Y
          Create_priv: Y
            Drop_priv: Y
           Grant_priv: Y
      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
2 rows in set (0.00 sec)

從記錄可用看到test1用戶對sample2數據庫擁有select權限。也就是訪問權限,表示裏面的對象都具備。可是裏面是沒有表的。

(test1@localhost)[(none)]> use sample2;
Database changed
(test1@localhost)[sample2]> show tables;
Empty set (0.00 sec)
#而且test1用戶可用select出smp表的數據。
(test1@localhost)[sample2]> select * from smp;
+------+------+
| id   | name |
+------+------+
|    1 | aaa  |
+------+------+
1 row in set (0.00 sec)
# 然而test1用戶並不顯示的擁有任在sample2上的表權限。所謂的權限的繼承。
(test1@localhost)[information_schema]> select * from TABLE_PRIVILEGES where TABLE_SCHEMA='sample2';
Empty set (0.00 sec)

#奇怪,並無給表smp受權select,受援的是select on sample2.*。嘗試單獨對錶收回select 權限:

(root@localhost)[sample2]> revoke select on sample2.smp from test1;
ERROR 1147 (42000): There is no such grant defined for user 'test1' on host '%' on table 'smp'
#以失敗了結。顯示的revoke並不能收回隱式繼承來的權限。

(test1@localhost)[sample2]> show tables;
+-------------------+
| Tables_in_sample2 |
+-------------------+
| smp               |
+-------------------+
1 row in set (0.00 sec)

#這個說明了mysql的權限授予,採用了白名單方式,也就是無論你從什麼層次得到過對象的權限,那麼你就擁有對方想的權限,而並不會由於細粒度層次上不擁有權限而覆蓋高層次上擁有的權限。這就意味着受權的時候,須要特別注意受權的粒度要儘可能小。高層次的受權是至關的危險的。

相關文章
相關標籤/搜索