一、查看是否开启定时功能
select @@event_scheduler;
-- 或
show variables like 'event_scheduler';
二、开启定时任务功能
set GLOBAL event_scheduler=ON;
三、写自己的sql逻辑
DELIMITER //
create procedure del_7_data() -- 名字
BEGIN
delete from a_test where created_at>'2019-02-03 09:35:00'; -- 这个是自己的sql逻辑
END//
DELIMITER ;
四、设置事件执行时间
create event if not exists e_deleteData
on schedule every 1 day STARTS '2019-07-19 12:00:00'
on completion preserve
do call del_7_data();
五、执行
ALTER EVENT e_deleteData ON COMPLETION PRESERVE ENABLE;
六、查看和删除
-- 查看任务
select * from mysql.proc where db='数据库名';
SELECT * FROM information_schema.EVENTS;
-- 删除任务
drop event e_deleteData;
drop procedure del_7_data