zoukankan      html  css  js  c++  java
  • MySQL数据库用户、角色、授权

    权限包括  insert   delete   update   select   all privileges 

    登录MySQL

    > mysql -uroot -p
    Enter password: ****

    1. 添加用户

    mysql> insert into mysql.user(host,user,password) values('%', 'xiaoming', password('xiaoming123'));
    mysql> flush privileges;

    现在可以使用帐号(xiaoming,xiaoming123)登录MySQL了。但是只有默认权限,仅能操作两个数据库(information_schema和test)

    2. 授权

    grant <权限列表> on <关系> to <用户/角色>

    等价于

    grant <权限列表> on <关系> to <用户/角色>@'%' 

    表示授权给来自所有主机该用户

     @<主机>  用来指定用户登录的主机

    grant insert on school.* to xiaoming

    此时,用户xiaoming就拥有了对数据库school的insert权限。

    mysql> show databases; ---授权前
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | test               |
    +--------------------+
    2 rows in set (0.00 sec)
    
    mysql> show databases; --- 授权后
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | school             |
    | test               |
    +--------------------+
    3 rows in set (0.00 sec)
    
    mysql> use school;
    Database changed
    mysql> insert into student(name,score) values('xiaoming',60);
    Query OK, 1 row affected (0.08 sec)
    
    mysql> select * from student;
    ERROR 1142 (42000): SELECT command denied to user 'xiaoming'@'10.0.2.2' for table 'student'

    3. 添加用户并授权

    grant <权限> on <关系> to <用户> identified by <密码> [with grant options]

    mysql> grant select on school.* to xiaoqiang@'%' identified by 'xiaoqiang123';
    mysql> flush privileges;

     with grant options  参数表示被授权的用户可以将权限再授权给其他用户。

    4. 查看用户权限

     show grants for <用户/角色>

    mysql> show grants for xiaoqiang;
    +----------------------------------------------------------------------------------------------------------+
    | Grants for xiaoqiang@%                                                                                   |
    +----------------------------------------------------------------------------------------------------------+
    | GRANT USAGE ON *.* TO 'xiaoqiang'@'%' IDENTIFIED BY PASSWORD '*CFE5D9A8AB243F294A8D3C5B7F2B6BDCF7F71DB5' |
    | GRANT SELECT ON `school`.* TO 'xiaoqiang'@'%'                                                            |
    +----------------------------------------------------------------------------------------------------------+
    2 rows in set

    其中的 “usage” 表示默认权限

    5. 回收权限

    revoke <权限> on <关系> from <用户/角色>

    mysql> revoke select on school.* from xiaoqiang;
    mysql> flush privileges;

    在用户下次登录后生效

    默认是级联回收,即:会将该用户的该权限以及该用户授予给其他用户的该权限全部回收。

    revoke <权限> on <关系> from <用户/角色> restrict

    使用 restrict 防止级联回收。

  • 相关阅读:
    UVA 11488 Hyper Prefix Sets (字典树)
    UVALive 3295 Counting Triangles
    POJ 2752 Seek the Name, Seek the Fame (KMP)
    UVA 11584 Partitioning by Palindromes (字符串区间dp)
    UVA 11100 The Trip, 2007 (贪心)
    JXNU暑期选拔赛
    计蒜客---N的-2进制表示
    计蒜客---线段的总长
    计蒜客---最大质因数
    JustOj 2009: P1016 (dp)
  • 原文地址:https://www.cnblogs.com/lhat/p/6901511.html
Copyright © 2011-2022 走看看