1.查看even_schedule是否开启,如果没开启,使用下面命令开启,阿里云服务器需要在管理后台开启 SET GLOBAL event_scheduler=ON; 2.创建PROCEDURE存储过程 DROP PROCEDURE IF EXISTS KeepMessageDatasWith60Days; CREATE PROCEDURE KeepMessageDatasWith60Days() BEGIN SET @cou =(SELECT COUNT(1) FROM message WHERE createTime < DATE_FORMAT(DATE_SUB(NOW(),INTERVAL 60 DAY),'%Y%m%d')); WHILE @cou>0 DO DELETE FROM message WHERE createTime<date_format(date_sub(now(),interval 60="" day),'%y%m%d')="" limit="" 100000;="" <="" div=""> END WHILE; END; 3.创建event定时任务 DROP EVENT IF EXISTS KeepMessageDatasWith60Days; CREATE EVENT KeepMessageDatasWith60Days ON SCHEDULE EVERY 2 MONTH STARTS DATE_ADD(DATE_ADD(DATE_SUB(CURDATE(),INTERVAL DAY(CURDATE())-1 DAY),INTERVAL 2 MONTH),INTERVAL 1 HOUR) #每两个月的第一天凌晨1点执行 ON COMPLETION PRESERVE DISABLE DO CALL KeepMessageDatasWith60Days(); ALTER EVENT KeepMessageDatasWith60Days ON COMPLETION PRESERVE ENABLE;
标签:KeepMessageDatasWith60Days,INTERVAL,60,天前,mysql,DATE,DAY,PROCEDURE From: https://www.cnblogs.com/zhangdapangzo/p/17210825.html