MySQL用户和权限详解

MySQL 用户和权限

  • 本文主要分析和记录MySQL中新建、删除用户和授予、撤销相关权限的问题

参考文献

1. 用户

  • 1.创建用户
create user 'testUser'@'localhost' identity by '密码';
create user 'testUser'@'%' identity by '密码';
create user 'testUser'@'192.168.1.10' identity by '密码';

#%表示所有主机可接入,localhost标志仅本主机可接入,也可以指定主机IP如192.168.1.10
#如果需要远程登录的,将localhost改成%

如果报错:出现ERROR 1364 (HY000): Field ‘ssl_cipher’ doesn’t have a default value
解决方法:

#打开my.ini,查找
sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
#修改为
sql-mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
#然后重启MYSQL
  • 2.授予权限和撤销权限
#授予,下文详述具体怎么授予?授予什么权限?
grant all on *.* to 'testUser'@'%' identified by '密码';

#刷新
flush privileges;

#撤销
revoke all on *.* from 'testUser'@'%';

#刷新
flush privileges;
  • 3.删除用户
drop user 'testUser'@'%';

2. 权限

授予用户权限的基本格式如下:grant 权限 数据库对象.表对象 to 用户@主机

  • 1 授予指定用户testUser查询、插入、更新、删除testdb的所有表数据的权限:
grant select on testdb.* to 'testUser'@'%';  
grant insert on testdb.* to 'testUser'@'%';  #其中*第通配符,表示所有
grant update on testdb.* to 'testUser'@'%';
grant delete on testdb.* to 'testUser'@'%';

#总结成一条命令
grant select,insert,update,delete on testdb.* to 'testUser'@'%';
  • 2.授予指定用户testUser创建和删除表、索引、视图、存储过程的权限
grant create on testdb.* to 'testUser'@'%';  #其中*第通配符,表示所有
grant alter on testdb.* to 'testUser'@'%';
grant drop on testdb.* to 'testUser'@'%';

#总结成一条命令
grant create,alter,drop on testdb.* to 'testUser'@'%';

#外键权限
grant reference on testdb.* to 'testUser'@'%';

#索引权限
grant index on testdb.* to 'testUser'@'%';

#视图权限
grant create view on testdb.* to 'testUser'@'%';
grant show view on testdb.* to 'testUser'@'%';

#存储过程权限
grant create routine on testdb.* to 'testUser'@'%';
grant alter routine on testdb.* to 'testUser'@'%';
grant execute on testdb.* to 'testUser'@'%';
  • 3.授予指定用户管理数据库的权限

#仅管理testdb数据库
grant all privileges on testdb.* to 'testUser'@'%';

#管理所有数据库
grant all privileges on *.* to 'testUser'@'%';
#其中privileges关键字可省略
  • 4.MySQL权限的作用层次:5个层次
#1#作用在整个MySQL服务器上
grant all privileges on *.* to 'testUser'@'%';

#2#作用在单个数据库上
grant all privileges on testdb.* to 'testUser'@'%';

#3#作用在单个数据表上
grant all privileges on testdb.testTable to 'testUser'@'%';

#4#作用在单个数据表的若干个列上
grant select(id, name, home, phone) on testdb.testTable to 'testUser'@'%'; #select可以改其他,字段根据实际修改

#5#作用在存储过程、函数上
grant execute on procedure testdb.tsetfunc to 'testUser'@'%';
grant execute on function testdb.tsetfunc to 'testUser'@'%';
  • 5.查看权限
#查看当前用户的权限
show grants;

#查看mysql中其他用户的权限
show grants for 'testUser'@'%';
  • 6.撤销权限

revoke跟grant的使用语法差不多,只需要将关键字to换成from

#授予
grant all on *.* to 'testUser'@'%';

#撤销
revoke all on *.* from 'testUser'@'%';
  • 7.其他

mysql授权表共有5个表:user、db、host、tables_priv和columns_priv

  • user表
    user表列出可以连接服务器的用户及其口令,并且它指定他们有哪种全局(超级用户)权限。在user表启用的任何权限均是全局权限,并适用于所有数据库。

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

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

  • tables_priv表
    tables_priv表指定表级权限,在这里指定的一个权限适用于一个表的所有列。

  • columns_priv表
    columns_priv表指定列级权限。这里指定的权限适用于一个表的特定列。

  • 8.注意事项

  • grant, revoke 用户权限后,该用户只有重新连接 MySQL 数据库,权限才能生效。

  • 如果想让授权的用户,也可以将这些权限 grant 给其他用户,需要选项 grant option


grant all on testdb.* to 'testUser'@'%' with grant option;

猜你喜欢

转载自blog.csdn.net/a791693310/article/details/81083864
今日推荐