MySQL中的事件排程器EVENT
原文連結:
MySQL中的事件排程器,EVENT,也叫定時任務,類似於Unix crontab或Windows任務排程程式。
EVENT由其名稱和所在的schema唯一標識。
EVENT根據計劃執行特定操作。操作由SQL語句組成,語句可以是BEGIN…END語句塊。EVENT可以是一次性的,也可以是重複性的。一次性EVENT只執行一次,週期性EVENT以固定的間隔重複其操作,並且可以為週期性EVENT指定開始日期和時間、結束日期和時間。(預設情況下,定期EVENT在建立後立即開始,並無限期地繼續,直到它被禁用或刪除。)
EVENT由一個特殊的事件排程器執行緒執行,用SHOW PROCESSLIST可以檢視。
root@database-one 13:44: [gftest]> show variables like '%scheduler%'; +-----------------+-------+| Variable_name | Value | +-----------------+-------+| event_scheduler | OFF | +-----------------+-------+1 row in set (0.01 sec) root@database-one 13:46: [gftest]> show processlist; +--------+------+----------------------+-----------+---------+------+----------+------------------+| Id | User | Host | db | Command | Time | State | Info | +--------+------+----------------------+-----------+---------+------+----------+------------------+...... +--------+------+----------------------+-----------+---------+------+----------+------------------+245 rows in set (0.00 sec) root@database-one 13:46: [gftest]> set global event_scheduler=1; Query OK, 0 rows affected (0.00 sec) root@database-one 13:47: [gftest]> show variables like '%scheduler%'; +-----------------+-------+| Variable_name | Value | +-----------------+-------+| event_scheduler | ON | +-----------------+-------+1 row in set (0.01 sec) root@database-one 13:47: [gftest]> show processlist; +--------+-----------------+----------------------+-----------+---------+------+------------------------+------------------+| Id | User | Host | db | Command | Time | State | Info | +--------+-----------------+----------------------+-----------+---------+------+------------------------+------------------+...... | 121430 | event_scheduler | localhost | NULL | Daemon | 33 | Waiting on empty queue | NULL | ...... +--------+-----------------+----------------------+-----------+---------+------+------------------------+------------------+246 rows in set (0.01 sec)
可以看到,預設情況下,MySQL的EVENT沒有開啟,透過設定event_scheduler引數來開啟或者關閉EVENT。開啟後就會多一個event_scheduler,這個就是事件排程器執行緒。
除了開啟和關閉,還可以禁用,要禁用EVENT,請使用以下兩種方法之一:
- 啟動MySQL時用命令列引數
--event-scheduler=DISABLED
- 在MySQL配置檔案中配置引數
event_scheduler=DISABLED
MySQL 5.7中建立EVENT的完整語法如下:
CREATE [DEFINER = user] EVENT [IF NOT EXISTS] event_name ON SCHEDULE schedule [ON COMPLETION [NOT] PRESERVE] [ENABLE | DISABLE | DISABLE ON SLAVE] [COMMENT 'string'] 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}
詳細說明可以參考官網 https://dev.mysql.com/doc/refman/5.7/en/create-event.html
我們透過一個例項來驗證下。
1)建立一張表。
root@database-one 13:47: [gftest]> create table testevent(id int auto_increment primary key,create_time datetime); Query OK, 0 rows affected (0.01 sec) root@database-one 13:50: [gftest]> select * from testevent; Empty set (0.00 sec)
2)建立一個EVENT,每3秒往表中插一條記錄。
root@database-one 13:50: [gftest]> create event insert_date_testevent on schedule every 3 second do -> insert into testevent(create_time) values(now()); Query OK, 0 rows affected (0.01 sec) root@database-one 13:53: [gftest]> show events \G *************************** 1. row *************************** Db: gftest Name: insert_date_testevent Definer: root@% Time zone: +08:00 Type: RECURRING Execute at: NULL Interval value: 3 Interval field: SECOND Starts: 2020-03-26 13:53:10 Ends: NULL Status: ENABLED Originator: 1303306character_set_client: utf8 collation_connection: utf8_general_ci Database Collation: utf8_general_ci1 row in set (0.00 sec)
3)過一會,去表中查詢資料。
root@database-one 13:53: [gftest]> select * from testevent; +----+---------------------+| id | create_time | +----+---------------------+| 1 | 2020-03-26 13:53:10 | | 2 | 2020-03-26 13:53:13 | | 3 | 2020-03-26 13:53:16 | | 4 | 2020-03-26 13:53:19 | | 5 | 2020-03-26 13:53:22 | | 6 | 2020-03-26 13:53:25 | | 7 | 2020-03-26 13:53:28 | | 8 | 2020-03-26 13:53:31 | | 9 | 2020-03-26 13:53:34 | | 10 | 2020-03-26 13:53:37 | | 11 | 2020-03-26 13:53:40 | | 12 | 2020-03-26 13:53:43 | | 13 | 2020-03-26 13:53:46 | | 14 | 2020-03-26 13:53:49 | | 15 | 2020-03-26 13:53:52 | | 16 | 2020-03-26 13:53:55 | +----+---------------------+16 rows in set (0.00 sec)
從表裡資料可以看到,建立的插數定時任務已經在正常執行了。
EVENT的詳細資訊除了用show event命令,還可以從mysql.event或information_schema.events中查詢,也可以用show create event命令檢視。
root@database-one 00:09: [gftest]> select * from mysql.event \G *************************** 1. row *************************** db: gftest name: insert_date_testevent body: insert into testevent(create_time) values(now()) definer: root@% execute_at: NULL interval_value: 3 interval_field: SECOND created: 2020-03-26 13:53:10 modified: 2020-03-26 13:53:10 last_executed: 2020-03-26 16:09:37 starts: 2020-03-26 05:53:10 ends: NULL status: ENABLED on_completion: DROP sql_mode: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION comment: originator: 1303306 time_zone: +08:00character_set_client: utf8 collation_connection: utf8_general_ci db_collation: utf8_general_ci body_utf8: insert into testevent(create_time) values(now())1 row in set (0.00 sec) root@database-one 00:09: [gftest]> select * from information_schema.events \G *************************** 1. row *************************** EVENT_CATALOG: def EVENT_SCHEMA: gftest EVENT_NAME: insert_date_testevent DEFINER: root@% TIME_ZONE: +08:00 EVENT_BODY: SQL EVENT_DEFINITION: insert into testevent(create_time) values(now()) EVENT_TYPE: RECURRING EXECUTE_AT: NULL INTERVAL_VALUE: 3 INTERVAL_FIELD: SECOND SQL_MODE: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION STARTS: 2020-03-26 13:53:10 ENDS: NULL STATUS: ENABLED ON_COMPLETION: NOT PRESERVE CREATED: 2020-03-26 13:53:10 LAST_ALTERED: 2020-03-26 13:53:10 LAST_EXECUTED: 2020-03-27 00:10:22 EVENT_COMMENT: ORIGINATOR: 1303306CHARACTER_SET_CLIENT: utf8 COLLATION_CONNECTION: utf8_general_ci DATABASE_COLLATION: utf8_general_ci1 row in set (0.02 sec) root@database-one 00:10: [gftest]> show create event insert_date_testevent \G *************************** 1. row *************************** Event: insert_date_testevent sql_mode: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION time_zone: +08:00 Create Event: CREATE DEFINER=`root`@`%` EVENT `insert_date_testevent` ON SCHEDULE EVERY 3 SECOND STARTS '2020-03-26 13:53:10' ON COMPLETION NOT PRESERVE ENABLE DO insert into testevent(create_time) values(now()) character_set_client: utf8 collation_connection: utf8_general_ci Database Collation: utf8_general_ci1 row in set (0.00 sec)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31556440/viewspace-2683411/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 詳解 MySQL 用事件排程器 Event Scheduler 建立定時任務MySql事件
- 詳解MySQL用事件排程器Event Scheduler建立定時任務MySql事件
- mysql 事件 eventMySql事件
- MySQL入門--EVENT(事件)MySql事件
- 如何在 JavaScript 中實現 Event Bus(事件匯流排)JavaScript事件
- JS中event事件JS事件
- MySQL的事件溯源Event Sourcing表結構MySql事件
- Flink排程之排程器、排程策略、排程模式模式
- js 在瀏覽器中的event loop事件佇列JS瀏覽器OOP事件佇列
- node中的事件環(Event Loop)事件OOP
- js中事件物件eventJS事件物件
- Laravel 中的 Event 和事件的概念Laravel事件
- Yarn的排程器Yarn
- SAP CRM和Cloud for Customer中的Event handler(事件處理器)Cloud事件
- 理解瀏覽器和nodeJs中的事件迴圈(Event Loop)瀏覽器NodeJS事件OOP
- 排程器簡介,以及Linux的排程策略Linux
- Spring中的事件講解(Application Event)Spring事件APP
- nodejs中的事件迴圈 - Event LoopNodeJS事件OOP
- Pod的排程是由排程器(kube-scheduler)
- Go排程器系列(2)巨集觀看排程器Go
- Go Runtime 的排程器Go
- Kubernetes 排程器
- MySQL 配置InnoDB清理排程MySql
- 瀏覽器事件迴圈Event Loop瀏覽器事件OOP
- 理解瀏覽器和node.js中的Event loop事件迴圈瀏覽器Node.jsOOP事件
- Node.js Event Loop與瀏覽器 Event Loop(事件環)Node.jsOOP瀏覽器事件
- [譯] React 中的排程React
- Go runtime 排程器精講(五):排程策略Go
- Go語言排程器之主動排程(20)Go
- Go排程器系列(3)圖解排程原理Go圖解
- RxJava排程器的選擇RxJava
- Go runtime 排程器精講(二):排程器初始化Go
- Dynamics 365中的事件框架與事件執行管道(Event execution pipeline)事件框架
- Flutter中的事件匯流排(EventBus)Flutter事件
- Go語言排程器之排程main goroutine(14)GoAI
- Spark中資源排程和任務排程Spark
- (14)mysql 中的事件MySql事件
- freeswitch的event事件處理事件