MySql庫、表權限管理

#受權表
user #該表放行的權限,針對:全部數據,全部庫下全部表,以及表下的全部字段
db #該表放行的權限,針對:某一數據庫,該數據庫下的全部表,以及表下的全部字段
tables_priv #該表放行的權限。針對:某一張表,以及該表下的全部字段
columns_priv #該表放行的權限,針對:某一個字段mysql

#按圖解釋:
user:放行db1,db2及其包含的全部
db:放行db1,及其db1包含的全部
tables_priv:放行db1.table1,及其該表包含的全部
columns_prive:放行db1.table1.column1,只放行該字段sql

#建立用戶
create user 'egon'@'1.1.1.1' identified by '123';
create user 'egon'@'192.168.1.%' identified by '123';
create user 'egon'@'%' identified by '123';


#受權:對文件夾,對文件,對文件某一字段的權限
查看幫助:help grant
經常使用權限有:select,update,alter,delete
all能夠表明除了grant以外的全部權限

#針對全部庫的受權:*.*
grant select on *.* to 'egon1'@'localhost' identified by '123'; #只在user表中能夠查到egon1用戶的select權限被設置爲Y

#針對某一數據庫:db1.*
grant select on db1.* to 'egon2'@'%' identified by '123'; #只在db表中能夠查到egon2用戶的select權限被設置爲Y

#針對某一個表:db1.t1
grant select on db1.t1 to 'egon3'@'%' identified by '123';  #只在tables_priv表中能夠查到egon3用戶的select權限

#針對某一個字段:
mysql> select * from t3;
+------+-------+------+
| id   | name  | age  |
+------+-------+------+
|    1 | egon1 |   18 |
|    2 | egon2 |   19 |
|    3 | egon3 |   29 |
+------+-------+------+

grant select (id,name),update (age) on db1.t3 to 'egon4'@'localhost' identified by '123'; 
#能夠在tables_priv和columns_priv中看到相應的權限
mysql> select * from tables_priv where user='egon4'\G
*************************** 1. row ***************************
       Host: localhost
         Db: db1
       User: egon4
 Table_name: t3
    Grantor: root@localhost
  Timestamp: 0000-00-00 00:00:00
 Table_priv:
Column_priv: Select,Update
row in set (0.00 sec)

mysql> select * from columns_priv where user='egon4'\G
*************************** 1. row ***************************
       Host: localhost
         Db: db1
       User: egon4
 Table_name: t3
Column_name: id
  Timestamp: 0000-00-00 00:00:00
Column_priv: Select
*************************** 2. row ***************************
       Host: localhost
         Db: db1
       User: egon4
 Table_name: t3
Column_name: name
  Timestamp: 0000-00-00 00:00:00
Column_priv: Select
*************************** 3. row ***************************
       Host: localhost
         Db: db1
       User: egon4
 Table_name: t3
Column_name: age
  Timestamp: 0000-00-00 00:00:00
Column_priv: Update
rows in set (0.00 sec)

#刪除權限
revoke select on db1.* from 'egon'@'%';
相關文章
相關標籤/搜索