一 event 介绍
事件调度器是定时触发执行的,在这个角度上也可以称作是"临时的触发器"。触发器只是针对某个表产生的事件执行一些语句,而事件调度器则是在某一个(间隔)时间执行一些语句。事件是由一个特定的线程来管理的,也就是所谓的"事件调度器"。启用事件调度器后,拥有 SUPER 权限的账户执行 SHOW PROCESSLIST 就可以看到这个线程了。通过设定全局变量event_scheduler 的值即可动态的控制事件调度器是否启用。
在使用这个功能之前必须确保event_scheduler已开启,可执行
SET GLOBAL event_scheduler = 1;
或
SET GLOBAL event_scheduler = on;
设置成功之后,执行show proceslist;
root@127.0.0.1 : (none) 11:56:09> show variables like '%event_scheduler%';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| event_scheduler | OFF |
+-----------------+-------+
1 row in set (0.01 sec)
root@127.0.0.1 : (none) 11:56:30> SET GLOBAL event_scheduler = 1;
Query OK, 0 rows affected (0.03 sec)
root@127.0.0.1 : (none) 11:56:41> show variables like '%event_scheduler%';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| event_scheduler | ON |
+-----------------+-------+
1 row in set (0.01 sec)
root@127.0.0.1 : (none) 11:59:57> show processlist;
+--------+-----------------+-----------------+------+---------+------+------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+--------+-----------------+-----------------+------+---------+------+------------------------+------------------+
| 165933 | root | localhost:34672 | NULL | Query | 0 | NULL | show processlist |
| 165934 | event_scheduler | localhost | NULL | Daemon | 204 | Waiting on empty queue | NULL |
+--------+-----------------+-----------------+------+---------+------+------------------------+------------------+
2 rows in set (0.00 sec)
进程中会出现一个event_scheduler的用户用于执行调度事件。
二、创建事件
create evnet [if not exists] event_name
on schedule schedule
[on completion [not] preserve]
[enable | disenable]
[commet 'commet']
do sql_statement;
schedule:
AT timestamp [+ interval interval] | every interval [starts timestamp] [ends timestamp]
interval:
quantity {year | quarter | month | day | hour | minute |
week | sencond | year_month | day_hour | day_minute |
day_sencond | hour_minute | hour_sencond | minute_sencond}
1) 创建一个任务每隔两秒向表插入数据
create evnet e_tab_insert
on schedule every 2 sencond
do insert into yang.evnt values (now());
2) 10天后清空evnt表:
create evnet e_10d_truct_evnt
on schedule at current_timestamp + interal 10 day
do truncate table yang.evnt;
3) 2013年4月5日12点整清空evnt表:
create evnet e_evnt_1
on schedule at timestamp '2013-04-05 12:00:00'
do truncate table yang.evnt;
4) 2天后开启每天定时清空evnt表,一年后停止执行:
create evnet e_evnt_2
on schedule every 1 day
starts current_timestamp + interval 2 day
ends current_timestamp + interval 1 year
do truncate table yang.evnt
[on completion [not] preserve]可以设置这个事件是执行一次还是持久执行,默认为not preserve。
5) 每天定时清空evnt表(只执行一次,任务完成后就终止该事件):
create evnet e_evnt_3
on schedule every 1 day
on completion not preserve
do truncate table yang.evnt
[enable | disenable]可是设置该事件创建后状态是否开启或关闭,默认为enable。
[commet 'commet']可以给该事件加上注释。
三、修改事件
alter event event_name
[on schedule schedule]
[rename to new_event_name]
[on completion [not] preserve]
[commet 'commet']
[enable | disenable]
[do sql_statement]
1) 关闭事件
alter event e_evnt disenable;
2) 开启事件
alter event e_evnt enable;
3) 将每天清空evnt表改为5天清空一次:
alter event e_evnt
on schedule every 5 day;
四、删除事件
DROP event [IF EXISTS] event_name
五 注意事项
对于主从架构的数据库要注意将从库的任务调度关闭,以防止在主库执行了之后,从库又重复在备库进行调度evnet。