当前位置:网站首页>Mysql中事件和定时任务
Mysql中事件和定时任务
2022-08-11 04:04:00 【无敌小田田】
一、简介
事件(event)是MySQL在相应的时刻调用的过程式数据库对象。是MYSQL一个工具,一个事件可调用一次,也可周期性的启动,它由一个特定的线程来管理的,也就是所谓的“事件调度器”。
二、创建语法
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;
schedule:
AT timestamp [+ INTERVAL interval] ...
| EVERY interval
[STARTS timestamp [+ INTERVAL interval] ...]
[ENDS timestamp [+ INTERVAL 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}
名词解释
event_name :创建的event名字(唯一确定的)。
ON SCHEDULE:计划任务。
schedule: 决定event的执行时间和频率(注意时间一定要是将来的时间,过去的时间会出错),有两种形式 AT和EVERY。
[ON COMPLETION [NOT] PRESERVE]: 可选项,默认是ON COMPLETION NOT PRESERVE 即计划任务执行完毕后自动drop该事件;ON COMPLETION PRESERVE则不会drop掉。
[COMMENT 'comment'] :可选项,comment 用来描述event;相当注释,最大长度64个字节。
[ENABLE | DISABLE] :设定event的状态,默认ENABLE:表示系统尝试执行这个事件, DISABLE:关闭该事情,可以用alter修改
DO event_body: 需要执行的sql语句(可以是复合语句)。CREATE EVENT在存储过程中使用时合法的。
三、开启关闭事件调度器
1、查看是否开启
show variables like '%event_scheduler%';
默认是off,未开启
2、开启事件调度器
SET GLOBAL event_scheduler = ON;
SET @@global.event_scheduler = ON;
SET GLOBAL event_scheduler = 1;
SET @@global.event_scheduler = 1;
或者通过配置文件 my.cnf
event_scheduler = 1 #或者ON
查看调度器线程:
show processlist;
3、关闭事件调度器
SET GLOBAL event_scheduler = OFF;
SET @@global.event_scheduler = OFF;
SET GLOBAL event_scheduler = 0;
SET @@global.event_scheduler = 0;
4、查看调度器线程任务
SELECT @@event_scheduler;
SHOW PROCESSLIST;
show events; #查看当前schemal 调度任务
select * from mysql.event;#查看整个数据库 调度任务
四、创建测试表
1、简单省略
2、创建事件1(立即启动事件)
create event event_now
on schedule
at now()
do insert into events_list values('event_now', now());
3、创建事件2(每分钟启动事件)
create event test.event_minute
on schedule
every 1 minute
do insert into events_list values('event_now', now());
4、创建事件3(每秒钟启动事件)
CREATE event event_now
ON SCHEDULE
EVERY 1 SECOND
DO INSERT INTO event_test VALUES(1);
5、创建事件4(每秒钟调用存储过程)
CREATE DEFINER=`root`@`localhost` EVENT `eventUpdateStatus`
ON SCHEDULE EVERY 1 SECOND
STARTS '2017-11-21 00:12:44'
ON COMPLETION PRESERVE
ENABLE
DO call updateStatus()
边栏推荐
- Provincial level of Echart maps, as well as all prefecture-level download and use
- CTO said that the number of rows in a MySQL table should not exceed 2000w, why?
- MySQL数据库存储引擎以及数据库的创建、修改与删除
- 多串口RS485工业网关BL110
- Introduction to c # a week of high-level programming c # - LINQ Day Four
- QueryDet: Cascading Sparse Query Accelerates Small Object Detection at High Resolution
- What is machine learning?Explain machine learning concepts in detail
- Leetcode 669. 修剪二叉搜索树
- 机器学习怎么学?机器学习流程
- leetcode刷题第13天二叉树系列之《98 BST及其验证》
猜你喜欢
WPF DataGrid 使用数据模板(2)
leetcode刷题第13天二叉树系列之《98 BST及其验证》
The last update time of the tables queried by the two nodes of the rac standby database is inconsistent
Build Zabbix Kubernetes cluster monitoring platform
[FPGA] Design Ideas - I2C Protocol
Clang Code Model: Error: The clangbackend executable “X:/clangbackend.exe“ could not be started
多串口RS485工业网关BL110
【FPGA】day22-SPI协议回环
LeetCode刷题第16天之《239滑动窗口最大值》
【FPGA】day21-移动平均滤波器
随机推荐
What are port 80 and port 443?What's the difference?
MySQL数据库存储引擎以及数据库的创建、修改与删除
Rotary array problem: how to realize the array "overall reverse, internal orderly"?"Three-step conversion method" wonderful array
Detailed explanation of VIT source code
使用百度EasyDL实现施工人员安全装备检测
AI + medical: for medical image recognition using neural network analysis
使用百度EasyDL实现森林火灾预警识别
机器学习可以应用在哪些场景?机器学习有什么用?
Kubernetes集群搭建Zabbix监控平台
移动端地图开发选择哪家?
Clang Code Model: Error: The clangbackend executable “X:/clangbackend.exe“ could not be started
Read the article, high-performance and predictable data center network
这些云自动化测试工具值得拥有
拼多多店铺营业执照相关问题
【深度学习】基于卷积神经网络的天气识别训练
The last update time of the tables queried by the two nodes of the rac standby database is inconsistent
Uni - app - access to Chinese characters, pinyin initials (according to the Chinese get pinyin initials)
洛谷P4324 扭动的回文串
En-us is an invalid culture error solution when Docker links sqlserver
App Basic Framework Construction丨Log Management - KLog