event由特殊的event_scheduler线程来执行,
通过以下方式,
显示运行的线程信息
SELECT * FROM INFORMATION_SCHEMA.PROCESSLIST;
或
SHOW FULL PROCESSLIST;/SHOW FULL PROCESSLIST \G
mysql> SHOW FULL PROCESSLIST \G
*************************** 1. row ***************************
Id: 1
User: root
Host: localhost:50756
db: test
Command: Sleep
Time: 232
State:
Info: NULL
*************************** 2. row ***************************
Id: 3
User: root
Host: localhost:51561
db: test
Command: Query
Time: 0
State: NULL
Info: SHOW FULL PROCESSLIST
*************************** 3. row ***************************
Id: 4
User: event_scheduler
Host: localhost
db: NULL
Command: Daemon
Time: 62
State: Waiting for next activation
Info: NULL
3 rows in set (0.00 sec)
开启 Event Scheduler,以下4种方式等效
SET GLOBAL event_scheduler = ON;
SET @@global.event_scheduler = ON;
SET GLOBAL event_scheduler = 1;
SET @@global.event_scheduler = 1;
关闭 Event Scheduler,以下4种方式等效
SET GLOBAL event_scheduler = OFF;
SET @@global.event_scheduler = OFF;
SET GLOBAL event_scheduler = 0;
SET @@global.event_scheduler = 0;
CREATE event语法
CREATE
[DEFINER = { user | CURRENT_USER }]
EVENT
[IF NOT EXISTS]
event_name
ON SCHEDULE schedule
[ON COMPLETION [NOT] PRESERVE]
[ENABLE | DISABLE | DISABLE ON SLAVE]
[COMMENT 'comment']
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}
具体含义可以参见mysql 5.5 Refer
查看event ddl
SHOW CREATE EVENT test_event;/SHOW CREATE EVENT test_event \G
查询schema下的所有event
SHOW EVENTS FROM test;/SHOW EVENTS FROM test \G
或
SELECT event_schema,event_name,event_definition,interval_value,interval_field,last_executed,status
FROM `INFORMATION_SCHEMA`.`EVENTS`
WHERE `EVENT_SCHEMA` = 'test'
ORDER BY EVENT_NAME;
--设置实例
mysql> show variables like '%sched%';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| event_scheduler | OFF |
+-----------------+-------+
1 row in set (0.00 sec)
mysql> select @@global.event_scheduler;
+--------------------------+
| @@global.event_scheduler |
+--------------------------+
| OFF |
+--------------------------+
1 row in set (0.00 sec)
mysql> SET GLOBAL event_scheduler = ON;
Query OK, 0 rows affected (0.02 sec)
mysql> show variables like '%sched%';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| event_scheduler | ON |
+-----------------+-------+
1 row in set (0.00 sec)
mysql> select @@global.event_scheduler;
+--------------------------+
| @@global.event_scheduler |
+--------------------------+
| ON |
+--------------------------+
1 row in set (0.00 sec)
--创建过程并生成event.
mysql> DELIMITER $$
mysql>
mysql> CREATE PROCEDURE `test`.`test_proc_event`(IN p_date DATETIME)
-> BEGIN
-> START TRANSACTION;
-> INSERT INTO t_event VALUES(p_date);
-> COMMIT;
-> END$$
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> DELIMITER ;
mysql> delimiter //
mysql> create EVENT test_event
-> ON SCHEDULE
-> EVERY 2 MINUTE
-> do
-> begin
-> call test_proc_event(now());
-> end;
-> //
Query OK, 0 rows affected (0.00 sec)
--event生效
mysql> select * from t_event;
+---------------------+
| pdate |
+---------------------+
| 2012-09-07 22:28:11 |
| 2012-09-07 22:30:11 |
| 2012-09-07 22:32:11 |
| 2012-09-07 22:34:11 |
+---------------------+
4 rows in set (0.00 sec)