Mysql Event scheduling history
The current mysql version does not have Event execution history information. In order to check whether the Event is executed normally and the execution result conveniently, the following two steps can be used:
1, Create job execution Event history table
- CREATE TABLE `mysql`.`t_event_history` (
- `dbname` VARCHAR(128) NOT NULL DEFAULT '',
- `eventname` VARCHAR(128) NOT NULL DEFAULT '',
- `starttime` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
- `endtime` DATETIME DEFAULT NULL,
- `issuccess` INT(11) DEFAULT NULL,
- `duration` INT(11) DEFAULT NULL,
- `errormessage` VARCHAR(512) DEFAULT NULL,
- `randno` INT(11) DEFAULT NULL,
- PRIMARY KEY (`dbname`,`eventname`,`starttime`),
- KEY `ix_endtime` (`endtime`),
- KEY `ix_starttime_randno` (`starttime`,`randno`)
- ) ENGINE=INNODB DEFAULT CHARSET=utf8;
2, Create a job based on the following template
#Please pay attention to modify the relevant information according to the actual situation
- DELIMITER $$
- CREATE DEFINER=`root`@`localhost` EVENT `e_test` ON SCHEDULE
- #Modify the following scheduling information
- EVERY 1 DAY STARTS '2014-01-03 01:00:00' ON COMPLETION PRESERVE ENABLE DO
- BEGIN
- DECLARE r_code CHAR(5) DEFAULT '00000';
- DECLARE r_msg TEXT;
- DECLARE v_error INTEGER;
- DECLARE v_starttime DATETIME DEFAULT NOW();
- DECLARE v_randno INTEGER DEFAULT FLOOR(RAND()*100001);
- INSERT INTO mysql.t_event_history (dbname,eventname,starttime,randno)
- #Modify the following job name (the name of the job)
- VALUES(DATABASE(),'e_test', v_starttime,v_randno);
- BEGIN
- #Exception handling section
- DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
- BEGIN
- SET v_error = 1;
- GET DIAGNOSTICS CONDITION 1 r_code = RETURNED_SQLSTATE , r_msg = MESSAGE_TEXT;
- END;
- #Here is the actual called user program procedure
- CALL test.usp_test1();
- END;
- UPDATE mysql.t_event_history SET endtime=NOW(),issuccess=ISNULL(v_error),duration=TIMESTAMPDIFF(SECOND,starttime,NOW()), errormessage=CONCAT('error=',r_code,', message=',r_msg),randno=NULL WHERE starttime=v_starttime AND randno=v_randno;
- END$$
- DELIMITER ;
By querying the mysql.t_event_history table, we can know when the event is executed, whether it is successful, how long it takes to execute, and the error information when there is an error. It is very convenient for us to manage our daily scheduling plan.