建立监控表:
CREATE TABLE `t_event_history` ( id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT, `dbname` VARCHAR(128) NOT NULL DEFAULT 'cat', `eventname` VARCHAR(128) NOT NULL DEFAULT 'BakupCat', `starttime` DATETIME NOT NULL DEFAULT '2000-01-01 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 ) ENGINE=INNODB DEFAULT CHARSET=utf8;
根据以下建模板创建作业
#请注意根据实际情况修改相关信息
DELIMITER $$ create DEFINER=`root`@`%` EVENT `Job_Backup_tb_cat` ON SCHEDULE EVERY 1 DAY STARTS '2016-09-07 01:00:00' ON COMPLETION NOT PRESERVE ENABLE DO BEGIN DECLARE v_starttime DATETIME DEFAULT NOW(); DECLARE v_randno INTEGER DEFAULT FLOOR(RAND()*100001); DECLARE r_code CHAR(5) DEFAULT '00000'; DECLARE r_msg TEXT; DECLARE v_error INTEGER; INSERT INTO `t_event_history` (`starttime`,`issuccess`,randno) VALUES(v_starttime,0,v_randno); BEGIN #异常处理段 DECLARE CONTINUE HANDLER FOR SQLEXCEPTION BEGIN SET v_error = 1; GET DIAGNOSTICS CONDITION 1 r_code = RETURNED_SQLSTATE , r_msg = MESSAGE_TEXT; END; #此处为实际调用的用户程序过程 CALL Backup_tb_cat(); END; UPDATE t_event_history SET endtime=NOW(),issuccess=1,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 ;
通过查询t_event_history表,我们就知道event何时执行,执行是否成功,执行时长,出错时的错误信息,为管理我们日常调度计划提供很大方便。