1.查看事件调度是否开启,on表示开启
show variables like 'event_scheduler';
select @@event_scheduler;
2.开启、关闭事件调度器
set global event_scheduler = ON ;
set global event_scheduler = OFF;
注意:上面的设置方式,mysql重启后会回到原来的状态。如果永久开启调度器,需要在my.ini中添加
event_scheduler=on
重启服务器生效
3.创建事件
CREATE
[DEFINER={user | CURRENT_USER}] 事件执行时检查权限的用户
EVENT [IF NOT EXISTS] event_name
ON SCHEDULE schedule 事件执行时间、周期
[ON COMPLETION [NOT] PRESERVE] 是否循环执行,默认一次执行
[ENABLE | DISABLE | DISABLE ON SLAVE] 活动、关闭、从机中关闭
[COMMENT 'comment'] 备注
DO event_body; 执行代码(sql存储过程egin end多条语句事件)
on schedule 定时语句
AT timestamp [+ INTERVAL interval] ...
| EVERY interval
[STARTS timestamp [+ INTERVAL interval] ...]
[ENDS timestamp [+ INTERVAL interval] ...]
quantity {YEAR | QUARTER | MONTH | DAY | HOUR | MINUTE |
WEEK | SECOND | YEAR_MONTH | DAY_HOUR | DAY_MINUTE |
DAY_SECOND | HOUR_MINUTE | HOUR_SECOND | MINUTE_SECOND}
interval 间隔
每分钟
ON SCHEDULE EVERY '1' MINUTE STARTS '2020-09-02 15:35:49' ENDS '2020-09-02 16:00:49'
每天凌晨一点
ON SCHEDULE EVERY 1 DAY STARTS DATE_ADD(DATE_ADD(CURDATE(), INTERVAL 1 DAY), INTERVAL 1 HOUR)
每个月 的第一天凌晨1点执行
ON SCHEDULE EVERY 1 MONTH STARTS DATE_ADD(DATE_ADD(DATE_SUB(CURDATE(),INTERVAL DAY(CURDATE())-1 DAY),INTERVAL 1 MONTH),INTERVAL 1 HOUR)
每三个月,从现在的第一周开始
ON SCHEDULE EVERY 3 MONTH STARTS CURRENT_TIMESTAMP + 1 WEEK
每12小时 从现在起30分钟后开始 四个星期后结束
ON SCHEDULE EVERY 12 HOUR STARTS CURRENT_TIMESTAMP + INTERVAL 30 MINUTE ENDS CURRENT_TIMESTAMP + INTERVAL 4 WEEK
补充:mysql日期函数
现在日期
select CURDATE()
2020-09-03
距离月初相差多少天
select DAY(CURDATE())-1
2
日期减去间隔时间
select DATE_SUB(CURDATE(),INTERVAL DAY(CURDATE())-1 DAY)
2020-09-01
日期增加间隔时间
select DATE_ADD(DATE_ADD(CURDATE(), INTERVAL 1 DAY), INTERVAL 1 HOUR)
2020-09-04 01:00:00
案例:
1.创建表
CREATE TABLE `tb_user` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户编号', `name` varchar(30) NOT NULL COMMENT '用户姓名', `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '创建时间', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=223 DEFAULT CHARSET=utf8mb4 COMMENT='用户信息表'; CREATE TABLE `tb_total` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户编号', `userNumber` int(10) NOT NULL COMMENT '汇总', `createtime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '创建时间', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=1093 DEFAULT CHARSET=utf8mb4 COMMENT='用户信息汇总表';
创建存储过程
CREATE DEFINER=`root`@`localhost` PROCEDURE `p_total`() BEGIN DECLARE n_total INT default 0; SELECT COUNT(*) INTO n_total FROM test.tb_user; INSERT INTO tb_total (userNumber,createtime) VALUES(n_total,NOW()); END
创建事件
事件1 每5s执行一次
-- 创建事件 CREATE EVENT IF NOT EXISTS event_user ON SCHEDULE EVERY 5 SECOND ON COMPLETION PRESERVE COMMENT '新增用户信息定时任务' DO INSERT INTO tb_user(name,create_time) VALUES('pan_junbiao的博客',NOW());
事件2 汇总 每天凌晨一点
CREATE EVENT IF NOT EXISTS e_autoTotal
ON SCHEDULE EVERY 1 DAY STARTS DATE_ADD(DATE_ADD(CURDATE(), INTERVAL 1 DAY), INTERVAL 1 HOUR)
ON COMPLETION PRESERVE ENABLE
DO CALL p_total();
查询事件
SELECT * FROM information_schema.events;
启动事件
ALTER EVENT event_user ENABLE;
关闭事件
ALTER EVENT event_user DISABLE;
删除事件
DROP EVENT IF EXISTS event_user;
修改事件
ALTER [DEFINER={user | CURRENT_USER}] EVENT [IF NOT EXISTS] event_name ON SCHEDULE schedule [ON COMPLETION [NOT] PRESERVE] [ENABLE | DISABLE | DISABLE ON SLAVE] [COMMENT 'comment'] DO event_body;
-- 修改事件 调度内容
ALTER EVENT `event_proc`
DO begin
call test.p_total();
call test.proc_event_proc();
end;