MySQL定时任务EVENT事件的使用方法
作者:风行無痕 时间:2024-01-17 03:26:20
一、查看定时策略是否开启
show variables like '%event_scheduler%';
* on这里是启用的
查看进程:会看到一个用户为event_scheduler,执行状态为Waiting on empty queue的进程
show PROCESSLIST;
开启定时策略:
set global event_scheduler = 1;
关闭定时策略:
set global event_scheduler = 0;
在my.cnf配置文件中开启事件(永久开启,设置后需要重启MySQL才会生效)
配置文件的[mysqld]部分加上event_scheduler=ON
二、创建定时任务
-- 创建定时任务
create EVENT sync_lastday_attendance
ON SCHEDULE
EVERY '1' DAY STARTS '2021-09-19 05:00:00'
DO call sync_lastday_attendance()
解释:
1、create event sync_lastday_attendance:是创建名为sync_lastday_attendance的事件
2、EVERY '1' DAY 创建周期定时的规则,意思是每天执行一次
3、STARTS '2021-09-19 05:00:00'表示在具体某个时间执行,是2021-09-19凌晨5点整开始执行
3、可选 on completion preserve disable是表示创建后并不开始生效。
4、do call sync_lastday_attendance()是该event(事件)的操作内容,这里是调用名为sync_lastday_attendance()的存储过程
三、定时任务操作
查看所有定时任务
SELECT event_name,event_definition,interval_value,interval_field,status FROM information_schema.EVENTS;
查看指定定时任务创建详细
show create EVENT sync_lastday_attendance;
修改任务
-- 创建定时任务 8点执行 调用test()存储过程
ALTER EVENT sync_lastday_attendance
ON SCHEDULE
EVERY '1' DAY STARTS '2021-09-19 08:00:00'
DO call test()
开启定时任务
alter event sync_lastday_attendance on completion preserve enable;//开启定时任务
关闭定时任务
alter event sync_lastday_attendance on completion preserve disable;//关闭定时任务
删除定时任务
drop event sync_lastday_attendance; //删除定时任务
四、定时规则
1、周期执行(EVERY)
参数单位有:second、minute、hour、day、week(周)、quarter(季度)、month、year
on schedule every 1 week //每周执行1次
on schedule every 1 day //每天执行1次
2、在具体某个时间执行(AT)
on schedule at current_timestamp()+interval 5 day //5天后执行
on schedule at '2021-09-19 05:00:00' //在2021年9月19日,5点整执行
3、在某个时间段执行(STARTS ENDS)
on schedule every 1 day starts current_timestamp()+interval 3 day ends current_timestamp()+interval 1 month
//3天后开始每天都执行一次到下个月底结束
on schedule every 1 day ends current_timestamp()+interval 3 day
//从现在起每天执行,执行3天
来源:https://blog.csdn.net/gmaaa123/article/details/120379739
标签:MySQL,定时任务,EVENT事件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python使用pydub模块转换音频格式以及对音频进行剪辑
2023-03-22 02:17:37
![](https://img.aspxhome.com/file/2023/4/97344_0s.jpg)
解决Python报错:SyntaxError: invalid character ‘,‘ (U+FF0C)
2023-01-24 06:12:57
![](https://img.aspxhome.com/file/2023/5/124215_0s.png)
python实现音乐播放和下载小程序功能
2023-07-03 17:59:03
![](https://img.aspxhome.com/file/2023/5/105855_0s.jpg)
Python使用xpath实现图片爬取
2023-07-10 16:45:42
sqlserver、mysql获取连接字符串步骤
2024-01-22 00:49:33
MySQL字符串索引更合理的创建规则讨论
2024-01-24 19:10:55
![](https://img.aspxhome.com/file/2023/0/68110_0s.png)
利用python+ffmpeg合并B站视频及格式转换的实例代码
2021-06-09 21:14:00
python获取全国最新省市区数据并存入表实例代码
2021-10-19 14:16:23
![](https://img.aspxhome.com/file/2023/1/76971_0s.png)
Python中可变和不可变对象的深入讲解
2022-12-29 21:31:21
![](https://img.aspxhome.com/file/2023/5/87635_0s.png)
JAVASCRIPT实现的WEB页面跳转以及页面间传值方法
2023-08-23 04:56:09
python实现高斯投影正反算方式
2022-11-17 08:58:19
7分钟读懂Go的临时对象池pool以及其应用场景
2024-02-05 17:22:17
Python变量和数据类型和数据类型的转换
2023-09-28 07:44:34
![](https://img.aspxhome.com/file/2023/5/87945_0s.png)
pycharm 2019 最新激活方式(pycharm破解、激活)
2021-09-15 13:07:01
![](https://img.aspxhome.com/file/2023/9/89219_0s.jpg)
javascript实现九宫格相加数值相等
2024-04-17 10:32:53
![](https://img.aspxhome.com/file/2023/5/136235_0s.png)
分析运行中的 Python 进程详细解析
2021-09-19 14:47:30
Flask使用SocketIO实现WebSocket与Vue进行实时推送
2023-02-19 05:05:45
![](https://img.aspxhome.com/file/2023/6/112416_0s.jpg)
Django权限系统auth模块用法解读
2021-03-22 13:27:00
简单了解python 邮件模块的使用方法
2021-07-11 00:08:14
Jenkins配置maven项目之打包、部署、发布的全过程
2023-08-07 19:14:29
![](https://img.aspxhome.com/file/2023/1/61841_0s.jpg)