MySQL Trigger get current query that caused trigger to fire

后端 未结 3 1717
滥情空心
滥情空心 2021-01-22 19:30

I have scoured the web for the past 4 hours in search of a solution but all i can find is:

You cant. Impossible. Not gonna happen.

I dont like that approach.

3条回答
  •  借酒劲吻你
    2021-01-22 20:16

    We have investigated the issue and found that "DECLARE" doesn't work in the sample above, also the general_log flag should be enabled on the server. Finally we have the following trigger code:

    DELIMITER $$
    
    DROP TRIGGER IF EXISTS `our_trigger`$$
    CREATE TRIGGER `our_trigger` 
    BEFORE UPDATE ON `our_table_for_debug` 
    FOR EACH ROW BEGIN 
    
    SELECT argument INTO @tquery FROM mysql.general_log where thread_id = connection_id() and argument like 'update%' order by event_time desc limit 1;
    INSERT INTO `our_log_table` (`query`) VALUES (@tquery);
    
    END IF;
    
    END$$
    
    DELIMITER ;
    

    "our_log_table" is table with single mediumtext field named query. to enable general log you should run this query: general_log = 1; or enable it in mysql settings directly.

提交回复
热议问题