爱可生开源社区 https://mp.weixin.qq.com/s/fqyHfyNUwIjKNHYHfunc-g
部分权限回收功能的说明
MySQL数据库对于对象的操作级别分为:全局、数据库、表、字段等。粒度从粗到细。如果粗的粒度的权限满足了,将不再检验细粒度的级别,这种验证方式有的时候不方便,例如需要把100个数据库中除了某一个数据库外的访问权限赋予某个用户,需要进行99次赋权。从MySQL 8.0.16开始,MySQL推出了一种部分权限回收(Partial Revokes)的功能,可以将粗粒度赋予的权限在细粒度上回收。
使用部分权限回收功能
要使用这个功能需要将系统参数partial_revokes设置成on,这个参数默认是off,即默认不允许使用部分权限回收功能,在使用时会遇到下面的错误:
mysql> revoke select on mysql.* from scutech;
ERROR 1141 (42000): There is no such grant defined for user 'scutech' on host '%'
可以使用下面的命令将这个参数打开:
mysql> SET PERSIST partial_revokes = ON;
Query OK, 0 rows affected (0.00 sec)
下面的命令赋予用户scutech对除了mysql之外的所有数据库和下面的表的select权限:
mysql> grant select on *.* to scutech;
Query OK, 0 rows affected (0.04 sec)
mysql> revoke select on mysql.* from scutech;
Query OK, 0 rows affected (0.00 sec)
赋权完成后可以使用show grants命令进行检查:
mysql> show grants for scutech;
+-----------------------------------------------+
| Grants for scutech@% |
+-----------------------------------------------+
| GRANT SELECT ON *.* TO `scutech`@`%` |
| REVOKE SELECT ON `mysql`.* FROM `scutech`@`%` |
+-----------------------------------------------+
2 rows in set (0.00 sec)
赋权完成后在mysql.user表里面的User_attributes会有Restrictions的属性:
mysql> select User_attributes from mysql.user where user='scutech' and host='%';
+---------------------------------------------------------------------+
| User_attributes |
+---------------------------------------------------------------------+
| {"Restrictions": [{"Database": "mysql", "Privileges": ["SELECT"]}]} |
+---------------------------------------------------------------------+
1 row in set (0.00 sec)
回收部分权限回收功能
回收部分权限回收功能可以再次赋予部分权限,例如:
mysql> grant SELECT ON `mysql`.* to scutech;
Query OK, 0 rows affected (0.01 sec)
mysql> show grants for scutech;
+--------------------------------------+
| Grants for scutech@% |
+--------------------------------------+
| GRANT SELECT ON *.* TO `scutech`@`%` |
+--------------------------------------+
1 row in set (0.00 sec)
也可以从粗粒度上回收权限,这样细粒度的回收当然没有必要存在了,例如:
mysql> revoke SELECT ON *.* from scutech;
Query OK, 0 rows affected (0.01 sec)
mysql> show grants for scutech;
+-------------------------------------+
| Grants for scutech@% |
+-------------------------------------+
| GRANT USAGE ON *.* TO `scutech`@`%` |
+-------------------------------------+
1 row in set (0.00 sec)
说明:USAGE这个权限等于什么权限也没有。
Oracle ACE,华为云 MVP,Oracle10g,12c OCM; MySQL 5.6,5.7,8.0 OCP;CCNA; EMC Certified; IBM P Certified; RHCE; SQLServer 764; DB2 Certified; TOEIC 890;获得过两次国家部级科技进步奖;发明过两项计算机专利。微信:yaoyuanace 邮箱:yaoyuanace(at)qq.com