1. Introduction
Events are procedural database objects that MySQL invokes at corresponding moments. It is a tool of MYSQL. An event can be called once or started periodically. It is managed by a specific thread, which is the so-called "event scheduler".
2. Create grammar
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}
Glossary
event_name : The created event name (uniquely determined).
ON SCHEDULE: Schedule tasks.
schedule: Determine the execution time and frequency of the event (note that the time must be in the future, the past time will be wrong), there are two forms of AT and EVERY.
[ON COMPLETION [NOT] PRESERVE]: Optional, the default is ON COMPLETION NOT PRESERVE, that is, the event is automatically dropped after the scheduled task is executed; ON COMPLETION PRESERVE will not drop it.
[COMMENT 'comment'] : Optional, comment is used to describe the event; it is quite a comment, the maximum length is 64 bytes.
[ENABLE | DISABLE]: Set the state of the event, the default ENABLE: indicates that the system tries to execute this event, DISABLE: close the event, you can use alter to modify
DO event_body: SQL statement to be executed (can be a compound statement). CREATE EVENT is legal when used in stored procedures.
3. Turn on and off the event scheduler
1. Check whether it is enabled
show variables like '%event_scheduler%';
The default is off, not enabled
2. Open the event scheduler
SET GLOBAL event_scheduler = ON;
SET @@global.event_scheduler = ON;
SET GLOBAL event_scheduler = 1;
SET @@global.event_scheduler = 1;
Or through the configuration file my.cnf
event_scheduler = 1 # or ON
to view the scheduler thread:
show processlist;
3. Close the event scheduler
SET GLOBAL event_scheduler = OFF;
SET @@global.event_scheduler = OFF;
SET GLOBAL event_scheduler = 0;
SET @@global.event_scheduler = 0;
4. View scheduler thread tasks
SELECT @@event_scheduler;
SHOW PROCESSLIST;
show events; #View the current schema scheduling task
select * from mysql.event;#View the entire database scheduling task
4. Create a test table
1. Simple omission
2. Create event 1 (immediately start the event)
create event event_now
on schedule
at now()
do insert into events_list values('event_now', now());
3、创建事件2(每分钟启动事件)
create event test.event_minute
on schedule
every 1 minute
do insert into events_list values('event_now', now());
4. Create event 3 (start event every second)
CREATE event event_now
ON SCHEDULE
EVERY 1 SECOND
DO INSERT INTO event_test VALUES(1);
5. Create event 4 (call the stored procedure every second)
CREATE DEFINER=`root`@`localhost` EVENT `eventUpdateStatus`
ON SCHEDULE EVERY 1 SECOND
STARTS '2017-11-21 00:12:44'
ON COMPLETION PRESERVE
ENABLE
DO call updateStatus()
6. The creation event will be executed at 4:00 a.m. on April 1 every year
CREATE EVENT Crontab_UpdateTask
ON SCHEDULE EVERY 1 YEAR STARTS DATE_ADD(DATE(CONCAT(YEAR(CURDATE()) + 1,'-',4,'-',1)),INTERVAL 4 HOUR)
ON COMPLETION PRESERVE ENABLE
DO
BEGIN
update `user` set `passWord` = '66666';
END