在 mysql 中实现刷新权限
本教程通过示例解释了刷新权限操作及其实现。
在 mysql 中实现刷新权限
mysql 通过授权表实现用户管理,以确保服务器中的安全性和访问控制。通常,root 用户
直接通过 update
语句或通过 grant
关键字间接修改授权表。
但是,直接修改授权表需要刷新权限操作或重新启动/重新加载服务器以反映更改。
在 mysql 中,可以使用三个关键字调用刷新权限操作,对于对服务器执行多个更改更加方便和高效。
flush privileges
命令。mysqladmin flush-privileges
命令。mysqladmin reload
命令。
mysql 中的授权表
了解 mysql 授权表可以为刷新权限提供更好的上下文。如前所述,grants 表是一个系统表,用于存储有关 mysql 服务器连接中各种用户及其权限的信息。
要检查授权表中的权限,请使用 show grants
关键字。
-- showing grant privileges for the current user
showgrantsforcurrent_user();/* showing grant privileges for specific user
show grants for [username]
*/
授权表中列出的权限描述了用户可用的限制或权限。让我们创建一个名为 test_user
的用户并在 mysql.user
表中查看该用户的权限。
-- creating a sample user
createuser'test_user'@'localhost'identifiedby'20202010';-- checking assigned privileges
select*frommysql.userwhereuser='test_user'\g;
在 mysql 中使用 flush privileges
关键字提交对服务器的直接权限更改
update
语句与 flushprivilege
命令相结合,授予用户特权并同时反映更改。
例如,让我们为服务器中的所有数据库授予 test_user
只读权限 (select
)。
updatemysql.user-- directly modifying the user table
setselect_priv='y'-- granting select privilege for test_user
whereuser='test_user';select*frommysql.userwhereuser='test_user'\g;-- viewing changes
输出:
*************************** 1. row ***************************
host: localhost
user: test_user
select_priv: y <- this is the updated permission
insert_priv: n
update_priv: n
delete_priv: n (output has been truncated)
查询结果显示用户现在对所有数据库都有 select
权限。但是,此操作尚未反映在服务器中。
showgrantsfortest_user@localhost;-- viewing the grants for the user
输出:
-----------------------------------------------
| grants for test_user@localhost |
-----------------------------------------------
| grant usage on *.* to `test_user`@`localhost` |
-----------------------------------------------
1 row in set (0.00 sec)
现在,让我们执行刷新特权操作。
flushprivileges;-- this affects the changes made
showgrantsfortest_user@localhost;-- viewing the grants for the user, again
输出:
------------------------------------------------
| grants for test_user@localhost |
------------------------------------------------
| grant select on *.* to `test_user`@`localhost` |
------------------------------------------------
1 row in set (0.00 sec)
其他刷新权限命令也可以采用相同的方法,即 mysqladmin flush-privileges
和 mysqladmin reload
。
修改用户权限的推荐方法是通过 grant
命令,因为更改会自动反映,而无需刷新权限操作。
授权表的直接和间接修改之间的区别在这个官方文档中有足够详细的说明。
使用 grant
关键字修改 mysql 中的用户权限
让我们给 test_user
服务器上所有数据库和表的 insert
权限。这一次,我们使用 grant on
命令。
grant on
命令语法如下。
grant[privilege(s)]on[db_name.table_name]touser
要指定所有数据库和表,请使用通配符 *.*
代替 [db_name . table_name]
。
grantinserton*.*totest_user@localhost;-- giving test_user insert privileges
showgrantsfortest_user@localhost;-- checking for reflected changes
输出:
--------------------------------------------------------
| grants for test_user@localhost |
--------------------------------------------------------
| grant select, insert on *.* to `test_user`@`localhost` |
--------------------------------------------------------
1 row in set (0.00 sec)
正如预期的那样,更改反映了没有使用 flush privileges
命令。
转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处
本文地址:
相关文章
如何在 mysql 中声明和使用变量
发布时间:2024/03/26 浏览次数:115 分类:mysql
-
当你需要在 mysql 中的脚本中存储单个值时,最好的方法是使用变量。变量有不同的种类,有必要知道何时以及如何使用每种类型。
发布时间:2024/03/26 浏览次数:176 分类:mysql
-
本教程演示了如何在 mysql 中重置自动增量。
在 mysql 中使用 mysqladmin 刷新主机解除阻塞
发布时间:2024/03/26 浏览次数:82 分类:mysql
-
你将了解阻止主机的原因。此外,通过使用 phpmyadmin 和命令提示符刷新主机缓存来解除阻塞的不同方法和效果。
发布时间:2024/03/26 浏览次数:199 分类:mysql
-
本教程演示如何在 mysql 中转换为整数。
发布时间:2024/03/26 浏览次数:195 分类:mysql
-
本教程演示如何在 mysql 中过滤空值。