Mysql定时器定时删除表数据
由于测试环境有张日志表没定时2分钟程序就狂插数据,导致不到1一个月时间,这张日志表就占用了6.7G的空间,但是日志刷新较快,
有些日志就没什么作用,就写了个定时器,定期删除这张表的数据
首先先查看mysql是否开启定时任务开关
# SHOW VARIABLES LIKE 'event_scheduler';
---------------------
Value为ON则已打开,OFF则关闭
如果是OFF,就先打开:
# SET GLOBAL event_scheduler = ON;
然后创建我们想要的定时器
DELIMITER $$
DROP EVENT IF EXISTS deleteLog;
CREATE EVENT deleteLog
ON SCHEDULE EVERY 300 SECOND(每5分钟执行一次)
ON COMPLETION PRESERVE
DO BEGIN
delete from ftp_log where TO_DAYS(now())-TO_DAYS(createOn)>2;
END$$
DELIMITER ;
该脚本的意思是:每300秒执行一次计划,执行的动作为删除两天前的数据
DELIMITER $$
DROP EVENT IF EXISTS deleteFrameInfo;
CREATE EVENT deleteFrameInfo
ON SCHEDULE EVERY 1 DAY(每1天执行一次)
STARTS '2021-5-1 00:00:00'
ON COMPLETION PRESERVE
DO BEGIN
delete from vtiger_frame_info where TO_DAYS(now())-TO_DAYS(FROM_UNIXTIME(time,'%Y-%m-%d'))>30;(time是表中记录时间的字段)
END$$
DELIMITER;
创建完成后,查看定时器
# select * from mysql.event;