1.设置开启event scheduler
SET GLOBAL event_scheduler=1;
2.验证event_scheduler是否已经开启
show variables like ‘%event%’; #值为 ON 表明已开启
SHOW VARIABLES LIKE ‘event_scheduler’;
3.创建event
DROP EVENT IF EXISTS e_delete_upvote;
CREATE EVENT e_delete_upvote
ON SCHEDULE EVERY 1 day STARTS DATE_ADD(current_date(),INTERVAL 1 DAY) ON COMPLETION PRESERVE ENABLE
DO
TRUNCATE TABLE dreamland.upvote;
说明: ON SCHEDULE EVERY 1 day 指定循环间隔为每天执行
STARTS date_add(concat(current_date(), ‘ 23:59:00’), interval 0 second)
指定运行时间为23:59:00 ON COMPLETION PRESERVE ENABLE 指定创建完成后即启用,否则需手动启动
4.手动关闭事件
ALTER EVENT e_delete_upvote DISABLE;
5.手动该事件为可用
ALTER EVENT e_delete_upvote ENABLE;
6.查看已有的定时事件任务
SHOW EVENTS;
7.删除事件
DROP EVENT e_delete_upvote;