mysql cron job (event event)

1. Introduction Event

Events (event) is the process of MySQL database objects in the appropriate time to call. An event can be called once, but also periodically launch, which consists of a particular thread to manage, that is, the so-called "event scheduler."

Similar events and triggers are activated when certain things happen. When you start a statement on the database when started the trigger, and the event is based on the scheduled event to start. Due to the similarity of their each other, so the event it is also known as temporary triggers.

Event replaced the original work by the scheduled tasks can only be performed in the operating system, and MySQL's event scheduler can be accurate to perform a task per second, and the operating system scheduled tasks (such as: under CRON in Linux or Windows task Scheduler) can only be accurate to be performed once per minute.

Advantages and disadvantages of 2 events
2.1 advantages
some regular operations of the data is no longer dependent on external programs, databases directly using the function provided by itself.
Per second can be achieved to perform a task, which in some demanding real-time environment is very practical.

2.2 shortcomings
timed trigger, it can not be called.

3 Create an event

A statement to create event create an event. Each event consists of two main parts, the first part is scheduled event (eventschedule, when and in what represents a start event start frequency;

The second part is the event action (event action), which is the code that executes when the event started, the action event contains a SQL statement, it might be a simple insert or update statement, you can also make a stored procedure or
benin ... end statement block, both of which allow us to execute multiple SQL.

An event can be active (open) or stops (shut down), the activity mean that the event scheduler event action must be called to check, stop the declaration means that the event is stored in the directory, but the scheduler does not check whether it should call . After an event is created, it immediately becomes active, event-an activity may be performed one or more times.

3.1 Creating syntax is as follows

    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: Event name created (uniquely determined).
ON SCHEDULE: Scheduled Tasks.
schedule: the decision of execution time and frequency event (note the time it must be a future time, past time to be wrong), and there are two forms of AT EVERY.
[ON COMPLETION [NOT] PRESERVE] : Optional, default is ON COMPLETION NOT PRESERVE automatically drop the event that is planned after the task is completed; ON COMPLETION PRESERVE will not drop out.
[COMMENT 'comment']: Optional, used to describe Comment Event; considerable comment, the maximum length of 64 bytes.
[ENABLE | DISABLE]: setting state event of default ENABLE: indicates that the system attempts to perform this event, DISABLE: close the matter can be modified using the ALTER
DO event_body: SQL statement to be executed (can be a compound statement). Legitimate when used in a stored procedure CREATE EVENT.

3.2 On Off Event Scheduler
3.2.1 MySQL event scheduler event_scheduler responsible for calling the event, which is off by default. The scheduler constantly monitors whether an event is to be called, to create an event, you must open the scheduler.

mysql> show variables like '%event_scheduler%'; 
+-----------------+-------+ | Variable_name | Value | +-----------------+-------+ | event_scheduler | OFF | +-----------------+-------+ 

3.2.2 open event scheduler
via the command line
by any one of the following command line

SET GLOBAL event_scheduler = ON; 
SET @@global.event_scheduler = ON; SET GLOBAL event_scheduler = 1; SET @@global.event_scheduler = 1; 

By the configuration file my.cnf

event_scheduler = 1 #或者ON 

View scheduler thread

mysql> show processlist; 
+----+-----------------+-----------+------+---------+------+------------------------+------------------+ 
| Id | User            | Host      | db   | Command | Time | State                  | Info             | 
+----+-----------------+-----------+------+---------+------+------------------------+------------------+ 
|  2 | root            | localhost | NULL | Query   |    0 | NULL                   | show processlist | | 3 | event_scheduler | localhost | NULL | Daemon | 6 | Waiting on empty queue | NULL| +----+-----------------+-----------+------+---------+------+------------------------+------------------+ 

3.2.3 Close event scheduler
command line
by any of the following a command line

SET GLOBAL event_scheduler = OFF; 
SET @@global.event_scheduler = OFF; SET GLOBAL event_scheduler = 0; SET @@global.event_scheduler = 0; 

My.cnf profile by
increasing [mysqld] at

event_scheduler = 0 #或者OFF,DISABLED 

View scheduler thread

mysql> show processlist; 
+----+------+-----------+------+---------+------+-------+------------------+ 
| Id | User | Host      | db   | Command | Time | State | Info             | 
+----+------+-----------+------+---------+------+-------+------------------+ 
|  2 | root | localhost | NULL | Query   |    0 | NULL  | show processlist | +----+------+-----------+------+---------+------+-------+------------------+ 

3.3 For example: Create a dispatch table records every event name and event stamp
3.3.1 Create Test Table

mysql> drop table if exists events_list; 
mysql> create table events_list(event_name varchar(20) not null, event_started timestamp not null); 

3.3.2 Creating Event 1 (immediately start event)

create event event_now  
on schedule  
at now() do insert into events_list values('event_now', now()); 

View event execution results

mysql> select * from events_list; 
+------------+---------------------+ 
| event_name | event_started       | 
+------------+---------------------+ 
| event_now  | 2014-07-01 04:06:40 | 
+------------+---------------------+ 

3.3.3 Creating Event 2 (start event per minute)

create event test.event_minute  
on schedule  
every  1 minute do insert into events_list values('event_now', now()); 

View event execution results

mysql> select * from events_list; 
+------------+---------------------+ 
| event_name | event_started       | 
+------------+---------------------+ 
| event_now  | 2014-07-01 04:26:53 | 
| event_now  | 2014-07-01 04:27:53 | 
| event_now  | 2014-07-01 04:28:53 | 
+------------+---------------------+ 

3.3.3 Creating Event 3 (start events per second)

CREATE event event_now  
ON SCHEDULE  
EVERY 1 SECOND DO INSERT INTO event_test VALUES(1); 

3.3.4 Creating Event 4 (per call a stored procedure)

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() 

3.4 Note:
The default event created is stored in the current library, can also display the specified event in which the library is created
by show events only view the current library event created
an event that is executing the release, such as the implementation of the event immediately after the implementation of the event It will be automatically deleted, calling the event multiple times or waiting to be executed can view the event.
If two events at the same time need to call, mysql will determine the order of their call, if you want to specify the order, the need to ensure the implementation of at least one event after another incident one second
for recursive scheduled event, the end date can not be before the start date.
select can be included in one event, but his results disappear, as if not executed.

4 View Event
View current library event
mysql> show events;
view all events
mysql> select * from mysql.event;

History article:
JAVA micro-channel enterprises to change payment (ten minutes to get)
micro letter authorizing the acquisition method and steps users openId of
a micro-environmental signals simultaneously support multiple pages authorized
micro-channel two kinds of signature algorithm MD5 and HMAC-SHA256

Guess you like

Origin www.cnblogs.com/angryjj/p/11324590.html