Mysql 权限管理

Mysql 权限管理分2步:

1. 创建账号

1) 创建本地账号:

create user 'alex'@'localhost' identified by '123';    # 创建alex用户 密码为123,

 

2) 创建远程账号:

create user 'alex'@'192.168.10.102' identified by '123';   # 只允许IP为 192.168.10.102 的主机进行登录

create user 'alex'@'192.168.10.%' identified by '123';   # 只允许 192.168.10.0 这个网段登录

create user 'alex'@'%' identified by '123';  # 允许所有客户端都可以登录

 

2. 授权

Mysql 授权涉及到mysql数据库下的4张表,权限力度依次降低:

user     # 该表放行的权限,针对:所有数据,所有库下所有表,以及表下的所有字段。如  *.* 表示所有库

db   # 该表放行的权限,针对:某一数据库,该数据库下的所有表,以及表下的所有字段。如  db1.*  表示db1库下的所有表

tables_priv    # 该表放行的权限。针对:某一张表,以及该表下的所有字段。如  db1.t1  表示 db1库下的t1表

columns_priv    # 该表放行的权限,针对:某一个字段。如  id,name  只针对id和name字段

 

grant select on *.* to 'alex'@'localhost';    # 只授予select权限给alex

grant all on *.* to 'alex'@'localhost';    # 授予所有权限给alex(所有库下的所有表),相当于root,但没有grant权限

revoke select on *.* from 'alex'@'localhost';    # 移除select权限从alex用户

 

grant select on db1.* to 'alex'@'localhost';    # 授予select权限给db1下的所有表

revoke select on db1.* from 'alex'@'localhost';    # 移除权限

 

grant select,update on db1.t2 to 'alex'@'localhost';    # 授予select,update权限db1.t2表

revoke select,update on db1.t2 from 'alex'@'localhost';    # 移除权限

 

grant select(id, name), update(age) on db1.t2 to 'alex'@'localhost';   # 对t2表的id,name字段有查权限,对age字段有更新权限

revoke select(id, name), update(age) from db1.t2 to 'alex'@'localhost';    # 移除权限

 

创建用户+授权 可以在一条命令中执行:

grant all on *.* to 'root'@'%' identified by '123';

 

可通过查看mysql.db表看到每次执行授权后记录的变化:

select * from mysql.db\G;

 

 

 

 

 

 

版权声明:
作者:admin
链接:https://www.chenxie.net/archives/2059.html
来源:蜀小陈
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
< <上一篇
下一篇>>