MySQL准時履行劇本(籌劃義務)敕令實例。本站提示廣大學習愛好者:(MySQL准時履行劇本(籌劃義務)敕令實例)文章只能為提供參考,不一定能成為您想要的結果。以下是MySQL准時履行劇本(籌劃義務)敕令實例正文
檢查event能否開啟
show variables like '%sche%';
將事宜籌劃開啟
set global event_scheduler =1;
創立存儲進程test
CREATE PROCEDURE test ()
BEGIN
update examinfo SET endtime = now() WHERE id = 14;
END;
創立event e_test
create event if not exists e_test
on schedule every 30 second
on completion preserve
do call test();
每隔30秒將履行存儲進程test,將以後時光更新到examinfo表中id=14的記載的endtime字段中去.
封閉事宜義務
alter event e_test ON
COMPLETION PRESERVE DISABLE;
開戶事宜義務
alter event e_test ON
COMPLETION PRESERVE ENABLE;
以上測試均勝利,測試情況為mysql 5.4.2-beta-community mysql community server(GPL)
以上的相干內容就是對MySQL准時履行的引見,望你能有所收成。
mysql 籌劃義務重啟後消逝
我們只需修正一設置裝備擺設便可
event_scheduler在mysql的config中設置為OFF有關。去mysql中將設置裝備擺設改成ON則就弄定了。
更具體的年夜家可以往下看
MySQL5.1.x版本中引入了一項新特征EVENT,望文生義就是事宜、准時義務機制,在指定的時光單位內履行特定的義務,是以往後一些對數據准時性操作不再依附內部法式,而直接應用數據庫自己供給的功效。
要檢查以後能否已開啟事宜調劑器,可履行以下SQL:
SHOW VARIABLES LIKE 'event_scheduler';
或
SELECT @@event_scheduler;
或
SHOW PROCESSLIST;
若顯示:
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| event_scheduler | OFF |
+-----------------+-------+
則可履行
SET GLOBAL event_scheduler = 1;
或
SET GLOBAL event_scheduler = ON;
來開啟,也能夠直接在啟動敕令加上“–event_scheduler=1”,例如:
mysqld ... --event_scheduler=1
my.ini or my.cnf 中的
[mysqld]
添加 event_scheduler=ON
創立事宜(CREATE EVENT)
先來看一下它的語法:
CREATE EVENT [IF NOT EXISTS] event_name
ON SCHEDULE schedule
[ON COMPLETION [NOT] PRESERVE]
[ENABLE | DISABLE]
[COMMENT 'comment']
DO sql_statement;
schedule:
AT TIMESTAMP [+ INTERVAL INTERVAL]
| EVERY INTERVAL [STARTS TIMESTAMP] [ENDS TIMESTAMP]
INTERVAL:
quantity {YEAR | QUARTER | MONTH | DAY | HOUR | MINUTE |
WEEK | SECOND | YEAR_MONTH | DAY_HOUR | DAY_MINUTE |
DAY_SECOND | HOUR_MINUTE | HOUR_SECOND | MINUTE_SECOND}
1)起首來看一個簡略的例子來演示每秒拔出一筆記錄到數據表
USE test;
CREATE TABLE aaa (timeline TIMESTAMP);
CREATE EVENT e_test_insert
ON SCHEDULE EVERY 1 SECOND
DO INSERT INTO test.aaa VALUES (CURRENT_TIMESTAMP);
期待3秒鐘後,再履行查詢勝利。
2) 5天後清空test表:
CREATE EVENT e_test
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 5 DAY
DO TRUNCATE TABLE test.aaa;
3) 2007年7月20日12點整清空test表:
CREATE EVENT e_test
ON SCHEDULE AT TIMESTAMP '2007-07-20 12:00:00'
DO TRUNCATE TABLE test.aaa;
4) 天天准時清空test表:
CREATE EVENT e_test
ON SCHEDULE EVERY 1 DAY
DO TRUNCATE TABLE test.aaa;
5) 5天後開啟天天准時清空test表:
CREATE EVENT e_test
ON SCHEDULE EVERY 1 DAY
STARTS CURRENT_TIMESTAMP + INTERVAL 5 DAY
DO TRUNCATE TABLE test.aaa;
6) 天天准時清空test表,5天後停滯履行:
CREATE EVENT e_test
ON SCHEDULE EVERY 1 DAY
ENDS CURRENT_TIMESTAMP + INTERVAL 5 DAY
DO TRUNCATE TABLE test.aaa;
7) 5天後開啟天天准時清空test表,一個月後停滯履行:
CREATE EVENT e_test
ON SCHEDULE EVERY 1 DAY
STARTS CURRENT_TIMESTAMP + INTERVAL 5 DAY
ENDS CURRENT_TIMESTAMP + INTERVAL 1 MONTH
DO TRUNCATE TABLE test.aaa;
[ON COMPLETION [NOT] PRESERVE]可以設置這個事宜是履行一次照樣耐久履行,默許為NOT PRESERVE。
8) 天天准時清空test表(只履行一次,義務完成後就終止該事宜):
CREATE EVENT e_test
ON SCHEDULE EVERY 1 DAY
ON COMPLETION NOT PRESERVE
DO TRUNCATE TABLE test.aaa;
[ENABLE | DISABLE]可是設置該事宜創立後狀況能否開啟或封閉,默許為ENABLE。
[COMMENT ‘comment']可以給該事宜加上正文。
修正事宜(ALTER EVENT)
ALTER EVENT event_name
[ON SCHEDULE schedule]
[RENAME TO new_event_name]
[ON COMPLETION [NOT] PRESERVE]
[COMMENT 'comment']
[ENABLE | DISABLE]
[DO sql_statement]
1) 暫時封閉事宜
ALTER EVENT e_test DISABLE;
2) 開啟事宜
ALTER EVENT e_test ENABLE;
3) 將天天清空test表改成5天清空一次:
ALTER EVENT e_test
ON SCHEDULE EVERY 5 DAY;
刪除事宜(DROP EVENT)
語法很簡略,以下所示:
DROP EVENT [IF EXISTS] event_name
例如刪除後面創立的e_test事宜
DROP EVENT e_test;
固然條件是這個事宜存在,不然會發生ERROR 1513 (HY000): Unknown event毛病,是以最好加上IF EXISTS
DROP EVENT IF EXISTS e_test;