Mysql用户授权

来源:互联网 发布:金庸 误解 知乎 编辑:程序博客网 时间:2024/06/17 07:20

Mysql用户授权

Mysql的授权表

这里写图片描述
user:
user表列出可以连接服务器的用户及其口令,并且它指定了有哪种全局(超级用户)权限。在user表启用的任何权限均是全局权限,并适用于所有数据库。例如:如果你启用了DELETE权限,在这里列出的用户可以从任何表中删除记录,谨慎设置。

db:
db表列出数据库有哪些用户有权限访问它们,在这里指定的权限适用于一个数据库中的所有表。

host:
host表与db表结合使用在一个较好层次上控制特定主机对数据库的访问权限,这可能比单独使用db好。这个表不受GRANT和REVOKE语句的影响。

tables_priv:
tables_priv表指定表级权限,指定一个权限适用于一个表的所有列。

columns_priv:
columns_priv表指定列级权限,指定的权限适用于一个表的特定列。

授权

授权普通用户

mysql> grant select on db.* to normaluser@'%'mysql> grant insert on db.* to normaluser@'%'mysql> grant update on db.* to normaluser@'%'mysql> grant delete on db.* to normaluser@'%'mysql> grant select, insert, update, delete on db.* to normaluser@'%'

授权DDL用户

-- 创建、修改、删除 MySQL 数据表结构权限mysql> grant create on db.* to developer@'192.168.0.%';mysql> grant alter on db.* to developer@'192.168.0.%';mysql> grant drop on db.* to developer@'192.168.0.%';-- 外键权限mysql> grant references on db.* to developer@'192.168.0.%';-- 临时表权限mysql> grant create temporary tables on db.* to developer@'192.168.0.%';-- 索引权限mysql> grant index on db.* to developer@'192.168.0.%';-- 视图、查看视图源代码权限mysql> grant create view on db.* to developer@'192.168.0.%';mysql> grant show view on db.* to developer@'192.168.0.%';-- 存储过程、函数权限mysql> grant create routine on db.* to developer@'192.168.0.%'; -- 此权限可以查看存储过程mysql> grant alter routine on db.* to developer@'192.168.0.%'; -- 此权限可以删除存储过程mysql> grant execute on db.* to developer@'192.168.0.%';

授权DBA用户

-- 管理某个数据库的权限mysql> grant all privileges on db to dba@'localhost'-- 管理所有数据库的权限mysql> grant all on *.* to dba@'localhost'-- 把grant权限也赋给它mysql> grant all privileges on *.* to 'root'@'localhost' with grant option-- 需要校验密码的情况mysql> grant all privileges on *.* to 'developer'@'%' identified by 'password';

授权粒度

-- dba 可以查询 MySQL 中所有数据库中的表mysql> grant select on *.* to dba@localhost;-- dba 可以管理 MySQL 中的所有数据库 mysql> grant all on *.* to dba@localhost; -- dba 可以查询 db 中的表mysql> grant select on db.* to dba@localhost; -- 作用在单个数据表上mysql> grant select, insert, update, delete on db.xxxxxx to dba@localhost;-- 一个用户授权多张表mysql> grant select(user_id,username) on smp.xxxxxx to xxxxxx@'%' identified by 'xxxxxx';mysql> grant select on smp.mo_sms to xxxxxx@'%' identified by 'xxxxxx';-- 授权一张表的列mysql> grant select(id, se, rank) on db.xxxxxx to dba@localhost;-- 授权权储过程和函数权限mysql> grant execute on procedure db.xxxxxx to 'dba'@'localhost'mysql> grant execute on function db.xxxxxx to 'dba'@'localhost'

查看用户授权

-- 查看当前用户权限mysql> show grants;-- 查看其他用户权限mysql> show grants for dba@localhost;

撤销授权

-- revoke 跟 grant 的语法相同,只需要把关键字 "to" 换成 "from"mysql> grant all on *.* to dba@localhost;mysql> revoke all on *.* from dba@localhost;

参考:
《mysql grant授权》https://www.cnblogs.com/bethal/p/5512755.html