mysql计划任务每天定时执行,每天的零点执行一个存储过程
1、开启数据库定时调度
show variables like '%sche%'; //查看是否开启
select * from mysql.user where user='root'
set global event_scheduler =1 //设置开启
2、创建存储过程,这里的存储过程主要提供给mysql的定时器event来调用去执行:
CREATE DEFINER=`root`@`%` PROCEDURE `update_level_status`()
COMMENT '根据过期时间更新会员状态'
BEGIN
UPDATE user_activity SET `level`=0 WHERE `level`=1 AND (expire_time IS NULL OR `expire_time`<CURRENT_TIMESTAMP());
END
3、开启定时器event
CREATE DEFINER=`root`@`%` EVENT `time_update_level_status` ON SCHEDULE EVERY 1 DAY STARTS '2019-12-04 00:00:00' ON COMPLETION NOT PRESERVE ENABLE COMMENT '定时刷新level状态' DO CALL update_level_status