首页 技术 正文
技术 2022年11月21日
0 收藏 744 点赞 2,274 浏览 3362 个字

mysql进阶(二十八)MySQL GRANT REVOKE用法

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

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

  如果认证不成功的话,那么授权肯定是无法进行的。

  revoke 跟 grant 的语法差不多,只需要把关键字 “to” 换成 “from”。

  表 GRANT和REVOKE管理的权限

  如果你想允许用户myuser从ip为192.168.1.6的主机连接到mysql服务器,并使用mypassword作为密码

  • (1). GRANT ALL PRIVILEGES ON . TO ‘myuser’@’192.168.1.3’ IDENTIFIED

    BY ‘mypassword’ WITH GRANT OPTION;

    (2). FLUSH PRIVILEGES;

**

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

**

查看 MySQL 用户权限

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

show grants;

  查看其他 MySQL 用户权限:

show grants for dba@localhost;

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

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

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

MySQL 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 statusgrant alter routine on testdb.* to developer@’192.168.0.%’; -- now, you can drop a proceduregrant execute on testdb.* to developer@’192.168.0.%’;

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

grant all privileges on testdb to dba@’localhost’

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

  grant 高级 DBA 管理 MySQL 中所有数据库的权限。

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

MySQL grant 权限,分别可以作用在多个层次上。

  1.grant 作用在整个 MySQL 服务器上:

grant select on . to dba@localhost; – dba可以查询 MySQL 中所有数据库中的表。

grant all on . to dba@localhost; – dba可以管理 MySQL 中的所有数据库

  2.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。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,565
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,413
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,186
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905