MYSQL授予用户权限
...大约 5 分钟
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 存储过程、函数 权限。
-- now, can show procedure status
grant create routine on testdb.* to 'developer'@'192.168.0.%';
-- now, you can drop a procedure
grant alter routine on testdb.* to 'developer'@'192.168.0.%';
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'
五. MySQL grant 权限,分别可以作用在多个层次上
1. grant 作用在整个 MySQL 服务器上
dba 可以查询 MySQL 中所有数据库中的表。
grant select on *.* to 'dba'@'localhost';
dba 可以管理 MySQL 中的所有数据库
grant all on *.* to 'dba'@'localhost';
2. grant 作用在单个数据库上
dba 可以查询db中的表。
grant select on testdb.* to 'dba'@'localhost';
3. grant 作用在单个数据表上
grant select, insert, update, delete on testdb.orders to dba@'localhost';
4. grant 作用在表中的列上
grant select(id, se, rank) on testdb.apache_log to dba@'localhost';
5. grant 作用在存储过程、函数上
grant execute on procedure testdb.pr_add to 'dba'@'localhost'
grant execute on function testdb.fn_add to 'dba'@'localhost'
六. 查看 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 来统一管理。
注意
修改完权限以后 一定要刷新服务,或者重启服务,刷新服务用:FLUSH PRIVILEGES。
九. Mysql权限说明表
权限 | 权限级别 | 权限说明 |
---|---|---|
ALTER | 表 | 更改表,比如添加字段、索引等 |
ALTER ROUTINE | 存储过程 | 更改存储过程权限 |
CREATE | 数据库、表或索引 | 创建数据库、表或索引权限 |
CREATE ROUTINE | 存储过程 | 创建存储过程权限 |
CREATE TEMPORARY TABLES | 服务器管理 | 创建临时表权限 |
CREATE USER | 服务器管理 | 创建用户权限 |
CREATE VIEW | 视图 | 创建视图权限 |
DELATE | 表 | 删除数据权限 |
DROP | 数据库或表 | 删除数据库或表权限 |
EVENT | 服务器管理 | 创建、修改、执行和删除事件(event)权限 |
EXECUTE | 存储过程 | 执行存储过程权限 |
FILE | 服务器主机上的文件访问 | 文件访问权限 |
GRANT OPTION | 数据库、表或保存的程序 | 赋予权限选项 |
INDEX | 表 | 索引权限 |
INSERT | 表 | 表插入权限 |
LOCK TABLES | 服务器管理 | 锁表权限 |
PROCESS | 服务器管理 | 查看进程权限 |
REFERENCES | 数据库或表 | 操作外键的权限 |
RELOAD | 服务器管理 | 执行flush-hosts,flush-logs,flush-privileges,flush-status,flush-tables,flush-threads,refresh,reload等命令的权限 |
REPLICATION CLIENT | 服务器管理 | 复制权限 |
REPLICATION SLAVE | 服务器管理 | 复制权限 |
SELECT | 表 | 查询权限 |
SHOW DATABASES | 服务器管理 | 查看数据库权限 |
SHOW VIEW | 视图 | 查看视图权限 |
SHUTDOWN | 服务器管理 | 关闭数据库权限 |
SUPER | 服务器管理 | 执行kill线程权限 |
TRIGGER | 服务器管理 | 操作触发器权限 |
UPDATE | 表 | 更新权限 |
MYSQL的权限如何分布,就是针对表可以设置什么权限,针对列可以设置什么权限等等
权限分布 | 可能设置的权限 |
---|---|
表权限 | 'Select', 'Insert', 'Update', 'Delete', 'Create', 'Drop', 'Grant', 'References', 'Index', 'Alter' |
列权限 | 'Select', 'Insert', 'Update', 'References' |
过程权限 | 'Execute', 'Alter Routine', 'Grant' |
MySQL权限设置经验原则
权限控制主要是出于安全因素,因此需要遵循一下几个经验原则:
- 只授予能满足需要的最小权限,防止用户干坏事。比如用户只是需要查询,那就只给select权限就可以了,不要给用户赋予update、insert或者delete权限。
- 创建用户的时候限制用户的登录主机,一般是限制成指定IP或者内网IP段。
- 初始化数据库的时候删除没有密码的用户。安装完数据库的时候会自动创建一些用户,这些用户默认没有密码。
- 为每个用户设置满足密码复杂度的密码。
- 定期清理不需要的用户。回收权限或者删除用户。
Powered by Waline v2.15.5