GRANT REVOKE用法详解

举报
SHQ5785 发表于 2022/08/15 10:06:31 2022/08/15
【摘要】 一、前言MySQL权限系统围绕两个概念:认证->确定用户是否允许连接数据库服务器;授权->确定用户是否拥有足够的权限执行查询请求等。如果认证不成功的话,那么授权肯定是无法进行的。revoke 跟 grant 的语法差不多,只需要把关键字 “to” 换成 “from”。GRANT和REVOKE管理的权限如下:如果你想允许用户myuser从ip为192.168.1.6的主机连接到mysql服务...

一、前言

MySQL权限系统围绕两个概念:

  1. 认证->确定用户是否允许连接数据库服务器;
  2. 授权->确定用户是否拥有足够的权限执行查询请求等。

如果认证不成功的话,那么授权肯定是无法进行的。
revokegrant 的语法差不多,只需要把关键字 “to” 换成 “from”。
GRANT和REVOKE管理的权限如下:
这里写图片描述
这里写图片描述
如果你想允许用户myuser从ip为192.168.1.6的主机连接到mysql服务器,并使用mypassword作为密码:

GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'192.168.1.3' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;

FLUSH PRIVILEGES;

注意:授权后必须FLUSH PRIVILEGES;否则无法立即生效。

二、查看用户权限

查看当前用户(自己)权限:

show grants;

查看其他 MySQL 用户权限:

show grants for dba@localhost;

三、撤销已赋予给 MySQL 用户的权限

revokegrant 的语法差不多,只需要把关键字 “to” 换成 “from” 即可:

grant all on *.* to dba@localhost;
revoke all on *.* from dba@localhost;

四、grant、revoke 用户权限注意事项

  • grant, revoke 用户权限后,该用户只有重新连接 MySQL 数据库,权限才能生效。
  • 如果想让授权的用户,也可以将这些权限 grant 给其他用户,需要选项 “grant option
grant select on testdb.* to dba@localhost with grant option;

;这个特性一般用不到。实际中,数据库权限最好由 DBA 来统一管理。

五、用户和权限管理命令

create user :用于创建新的用户账户(从5.0版本开始有这个命令),在创建这个用户的时候不分配任何权限,需要在创建之后通过grant命令来给该用户分配相应的权限。

create user guest@localhost identified by '123456'grant select on mydb.* to guest@localhost;

drop user:删除用户账户(注意在4.1.1版本之前只能删除没有任何权限的账户,5.0.2之后可以删除任何账户)

drop user guest;

rename user:可以实现重命名一个用户账号。
grant:用于管理访问权限,也就是给用户账号授权。当然它同样可以创建一个新的用户账户。

grant select, insert, update, delete on new_db.* to guest@'%' identified by '88888888';

grant 权限 on 数据库.表 to 用户 @ 访问方式 identified by 密码

grant select on mydb.* to guest@localhost identified by '123456';

如果需要一个空密码或者无密码的账户,必须先用Create User命令,然后通过grant来分配权限。如果执行如下操作:grant all privileges on mydb.* to visitor@'%' ;而在数据库user表中没有先创建visitor用户,则会发生1133错误"Can't find any matching row in the user table"。

  • 注:grant只能创有密码的账户。

revoke:删除一个账户,具体查看MySQL文档。

mysql中可以给一个用户授予如select,insert,update,delete等其中的一个或者多个权限,主要使用grant命令,用法格式为:grant 权限 on 数据库对象 to 用户;

grant 普通数据用户,查询、插入、更新、删除数据库中所有表数据的权利。

grant select on testdb.* to common_user@’%grant insert on testdb.* to common_user@’%grant update on testdb.* to common_user@’%grant delete on testdb.* to common_user@’%

或者,用一条 MySQL 命令来替代:

grant select, insert, update, delete on testdb.* to common_user@’%

grant 数据库开发人员,创建表、索引、视图、存储过程、函数等权限。

grant 创建、修改、删除 MySQL 数据表结构权限

grant create on testdb.* to developer@’192.168.0.%;
grant alter on testdb.* to developer@’192.168.0.%;
grant drop on testdb.* to developer@’192.168.0.%;

grant 操作 MySQL 外键权限

grant references on testdb.* to developer@’192.168.0.%;

grant 操作 MySQL 临时表权限

grant create temporary tables on testdb.* to developer@’192.168.0.%;

grant 操作 MySQL 索引权限

grant index on testdb.* to developer@’192.168.0.%;

grant 操作 MySQL 视图、查看视图源代码权限

grant create view on testdb.* to developer@’192.168.0.%;
grant show view on testdb.* to developer@’192.168.0.%;

grant 操作 MySQL 存储过程、函数权限

grant create routine on testdb.* to developer@’192.168.0.%; -- now, can show procedure status
grant alter routine on testdb.* to developer@’192.168.0.%; -- now, you can drop a procedure
grant execute on testdb.* to developer@’192.168.0.%;

grant 普通 DBA 管理某个 MySQL 数据库权限

grant all privileges on testdb to dba@’localhost’

其中,关键字“privileges”可以省略。

grant MySQL 中所有数据库的权限

grant all on *.* to dba@’localhost’;

六、 grant 权限

  • grant 作用在整个 MySQL 服务器上:
grant select on *.* to dba@localhost; -- dba可以查询 MySQL 中所有数据库中的表。
grant all on *.* to dba@localhost; -- dba可以管理 MySQL 中的所有数据库
  • grant 作用在单个数据库上:
grant select on testdb.* to dba@localhost; -- dba 可以查询 testdb 中的表。
  • grant 作用在单个数据表上:
grant select, insert, update, delete on testdb.orders to dba@localhost;
  • grant 作用在表中的列上:
grant select(id, se, rank) on testdb.apache_log to dba@localhost;
  • grant 作用在存储过程、函数上:
grant execute on procedure testdb.pr_add to ’dba’@’localhost’
grant execute on function testdb.fn_add to ’dba’@’localhost’
  • 注意:修改完权限以后一定要刷新服务,或者重启服务,刷新服务用:FLUSH PRIVILEGES

七、拓展阅读

【版权声明】本文为华为云社区用户原创内容,未经允许不得转载,如需转载请自行联系原作者进行授权。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

举报
请填写举报理由
0/200