1. 程式人生 > >Mysql 查看定時器 打開定時器 設置定時器時間

Mysql 查看定時器 打開定時器 設置定時器時間

rop gin nth 時間 狀態 fault sele minute value

 1 1.查看是否開啟evevt與開啟evevt。
 2 
 3 1.1、MySQL evevt功能默認是關閉的,可以使用下面的語句來看evevt的狀態,如果是OFF或者0,表示是關閉的。
 4               show VARIABLES LIKE ‘%sche%‘;
 5 1.2、開啟evevt功能
 6             SET GLOBAL event_scheduler = 1;
 7 2.創建定時器的過程
 8 2.1、創建測試表test
 9 drop table if exists test;
10 create table test
11 (
12 id int
(11) not null auto_increment primary key, 13 time datetime not null 14 ) engine=innodb default charset=utf8; 15 2.2、創建evevt要調用的存儲過程test_proce 16 delimiter // 17 drop procedure if exists test_proce// 18 create procedure test_proce() 19 begin 20 insert into test(time) values(now()); 21 end// 22 delimiter ; 23
2.3、開啟evevt(要使定時起作用,MySQL的常量GLOBAL event_scheduler必須為on或者是1) 24 執行show variables like ‘event_scheduler‘;查看evevt是否開啟; 25 若沒開啟執行set global event_scheduler=‘on‘; 26 2.4、創建事件test_event(其作用:每隔一秒自動調用test_proce()存儲過程) 27 drop event if exists test_event; 28 create event test_event 29 on schedule every 1 second
30 on completion preserve disable 31 do call test_proce(); 32 2.5、開啟事件test_event 33 alter event test_event on completion preserve enable; 34 2.6、關閉事件test_event 35 alter event test_event on completion preserve disable; 36 2.7、查看表test 37 select * from test; 38 39 3.查看自己創建的event 40 如果要查看更加詳細的信息,你需要root用戶的授權,如果是你自己的數據庫你可以用下面語句查看 41 select * from mysql.event; 42 下面的我的evevt的查看結果 43 mysql創建定時器(event),查看定時器,打開定時器,設置定時器時間 44 45 4.event的時間設置 46 設置event很簡單,但是麻煩的是如何設置執行的時間,網上找了一些,自己總結了一下。 47 先看語句,如下面這個 48 CREATE EVENT test_event ON SCHEDULE EVERY 1 DAY STARTS ‘2012-09-24 00:00:00‘ 49 ON COMPLETION PRESERVE ENABLE DO CALL test_procedure(); 50 EVERY 後面的是時間間隔,可以選 1 second,3 minute,5 hour,9 day,1 month,1 quarter(季度),1 year 51 從2013年1月13號0點開始,每天運行一次 52 ON SCHEDULE EVERY 1 DAY STARTS ‘2013-01-13 00:00:00‘ 53 從現在開始每隔九天定時執行 54 ON SCHEDULE EVERY 9 DAY STARTS NOW() ; 55 每個月的一號淩晨1 點執行 56 on schedule every 1 month starts date_add(date_add(date_sub(curdate(),interval day(curdate())-1 day),interval 1 month),interval 1 hour); 57 每個季度一號的淩晨1點執行 58 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 1 hour); 59 每年1月1號淩晨1點執行 60 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 1 hour);

Mysql 查看定時器 打開定時器 設置定時器時間