MySql-用户管理

用户管理

主要是为了控制权限,让不同的开发者仅能操作属于自己的业务范围内的数据。可以通过select * from mysql.user \G;来查看当前用户以及用户权限

创建mysql账户

create user 用户名@主机名 identified by 密码;

create user jerry@'192.168.11.210' identified by '123';  # 给指定的'192.168.11.210'创建用户名nick,密码123。

该用户名只能在指定的ip上使用。如果要在所有ip上创建时,可以在主机名上写%。在创建账户时,该账户是没有任何权限,所以在创建用户后要给账户授权

授权语句

授权语句执行时,如果账号不存在会自动创建账户,其次只有root才能为其他账户授权。授权时会根据权限来将账户密码存在不同的表中

user表:存与用户相关的信息

db表:用户的数据库权限信息

tables_priv:用户的表权限

columns_priv:用户的字段权限

grant 权限1,权限2... on 数据库.表 to 用户名 @主机名 [identified by '密码'] # 给用户分配权限,如果用户不存在时,可以在分配权限的同时创建该用户

# 权限:all表示所有权限,其他权限包括:insert、update、delete、select...
# 数据库和表:* 表示全部
# 主机名:可以是指定的某个ip地址,可以是localhost|127.0.0.1表示本机,可以是%表示所有

grant all on *.* to tom@"localhost" identified by "123";
#该语句中的all 增删改查所有权限 但是不包括grant权限
#*.* 表示任何数据库 任何表 存储在user表

grant all on *.* to toms@"%" identified by "123";
# host 为% 表示 该账户可以在任何主机上登录但是不包括localhost
grant all on *.* to toms@"localhost" identified by "123";
# 继续执行 上述语句保证localhost也可以登录该账户

grant all on db.* to tom@"localhost" identified by "123" 
#db.* 该用户可以操作db数据库的任何表 存储在 db表

grant all on db.t1 to tom@"localhost" identified by "123" 
#db.* 该用户可以操作db数据库的t1表 存储在 table_privi表

grant select(id) on db.t1 to tom@"localhost" identified by "123" 
#精确到字段操作级别,该用户只能查询 db下的t1表,存储在columns_priv表

grant all on *.* to tom@"localhost" identified by "123" with grant option;
#with grant option 表示该账户可以将权限授予其他用户


REVOKE all privileges [column]  on 数据库名.表名 from user@"host";  #收回权限

drop user 用户名@"host"  #删除用户

show grants for 用户名 @ip地址;  # 查看权限

flush privileges;
#刷新权限表 一些时候权限信息可能会有所延迟 可以执行该语句立即刷新权限信息
posted @ 2019-07-17 20:28  Never&say&die  阅读(167)  评论(0编辑  收藏  举报