• 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 防止级联回收。

  • 相关阅读:
    Aix_bugzilla
    aix Mysql安装 Oracle官方教程
    Aix6.1安装openssh
    日媒:阿里巴巴上市融资或超Facebook
    设计模式(一)---单例模式
    Handler具体解释系列(七)——Activity.runOnUiThread()方法具体解释
    TCP/IP协议族——IP工作原理及实例具体解释(上)
    leetCode 41.First Missing Positive (第一个丢失的正数) 解题思路和方法
    Material Design之RecyclerView的使用(一)
    jQuery和CSS3超酷表单美化插件
  • 原文地址:https://www.cnblogs.com/lhat/p/6901511.html
Copyright © 2020-2023  润新知