MySQL 创建定时任务 详解
自 MySQL5.1.6起,增加了一个非常有特色的功能–事件调度器(Event Scheduler),可以用做定时执行某些特定任务,来取代原先只能由操作系统的计划任务来执行的工作。事件调度器有时也可称为临时触发器(temporal triggers),因为事件调度器是基于特定时间周期触发来执行某些任务,而触发器(Triggers)是基于某个表所产生的事件触发的,区别也就在这里
在使用这个功能之前必须确保 event_scheduler 已开启,可执行 :
mysq> SET GLOBAL event_scheduler = 1;
# 或
mysql> SET GLOBAL event_scheduler = ON;
也可以在配置文件中添加设置 : event_scheduler=1
也可以直接在启动命令加上 : --event_scheduler=1
查看当前是否已开启事件调度器 :
mysql> SHOW VARIABLES LIKE 'event_scheduler';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| event_scheduler | ON |
+-----------------+-------+
1 row in set (0.01 sec)
# 或
mysql> SELECT @@event_scheduler;
+-------------------+
| @@event_scheduler |
+-------------------+
| ON |
+-------------------+
1 row in set (0.00 sec)
# 或
mysql> SHOW PROCESSLIST;
+----+-----------------+-----------+------+---------+------+------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-----------------+-----------+------+---------+------+------------------------+------------------+
| 6 | root | localhost | NULL | Query | 0 | starting | SHOW PROCESSLIST |
| 7 | event_scheduler | localhost | NULL | Daemon | 81 | Waiting on empty queue | NULL |
+----+-----------------+-----------+------+---------+------+------------------------+------------------+
2 rows in set (0.00 sec)
创建事件(CREATE EVENT)
CREATE EVENT [IFNOT EXISTS] event_name
ON SCHEDULE schedule
[ON COMPLETION [NOT] PRESERVE]
[ENABLE | DISABLE]
[COMMENT 'comment']
DO sql_statement;
[ON COMPLETION [NOT] PRESERVE] 可以设置这个事件是执行一次还是持久执行,默认为 NOT PRESERVE
[ENABLE | DISABLE] 可是设置该事件创建后状态是否开启或关闭,默认为ENABLE
[COMMENT 'comment'] 可以给该事件加上注释
# 创建表
mysql> USE Test;
mysql> CREATE TABLE aaa(timeline TIMESTAMP);
# 每秒插入一条记录到数据表
mysql> CREATE EVENT e_test_insert ON SCHEDULE EVERY 1 SECOND
-> DO INSERT INTO test.aaa VALUES(CURRENT_TIMESTAMP);
mysql> SELECT * FROM aaa;
# 5天后清空 test表
mysql> CREATE EVENT e_test
-> ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 5 DAY
-> DO TRUNCATE TABLE test.aaa;
# 2018年8月8日8点整清空 test表
mysql> CREATE EVENT e_test
-> ON SCHEDULE AT TIMESTAMP '2018-08-08 8:00:00'
-> DO TRUNCATE TABLE test.aaa;
# 每天定时清空 test表
mysql> CREATE EVENT e_test
-> ON SCHEDULE EVERY 1 DAY
-> DO TRUNCATE TABLE test.aaa;
# 5天后开启每天定时清空test表
mysql> CREATE EVENT e_test
-> ON SCHEDULE EVERY 1 DAY
-> STARTS CURRENT_TIMESTAMP + INTERVAL 5 DAY
-> DO TRUNCATE TABLE test.aaa;
# 每天定时清空 test表,5天后停止执行
mysql> CREATE EVENT e_test
-> ON SCHEDULE EVERY 1 DAY
-> ENDS CURRENT_TIMESTAMP + INTERVAL 5 DAY
-> DO TRUNCATE TABLE test.aaa;
# 5天后开启每天定时清空test表,一个月后停止执行
mysql> CREATE EVENT e_test
-> ON SCHEDULE EVERY 1 DAY
-> STARTS CURRENT_TIMESTAMP + INTERVAL 5 DAY
-> ENDS CURRENT_TIMESTAMP + INTERVAL 1 MONTH
-> DO TRUNCATE TABLE test.aaa;
# 每天定时清空test表(只执行一次,任务完成后就终止该事件)
mysql> CREATE EVENT e_test
-> ON SCHEDULE EVERY 1 DAY
-> ON COMPLETION NOT PRESERVE
-> DO TRUNCA TETABLE test.aaa;
修改事件(ALTER EVENT)
ALTER EVENT event_name
[ON SCHEDULE schedule]
[RENAME TO new_event_name]
[ON COMPLETION [NOT] PRESERVE]
[COMMENT 'comment']
[ENABLE | DISABLE]
[DO sql_statement]
# 临时关闭事件
mysql> ALTER EVENT e_test DISABLE;
# 开启事件
mysql> ALTER EVENT e_test ENABLE;
# 将每天清空test表改为5天清空一次
mysql> ALTER EVENT e_test ON SCHEDULE EVERY 5 DAY;
注 : 如果将 event 执行 ALTER EVENT event_name DISABLE. 那么当重新启动 mysql服务器后,该 event 将被删除
删除事件(DROP EVENT)
DROP EVENT [IF EXISTS] event_name
# 删除创建的 e_test事件
mysql> DROP EVENT e_test; # 如果该事件不存在将会产生 ERROR 1513 (HY000): Unknown event 错误,因此最好加上IF EXISTS
mysql> DROP EVENT IF EXISTS e_test;
每秒钟调用一次存储过程,用于判断 SLAVE 是否正常运行,如果发现 SLAVE 关闭了,忽略 0 次错误,然后重新启动 SLAVE
mysql> DELIMITER //
-> CREATE PROCEDURE Slave_Monitor()
-> BEGIN
-> SELECT VARIABLE_VALUE INTO @SLAVE_STATUS FROM information_schema.GLOBAL_STATUS WHEREVARIABLE_NAME='SLAVE_RUNNING';
-> IF ('ON'!= @SLAVE_STATUS) THEN
-> SET GLOBAL SQL_SLAVE_SKIP_COUNTER=0;
-> SLAVE START;
-> ENDIF;
-> END; //
-> DELIMITER;
# 由于存储过程中无法调用类似 SHOW SLAVE STATUS 这样的语句,因此无法得到确切的复制错误信息和错误代码,不能进一步的处理 SLAVE 停止的各种情况
mysql> CREATE EVENT IF NOT EXISTS Slave_Monitor
-> ON SCHEDULE EVERY 5 SECOND
-> ON COMPLETION PRESERVE
-> DO CALL Slave_Monitor();
————————————————
版权声明:本文为CSDN博主「小柴的生活观」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/chenshun123/article/details/79677193