MySQL事件是一種自動化執(zhí)行計(jì)劃任務(wù)的功能。通過定義事件,可以在特定的時(shí)間內(nèi)啟動一個任務(wù),以執(zhí)行某些指定的操作。但有時(shí),我們可能需要檢查事件是否已經(jīng)啟動,以確保任務(wù)能夠按照預(yù)期執(zhí)行。
mysql>SHOW EVENTS;
+-----------------------+----------------+---------------------+------+------------+------------+----------------+----------------+---------------------+------+----------+------------+----------------------+----------------------+--------------------+
| Db | Name | Definer | Time zone | Type | Execute at | Interval value | Interval field | Starts | Ends | Status | Originator | character_set_client | collation_connection | Database Collation |
+-----------------------+----------------+---------------------+------+------------+------------+----------------+----------------+---------------------+------+----------+------------+----------------------+----------------------+--------------------+
| mydb | myevent | root@localhost :55752 | SYSTEM | RECURRING | NULL | 10 | SECOND | 2021-06-09 07:21:20 | NULL | STARTED | 1 | utf8mb4 | utf8mb4_0900_ai_ci | utf8mb4_0900_ai_ci |
+-----------------------+----------------+---------------------+------+------------+------------+----------------+----------------+---------------------+------+----------+------------+----------------------+----------------------+--------------------+
1 row in set (0.00 sec)
在上述查詢中,我們可以看到該事件以STARTED
狀態(tài)運(yùn)行。如果事件不存在或未啟動,則不會顯示在查詢結(jié)果中。
除此之外,我們也可以使用以下語句來檢查事件是否啟動:
mysql>SELECT @@event_scheduler;
+---------------------+
| @@event_scheduler |
+---------------------+
| ON |
+---------------------+
1 row in set (0.00 sec)
如果輸出結(jié)果為ON
,則代表事件調(diào)度器已經(jīng)啟用,事件將會按照定義的計(jì)劃執(zhí)行。如果輸出結(jié)果為OFF
,則需要使用以下語句來啟用事件調(diào)度器:
mysql>SET GLOBAL event_scheduler = ON;
通過以上查詢和設(shè)置,我們可以確保MySQL事件已經(jīng)啟動,并按照預(yù)期執(zhí)行。