First, preface
since MySQL5.1.6, has added a very distinctive feature – event scheduler, which can be used to perform certain tasks on a timed basis (for example: Deleting records, summarizing data, etc.). To replace work that was previously only performed by the operating system's scheduled tasks. It is also worth mentioning that the MySQL event Scheduler can perform one task per second, while the operating system's scheduled tasks (such as Cron under Linux or Scheduled tasks under Windows) can only be performed once per minute. For some applications that require a higher level of real-time data (e.g., stocks, odds, scores, etc.), it is very suitable. The
Event Scheduler can sometimes also be called a temporary trigger (temporal triggers) because the event scheduler performs certain tasks based on a specific time period trigger, and the trigger (triggers) is triggered based on events generated by a table, and the difference is here.
Before using this feature, you must ensure that Event_scheduler is turned on, and you can execute
SET GLOBAL event_scheduler = 1;
---or we can add Event_scheduler = 1
or
SET GLOBAL event_scheduler = On in the configuration my.cnf file;
to open, or you can add "--event_scheduler=1" directly to the start command, for example:
mysqld ...--event_scheduler=1
to see if the event Scheduler is currently turned on, perform the following SQL:
SHOW VARIABLES like ' Event_scheduler ';
or
SELECT @ @event_scheduler;
or
SHOW processlist;
Ii. Create event
First look at its syntax:
1 |
CREATE EVENT [IF NOT EXISTS] event_name |
3 |
[ ON COMPLETION [ NOT ] PRESERVE] |
Schedule
at TIMESTAMP [+ INTERVAL INTERVAL]
| Every INTERVAL [starts TIMESTAMP] [ENDS TIMESTAMP]
INTERVAL:
Quantity {Year | QUARTER | MONTH | Day | HOUR | MINUTE |
WEEK | SECOND | Year_month | Day_hour | Day_minute |
Day_second | Hour_minute | Hour_second | Minute_second}
1) First look at a simple example to demonstrate inserting a record into a data table per second
2 |
CREATE TABLE aaa (timeline TIMESTAMP ); |
3 |
CREATE EVENT e_test_insert |
4 |
ON SCHEDULE EVERY 1 SECOND |
5 |
DO INSERT INTO test.aaa VALUES ( CURRENT_TIMESTAMP ); |
Wait 3 seconds, then execute the query to see:
Mysql> SELECT * from AAA;
+---------------------+
| Timeline |
+---------------------+
| 2007-07-18 20:44:26 |
| 2007-07-18 20:44:27 |
| 2007-07-18 20:44:28 |
+---------------------+
2) Empty the test table after 5 days:
2 |
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 5 DAY |
3 |
DO TRUNCATE TABLE test.aaa; |
3) July 20, 2007 12 O'Clock full empty test table:
2 |
ON SCHEDULE AT TIMESTAMP ‘2007-07-20 12:00:00‘ |
3 |
DO TRUNCATE TABLE test.aaa; |
4) Clear the test table periodically every day:
3 |
DO TRUNCATE TABLE test.aaa; |
5) After 5 days, open the test table at regular intervals every day:
3 |
STARTS CURRENT_TIMESTAMP + INTERVAL 5 DAY |
4 |
DO TRUNCATE TABLE test.aaa; |
6) Clear the test table every day and stop execution after 5 days:
3 |
ENDS CURRENT_TIMESTAMP + INTERVAL 5 DAY |
4 |
DO TRUNCATE TABLE test.aaa; |
7) After 5 days, open the test table periodically, and stop execution after one months:
3 |
STARTS CURRENT_TIMESTAMP + INTERVAL 5 DAY |
4 |
ENDS CURRENT_TIMESTAMP + INTERVAL 1 MONTH |
5 |
DO TRUNCATE TABLE test.aaa; |
[on completion [NOT] PRESERVE] can set whether this event is executed once or persisted, default to not PRESERVE.
8) Clear the test table every day (only once and terminate the event when the task is completed):
3 |
ON COMPLETION NOT PRESERVE |
4 |
DO TRUNCATE TABLE test.aaa; |
[ENABLE | DISABLE] But sets whether the state is turned on or off after the event is created, and the default is enable.
[COMMENT ' COMMENT '] can add comments to the event.
III. modifying events (Alter event)
3 |
[RENAME TO new_event_name] |
4 |
[ ON COMPLETION [ NOT ] PRESERVE] |
1) Temporary Shutdown event
ALTER EVENT e_test DISABLE;
2) Turn on event
ALTER EVENT e_test ENABLE;
3) Change the daily empty test table to 5 days to clear:
ALTER EVENT E_test
On SCHEDULE every 5 day;
Iv. Delete events (drop event)
The syntax is simple, as follows:
DROP EVENT [IF EXISTS] Event_Name
For example, delete the previously created E_test event
DROP EVENT e_test;
Of course the premise is that this event exists, otherwise it will generate error 1513 (HY000): Unknown event error, so it is best to add if EXISTS
DROP EVENT IF EXISTS e_test;
Note: If you have performed the event, alter event event_name disable. Then when you restart the MySQL service
The event will be deleted (Beta version: 5.1.30)
Application case
In this case, the event scheduler is used to invoke a stored procedure every second to determine if the SLAVE is functioning properly, if SLAVE is turned off, 0 errors are ignored, and SLAVE is restarted.
* Create a stored procedure first
02 |
create procedure `Slave_Monitor`() |
04 |
SELECT VARIABLE_VALUE INTO @SLAVE_STATUS |
05 |
FROM information_schema.GLOBAL_STATUS |
06 |
WHERE VARIABLE_NAME= ‘SLAVE_RUNNING‘ ; |
07 |
IF ( ‘ON‘ != @SLAVE_STATUS) THEN |
08 |
SET GLOBAL SQL_SLAVE_SKIP_COUNTER=0; |
Because statements like SHOW SLAVE STATUS cannot be called in a stored procedure, exact replication error messages and error codes cannot be obtained and further processing of SLAVE stops is not possible.
* Next, create a task
1 |
CREATE EVENT IF NOT EXISTS `Slave_Monitor` |
2 |
ON SCHEDULE EVERY 5 SECOND |
A task is created, executed every 5 seconds, and the task is retained after the task is completed, rather than deleted. Of course, the task in this example will not end unless it is manually disabled.
*
If you want to temporarily close a task while running, execute the ALTER EVENT statement:
(root:localhost:) test> alter event ' Slave_monitor ' on
Completion PRESERVE DISABLE;
(root:localhost:) test> alter event ' Slave_monitor ' on
Completion PRESERVE ENABLE;
[Original from: http://blog.csdn.net/mer1234567/article/details/7514855]