MySQL 何时需要使用 flush privileges 命令

数据库关系型数据库技术服务知识库
前言

flush privileges 命令的主要作用是重新在 mysql 系统库中读取相关的权限表,来使用户相关权限可以及时刷新并生效。具体来说,flush privileges 用于同步全量的权限数据并刷新内存,反之亦然。 我们何时需要使用 flush privileges 命令?

何时不需要使用

如果我们使用 create user,grant,revoke 等用户管理语句,不需要手动去运行 flush 命令。 用户具有的权限如下:

mysql> show grants for rudonx1;
+-------------------------------------------------------------+
| Grants for rudonx1@%                                        |
+-------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'rudonx1'@'%'                         |
| GRANT SELECT, UPDATE, DELETE ON `rudonx`.* TO 'rudonx1'@'%' |
+-------------------------------------------------------------+
2 rows in set (0.03 sec)

使用 revoke 命令进行权限回收

mysql> revoke delete on rudonx.* from 'rudonx1'@'%';
Query OK, 0 rows affected (0.04 sec)

查看具有的权限,发现已经生效

mysql> show grants for rudonx1;
+-----------------------------------------------------+
| Grants for rudonx1@%                                |
+-----------------------------------------------------+
| GRANT USAGE ON *.* TO 'rudonx1'@'%'                 |
| GRANT SELECT, UPDATE ON `rudonx`.* TO 'rudonx1'@'%' |
+-----------------------------------------------------+
2 rows in set (0.03 sec)

何时需要使用

某些用户习惯于使用 insert,update 等 DML 语句去手动更新权限表,在这种场景下,需要使用 flush privileges 命令。 注意:使用 DML 语句手动更新权限表并不推荐的方式。

mysql> update mysql.db set select_priv='N' where user ='rudonx1';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

查看权限,发现并未更新:

mysql> show grants for rudonx1;
+-----------------------------------------------------+
| Grants for rudonx1@%                                |
+-----------------------------------------------------+
| GRANT USAGE ON *.* TO 'rudonx1'@'%'                 |
| GRANT SELECT, UPDATE ON `rudonx`.* TO 'rudonx1'@'%' |
+-----------------------------------------------------+
2 rows in set (0.00 sec)

执行 flush 语句并查看:

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

mysql> show grants for rudonx1;
+---------------------------------------------+
| Grants for rudonx1@%                        |
+---------------------------------------------+
| GRANT USAGE ON *.* TO 'rudonx1'@'%'         |
| GRANT UPDATE ON `rudonx`.* TO 'rudonx1'@'%' |
+---------------------------------------------+
2 rows in set (0.00 sec)
总结

从上面的测试上来看,如果使用账户管理命令,如 grant,revoke 等,是不需要手动执行 flush 命令,如果使用 DML 语句来更新系统权限表,建议运行完后手动执行来使权限生效。 附上官方文档上的说明[1]:

If you modify the grant tables indirectly using an account-management statement, the server notices these changes and loads the grant tables into memory again immediately

If you modify the grant tables directly using statements such as INSERT, UPDATE, or DELETE (which is not recommended), the changes have no effect on privilege checking until you either tell the server to reload the tables or restart it.

同时需要注意的是,由于 火山引擎 RDS for MySQL 是一个托管服务,不提供 super 权限,因此无法使用 DML 语句来更新权限表。您可以通过控制台来进行权限管理或者手动执行命令来赋予用户相应的权限。

参考文档

[1] https://dev.mysql.com/doc/refman/5.7/en/privilege-changes.html 如果您有其他问题,欢迎您联系火山引擎技术支持服务

37
0
0
0
相关产品
评论
未登录
看完啦,登录分享一下感受吧~
暂无评论