MySQL 5.5建立和檢視資料庫計劃任務(event)

feelpurple發表於2016-05-23
--檢視相關引數
mysql> show variables like '%event%';
+---------------------------------------------------+-------+
| Variable_name                                     | Value |
+---------------------------------------------------+-------+
| event_scheduler                                   | OFF   |
| performance_schema_events_waits_history_long_size | 10000 |
| performance_schema_events_waits_history_size      | 10    |
+---------------------------------------------------+-------+
3 rows in set (0.00 sec)

--開啟相關引數
mysql> set global event_scheduler = 1;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like '%event%';
+---------------------------------------------------+-------+
| Variable_name                                     | Value |
+---------------------------------------------------+-------+
| event_scheduler                                   | ON    |
| performance_schema_events_waits_history_long_size | 10000 |
| performance_schema_events_waits_history_size      | 10    |
+---------------------------------------------------+-------+
3 rows in set (0.00 sec)

--開啟引數後,線上程中會出現event_scheduler相關執行緒
mysql> show processlist\G

*************************** 7. row ***************************
     Id: 13
   User: event_scheduler
   Host: localhost
     db: NULL
Command: Daemon
   Time: 474
  State: Waiting for next activation
   Info: NULL
7 rows in set (0.00 sec)

--建立計劃任務
mysql> CREATE EVENT myevent
    ->     ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 HOUR
    ->     DO
    ->       UPDATE fire.t12 SET sal = sal + 100;
Query OK, 0 rows affected (0.02 sec)

--檢視資料庫裡面的計劃任務
mysql> show events from fire\G
*************************** 1. row ***************************
                  Db: fire
                Name: myevent
             Definer: system@localhost
           Time zone: SYSTEM
                Type: ONE TIME
          Execute at: 2016-05-23 02:48:03
      Interval value: NULL
      Interval field: NULL
              Starts: NULL
                Ends: NULL
              Status: ENABLED
          Originator: 100
character_set_client: gbk
collation_connection: gbk_chinese_ci
  Database Collation: utf8_general_ci
1 row in set (0.00 sec)

--檢視計劃任務的內容
mysql> show create event myevent\G
*************************** 1. row ***************************
               Event: myevent
            sql_mode: STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION
           time_zone: SYSTEM
        Create Event: CREATE DEFINER=`system`@`localhost` EVENT `myevent` ON SCHEDULE AT '2016-05-23 02:48:03' ON COMPLETION NOT PRESERVE ENABLE DO UPDATE fire.t12 SET sal = sal + 100
character_set_client: gbk
collation_connection: gbk_chinese_ci
  Database Collation: utf8_general_ci
1 row in set (0.00 sec)

--禁止計劃任務
mysql> ALTER EVENT myevent DISABLE;

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26506993/viewspace-2105493/,如需轉載,請註明出處,否則將追究法律責任。

相關文章