Home > Database > Mysql Tutorial > mysql如何定时执行_MySQL

mysql如何定时执行_MySQL

WBOY
Release: 2016-06-01 13:43:40
Original
1280 people have browsed it

bitsCN.com 5.1版本以后,MYSQL支持定时执行(event)的功能,就跟linux的crontab差不多。
 
先查看一下你的MSYQL版本,查询版本信息的SQL语句:
 
select VERSION()
确定版本支持之后,再查看一下event是否开启:
 
show variables like ’%sche%’;
如果未开启,那如何开启呢?
 
show VARIABLES LIKE ’%sche%’;
set global event_scheduler =1;
show VARIABLES LIKE ’%sche%’;
一般定时执行有2种:
 
1、从具体一个时间点开始,每隔一段时间执行一次;
 
从现在开始,每30秒执行一次
 
create event if not exists e_test
on schedule every 30 second starts now()
on completion preserve
do
INSERT into sdb_yoyi (yoyiscid,orderid) VALUES(2,3)
2、从具体一个时间点开始,在当日的某个时间点进行执行;
 
每个月的一号凌晨1点执行
 
create event if not exists e_test2
 
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 completion preserve
 
do
 
INSERT into sdb_yoyi (yoyiscid,orderid) VALUES(2,3)
 
 
 
也可以对事件任务进行关闭:
 
alter event e_test ON
 
COMPLETION PRESERVE DISABLE;
 
对已经关闭的事件任务进行开启:
 
alter event e_test ON
 
COMPLETION PRESERVE ENABLE; bitsCN.com

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template