作者:皇家突然回家_390 | 来源:互联网 | 2014-06-09 00:50
MySQL定时器开启、调用创建测试表www.2cto.com1CREATETABLEt2(3vVARCHAR(100)NOTNULL4)ENGINEINNODBDEFAULTCHARSETutf8;创建定时器调用的存储过程1DELIMITER$$2DROPPROCEDUR...S
MySQL定时器开启、调用
创建测试表 www.2cto.com
1
CREATE TABLE t
2
(
3
v VARCHAR(100) NOT NULL
4
) ENGINE INNODB DEFAULT CHARSET=utf8;
创建定时器 调用的存储过程
1
DELIMITER $$
2
DROP PROCEDURE IF EXISTS e_test $$
3
CREATE PROCEDURE e_test()
4
BEGIN
5
INSERT INTO t VALUES('1');
6
END $$
7
DELIMITER ;
要使定时起作用 MySQL的常量GLOBAL event_scheduler必须为on或者是1
-- 查看是否开启定时器
1
SHOW VARIABLES LIKE '%sche%';
-- 开启定时器 0:off 1:on
1
SET GLOBAL event_scheduler = 1;
-- 创建事件
--每隔一秒自动调用e_test()存储过程
1
CREATE EVENT IF NOT EXISTS event_test
2
ON SCHEDULE EVERY 1 SECOND
3
ON COMPLETION PRESERVE
4
DO CALL e_test();
-- 开启事件
1
ALTER EVENT event_test ON
2
COMPLETION PRESERVE ENABLE;
-- 关闭事件
1
ALTER EVENT event_test ON
2
COMPLETION PRESERVE DISABLE;
从现在开始每隔九天定时执行
1
CREATE EVENT EVENT1
2
ON SCHEDULE EVERY 9 DAY STARTS NOW()
3
ON COMPLETION PRESERVE ENABLE
4
DO
5
BEGIN
6
CALL TOTAL();
7
END
每个月的一号凌晨1 点执行
1
CREATE EVENT EVENT2
2
ON SCHEDULE EVERY 1 MONTH STARTS DATE_ADD(DATE_ADD(DATE_SUB(CURDATE(),INTERVAL DAY(CURDATE())-1 DAY), INTERVAL 1 MONTH),INTERVAL 1 HOUR)
3
ON COMPLETION PRESERVE ENABLE
4
DO
5
BEGIN
6
CALL STAT();
7
END
每个季度一号的凌晨2点执行
1
CREATE EVENT TOTAL_SEASON_EVENT
2
ON SCHEDULE EVERY 1 QUARTER STARTS DATE_ADD(DATE_ADD(DATE( CONCAT(YEAR(CURDATE()),'-',ELT(QUARTER(CURDATE()),1,4,7,10),'-',1)),INTERVAL 1 QUARTER),INTERVAL 2 HOUR)
3
ON COMPLETION PRESERVE ENABLE
4
DO
5
BEGIN
6
CALL SEASON_STAT();
7
END
每年1月1号凌晨四点执行
1
CREATE EVENT TOTAL_YEAR_EVENT
2
ON SCHEDULE EVERY 1 YEAR STARTS DATE_ADD(DATE(CONCAT(YEAR(CURDATE()) + 1,'-',1,'-',1)),INTERVAL 4 HOUR)
3
ON COMPLETION PRESERVE ENABLE
4
DO
5
BEGIN
6
CALL YEAR_STAT();
7
END
MySQL的event在进行按月,季,年进行自动调用存储过程时,为了测试可以把
系统改为年的最后一天,如2010-12-31 23:59:55;