mysql grant 用户权限总结

用户权限管理主要有以下作用: 
1. 可以限制用户访问哪些库、哪些表 
2. 可以限制用户对哪些表执行SELECT、CREATE、DELETE、DELETE、ALTER等操作 
3. 可以限制用户登录的IP或域名 
4. 可以限制用户自己的权限是否可以授权给别的用户

一、用户授权

mysql> grant all privileges on *.* to 'yangxin'@'%' identified by 'yangxin123456' with grant option;

  • all privileges:表示将所有权限授予给用户。也可指定具体的权限,如:SELECT、CREATE、DROP等。
  • on:表示这些权限对哪些数据库和表生效,格式:数据库名.表名,这里写“*”表示所有数据库,所有表。如果我要指定将权限应用到test库的user表中,可以这么写:test.user
  • to:将权限授予哪个用户。格式:”用户名”@”登录IP或域名”。%表示没有限制,在任何主机都可以登录。比如:”yangxin”@”192.168.0.%”,表示yangxin这个用户只能在192.168.0IP段登录
  • identified by:指定用户的登录密码
  • with grant option:表示允许用户将自己的权限授权给其它用户

二、刷新权限

对用户做了权限变更之后,一定记得重新加载一下权限,将权限信息从内存中写入数据库。

mysql> flush privileges;

三、查看用户权限

mysql> grant select,create,drop,update,alter on *.* to 'yangxin'@'localhost' identified by 'yangxin0917' with grant option;

mysql> show grants for 'yangxin'@'localhost';

四、回收权限

删除yangxin这个用户的create权限,该用户将不能创建数据库和表。

mysql> revoke create on *.* from 'yangxin@localhost';

mysql> flush privileges;

五、删除用户

ysql> select host,user from user;
+---------------+---------+
| host          | user    |
+---------------+---------+
| %             | root    |
| %             | test3   |
| %             | yx      |
| 192.168.0.%   | root    |
| 192.168.0.%   | test2   |
| 192.168.0.109 | test    |
| ::1           | yangxin |
| localhost     | yangxin |
+---------------+---------+
8 rows in set (0.00 sec)
mysql> drop user 'yangxin'@'localhost';

  

原文地址:https://www.cnblogs.com/sunlong88/p/9190878.html