跳至主要內容

MYSQL授予用户权限

zhengcog...大约 5 分钟博文数据库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 存储过程、函数 权限。

-- 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段。
  • 初始化数据库的时候删除没有密码的用户。安装完数据库的时候会自动创建一些用户,这些用户默认没有密码。
  • 为每个用户设置满足密码复杂度的密码。
  • 定期清理不需要的用户。回收权限或者删除用户。
上次编辑于:
贡献者: Hyman
评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v2.15.5