GRANT REVOKE 用法详解

一、前言

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

  1. 认证->确定用户是否允许连接数据库服务器;

  2. 授权->确定用户是否拥有足够的权限执行查询请求等。

如果认证不成功的话,那么授权肯定是无法进行的。revokegrant 的语法差不多,只需要把关键字 “to” 换成 “from”。GRANT 和 REVOKE 管理的权限如下:

1731995287865.png
1731995315521.png

如果你想允许用户 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;**否则无法立即生效。

二、查看用户权限

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

show grants;

查看其他 MySQL 用户权限:

show grants for dba@localhost;

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

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

grant all on *.* to 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';

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@’%’

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

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

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

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

grant create 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** 操作 MySQL 存储过程、函数权限

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

**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 作用在单个数据库上:
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’
  • 注意:修改完权限以后一定要刷新服务,或者重启服务,刷新服务用:FLUSH PRIVILEGES
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容