MySQL 实战笔记 第01期:MySQL 角色管理

数据库 MySQL
角色 ( Role ) 可以用来批量管理用户,同一个角色下的用户,拥有相同的权限。那 MySQL 数据库是否也有这样的功能呢 ?答案是肯定的。MySQL 5.7.X 可以通过 mysql.proxies_priv 来模拟角色 (Role) 的功能。

[[359913]]

角色 ( Role ) 可以用来批量管理用户,同一个角色下的用户,拥有相同的权限。那 MySQL 数据库是否也有这样的功能呢 ?答案是肯定的。MySQL 5.7.X 可以通过 mysql.proxies_priv 来模拟角色 (Role) 的功能。下面让我们来实验一下(测试的版本 MySQL 5.7.28):

1 配置 proxy

mysql> show variables like "%proxy%"; #查看当前proxy是否开启,OFF 表示没有开启 
+-----------------------------------+-------+ 
| Variable_name                     | Value | 
+-----------------------------------+-------+ 
| check_proxy_users                 | OFF   | 
| mysql_native_password_proxy_users | OFF   | 
| proxy_user                        |       | 
| sha256_password_proxy_users       | OFF   | 
+-----------------------------------+-------+ 
rows in set (0.02 sec) 
 
mysql> set global check_proxy_users =on
Query OK, 0 rows affected (0.00 sec) 
 
mysql> set global mysql_native_password_proxy_users = on
Query OK, 0 rows affected (0.01 sec) 
 
mysql> exit 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.

以上设置参数,对当前会话无效,需要退出后重新登录,或直接设置到 my.cnf 中去;

2 创建角色和用户

mysql>  create user role_dba; 
Query OK, 0 rows affected (1.03 sec) 
 
mysql> create user 'jack'
Query OK, 0 rows affected (0.01 sec) 
 
mysql> create user 'mary'
Query OK, 0 rows affected (0.01 sec) 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

用户为设置密码,如需密码可以使用 identified by '####' 设置;

3 权限映射

将 role_dba 的权限映射( map )到 jack 、mary

mysql> grant proxy on role_dba to jack; 
Query OK, 0 rows affected (0.02 sec) 
 
mysql> grant proxy on role_dba to mary; 
Query OK, 0 rows affected (0.01 sec) 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

4 给用户赋权

给 role_dba 赋权(模拟 role 赋权)

mysql> grant select on *.* to role_dba; 
Query OK, 0 rows affected (0.01 sec) 
 
mysql> show grants for role_dba; 
+---------------------------------------+ 
| Grants for role_dba@%                 | 
+---------------------------------------+ 
GRANT SELECT ON *.* TO 'role_dba'@'%' | 
+---------------------------------------+ 
1 row in set (0.00 sec) 
 
mysql> show grants for jack; 
+---------------------------------------------+ 
| Grants for jack@%                           | 
+---------------------------------------------+ 
GRANT USAGE ON *.* TO 'jack'@'%'            | 
GRANT PROXY ON 'role_dba'@'%' TO 'jack'@'%' | 
+---------------------------------------------+ 
rows in set (0.00 sec) 
 
mysql> show grants for mary; 
+---------------------------------------------+ 
| Grants for mary@%                           | 
+---------------------------------------------+ 
GRANT USAGE ON *.* TO 'mary'@'%'            | 
GRANT PROXY ON 'role_dba'@'%' TO 'mary'@'%' | 
+---------------------------------------------+ 
rows in set (0.00 sec) 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.

5 查看 mysql.proxies_priv

mysql> select * from mysql.proxies_priv; 
+-----------+------+--------------+--------------+------------+----------------------+---------------------+ 
| Host      | User | Proxied_host | Proxied_user | With_grant | Grantor              | Timestamp           | 
+-----------+------+--------------+--------------+------------+----------------------+---------------------+ 
| localhost | root |              |              |          1 | boot@connecting host | 0000-00-00 00:00:00 | 
| %         | will | %            | will_dba     |          0 | root@localhost       | 0000-00-00 00:00:00 | 
| %         | tom  | %            | will_dba     |          0 | root@localhost       | 0000-00-00 00:00:00 | 
| %         | jack | %            | role_dba     |          0 | root@localhost       | 0000-00-00 00:00:00 | 
| %         | mary | %            | role_dba     |          0 | root@localhost       | 0000-00-00 00:00:00 | 
+-----------+------+--------------+--------------+------------+----------------------+---------------------+ 
rows in set (0.01 sec) 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

6 验证

$ mysql -h 127.0.0.1 -u jack  
 
Welcome to the MySQL monitor. Commands end with ; or \g. 
 
Your MySQL connection id is 249 
 
Server version: 5.7.28-log MySQL Community Server (GPL) 
 
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. 
 
Oracle is a registered trademark of Oracle Corporation and/or its 
 
affiliates. Other names may be trademarks of their respective 
 
owners. 
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 
 
mysql> select * from test.ssd limit 1; 
 
+---+------+------+ 
 
| a | b  | c  | 
 
+---+------+------+ 
 
| 1 | NULL | NULL | 
 
+---+------+------+ 
 
1 row in set (0.01 sec) 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.

mysql.proxies_priv 仅仅是对 Role 的模拟,和 Oracle 的角色还是有所不同的;官方称呼为 Role like。

MySQL 8.0 正式增加了 role 功能,有兴趣的同学可以自行了解。MySQL 5.6.X 模拟 Role 功能需要安装插件,具体方法可参考:https://dev.mysql.com/doc/refman/5.6/en/proxy-users.htmlhttps://dev.mysql.com/doc/refman/5.6/en/pluggable-authentication.html 

责任编辑:庞桂玉 来源: 杨建荣的学习笔记
相关推荐

2020-12-24 18:00:45

MySQL元数据锁数据库

2018-05-03 10:33:14

数据库MySQL 8.0角色管理

2021-04-16 10:35:14

MySQL权限管理

2009-06-18 14:20:45

hibernate实战

2009-06-16 13:09:15

Hibernate实战Hibernate

2017-06-30 13:00:40

仓储信息化ERP

2011-08-16 14:14:22

MySQL数据库初学者

2021-04-23 10:31:18

MySQLRole数据库

2016-07-15 09:08:12

V课堂数字化制造

2012-02-07 15:29:45

开发技术周刊

2009-04-20 08:51:50

MySQL查询优化数据库

2013-01-21 13:41:59

IBMdW

2017-10-09 22:33:56

SQL等值分组有序分组

2015-11-11 14:54:35

网络·安全技术周刊

2020-06-22 10:19:58

技术资讯

2020-02-14 16:20:19

大咖来了IT管理者

2022-05-17 11:06:44

数据库MySQL系统

2017-10-18 22:34:33

SQL等值分组有序分组

2017-09-05 22:34:24

遍历SQL运算

2017-11-08 06:18:43

JOINSQL运算
点赞
收藏

51CTO技术栈公众号