MySQL回收某一授權

來源:互聯網
上載者:User

MySQL回收某一授權

建立測試表
create table t1(id int);
 create table t2(id int);
 create table t3(id int);
 create table t4(msg varchar(100));

如果授權的時候圖省事兒,使用萬用字元授權.
grant select,insert,update,delete on mvbox.* to 'xx'@'localhost' identified by 'xx';

如果以後需要回收某一張表許可權的時候,就會比較麻煩.
mysql> show grants for xx@'localhost';
 +-----------------------------------------------------------------------------------------------------------+
 | Grants for xx@localhost                                                                                  |
 +-----------------------------------------------------------------------------------------------------------+
 | GRANT USAGE ON *.* TO 'xx'@'localhost' IDENTIFIED BY PASSWORD '*B30134364A2D14319904C2C807363CF2C81ABD5B' |
 | GRANT SELECT, INSERT, UPDATE, DELETE ON `mvbox`.* TO 'xx'@'localhost'                                    |
 +-----------------------------------------------------------------------------------------------------------+
 2 rows in set (0.00 sec)

 mysql> revoke insert on mvbox.t1 from xx@'localhost';
ERROR 1147 (42000): There is no such grant defined for user 'xx' on host 'localhost' on table 't1'
 mysql>

因為授權是使用的萬用字元,回收也需要使用萬用字元.

如果需要回收t1表的insert許可權,可以使用如下的觸發器.

delimiter //
 
CREATE TRIGGER tri1 BEFORE INSERT ON t1 FOR EACH ROW

BEGIN

    DECLARE msg varchar(100);

    DECLARE cu varchar(40);

    set cu=(select substring_index((select user()),'@',1)) ;

    IF cu='xx' THEN

            set msg = concat(cu,"You have no right to operate data!please connect DBAs");

            SIGNAL SQLSTATE 'HY000' SET MESSAGE_TEXT = msg;

    END IF;

END;

//

delimiter ;
這時,以xx登入,insert t1表則報錯如下
mysql> insert into t1 values(10);
 ERROR 1644 (HY000): xx You have no right to operate data!please connect DBAs

 


這裡需要注意的是current_user,user函數,在觸發器調用的時候,返回的內容是不一樣的.
刪除原來的觸發器,建立一個觸發器測試


drop trigger tri1;
 
delimiter //

CREATE TRIGGER `tri2` BEFORE INSERT ON `t1` FOR EACH ROW

BEGIN

    insert into t4 values(concat(current_user(),',',user(),',',session_user()));

END;

//

delimiter ;
使用xx使用者登入,執行如下命令
mysql> insert into t1 values(10);
 Query OK, 1 row affected (0.00 sec)

 mysql> select * from t4;
 +------------------------------------------+
 | msg                                      |
 +------------------------------------------+
 | root@localhost,xx@localhost,xx@localhost |
 +------------------------------------------+
 1 row in set (0.00 sec)

可以發現在觸發器中,current_user()返回的是觸發器的定義者.
而user(),session_user()才是串連的使用者.

mysql>

本文永久更新連結地址:

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.