MySql授权和撤销权限操作

MySQL 赋予用户权限命令的简单格式可概括为:

grant 权限 on 数据库对象 to 用户

  用户后面可以加@'ip地址' identified by '密码'

 例如:

grant all on *.* to root@'%'IDENTIFIED by '123456'
grant all on *.* to ted@'192.168.1.51' IDENTIFIED by '123456'

上面的语句表示将 所有 数据库的所有权限授权给 ted 这个用户,允许 ted 用户在 123.123.123.123 这个 IP 进行远程登陆,并设置 ted 用户的密码为 123456 。

下面逐一分析所有的参数:
USAGE:连接(登录)权限,建立一个用户,就会自动授予其usage权限(默认授予)。该权限只能用于数据库登录,不能执行任何操作,且该权限不能被回收,即使使用REVOKE也不能删除用户权限。

all PRIVILEGES    表示赋予所有的权限给指定用户,这里也可以替换为赋予某一具体的权限,例如:
select,insert,update,delete,create,drop 等,具体权限间用“,”半角逗号分隔。 discuz.*   表示上面的权限是针对于哪个表的,discuz 指的是数据库,后面的 * 表示对于所有的表,由此可以推理出:对于全部数据库的全部 表授权为“*.*”,对于某一数据库的全部表授权为“数据库名.*”,对于某一数据库的某一表授权为“数据库名.表名”。 ted 表示你要给哪个用户授权,这个用户可以是存在的用户,也可以是不存在的用户。 123.123.123.123    表示允许远程连接的 IP 地址,如果想不限制链接的 IP 则设置为“%”即可。 123456   为用户的密码。 执行了上面的语句后,再执行下面的语句,方可立即生效。 > flush privileges;

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

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

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

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

grant create,alter,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,show vuew 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 高级 DBA 管理 MySQL 中所有数据库的权限:

grant all on *.* to dba@'localhost' 

十. grant 作用在表中的列上:

grant select(id, se, rank) on testdb.apache_log to dba@localhost; 

十一、查看 MySQL 用户权限

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

show grants; 

查看其他 MySQL 用户权限:

show grants for 'wuzhd'; 

十二、撤销已经赋予给 MySQL 用户权限的权限。

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

grant all on *.* to dba@localhost;

revoke all on *.* from dba@localhost; 

(2) MySQL grant、revoke 用户权限注意事项

(3)  grant, revoke 用户权限后,该用户只有重新连接 MySQL 数据库,权限才能生效。

(4)  如果想让授权的用户,也可以将这些权限 grant 给其他用户,需要选项 “grant option“

grant select on testdb.* to dba@localhost with grant option;

猜你喜欢

转载自www.cnblogs.com/ZhengHengWU/p/12766448.html