mysql event timing task

Lead:

Recently I used mysql event, so I learned a wave and recorded it.
MySQL has added the event function since 5.7, which is similar to linux crontab, which is a timed task used to perform certain tasks at a time or periodically.

The main features and attributes of event:

1. The event in mysql uniquely identifies an event based on its name and schema.

2. The event can be composed of a sql statement or a BEGIN-END code block. Event can be executed once or periodically. Periodic execution can be set with a regular start time and end time, or you can set the start time and end time separately, or you can set neither.
By default, as long as the timing task is set successfully, it will continue to execute until the event is unavailable or deleted.
Points to note here: If an event is not executed in a cycle, another event instance will also start to execute. For example, the A event is set to be executed once a minute, and the second minute has not been completed, at this time another new event instance will also start to execute. If these instances are competing for resources, it may cause the MySQL service to become very slow.

3. The user can create, modify, and delete events. Syntactically incorrect creation or modification statements will directly throw error messages. The user can write operation statements that the user does not have permission to in the action of the event. The
creation and modification can be successful, but an exception will be thrown when executed. For example, the user only has the select permission for table A. He updates table A in the event. The event can be created or modified successfully, but an error will be reported when executed (this is a pit, I thought it was OK after writing, but the result is Only at
the time of execution will I know if there are any problems).

4. The sql statement can modify the event name, time, execution cycle, status (enabled or disabled), schema, etc.

5. The creator of the event is the user who created the event by default, but if other users also modify the event, the creator of the event will be changed to the last user who modified the event. As long as the user has the permission to modify the event of the database, the content of the event can be modified.

Event settings:

The parameter value of event is called event_scheduler, which has three values: NO, OFF, and DISABLED. The corresponding semantics are open, close and unavailable.
Note: The parameter values ​​set on the command line will become invalid after mysql restart. Only the values ​​modified in the configuration file will not become invalid after restart.

Open event:
enter in the mysql command line:

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

Close event

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

Although ON and 1, OFF and 0 are equivalent, try to use ON and oFF, because there is another value DISABLED that corresponds to a value without numbers, so try to use English.
Note:
The event scheduler can be set to DISABLED only when the server is started.
If event_scheduler is ON or OFF, it cannot be set to DISABLED at runtime.
And if the event scheduler is set to DISABLED at startup, the value of event_scheduler cannot be changed at runtime.

There are two ways to set event as unavailable. There are two
command lines:

--event-scheduler=DISABLED

Configuration file

event_scheduler=DISABLED

Syntax of event:

1.event creation:

CREATE
    [DEFINER = user]
    EVENT
    [IF NOT EXISTS]
    event_name
    ON SCHEDULE schedule
    [ON COMPLETION [NOT] PRESERVE]
    [ENABLE | DISABLE | DISABLE ON SLAVE]
    [COMMENT 'string']
    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}

This is the official creation specification, which looks a bit daunting. Let's take an example to help understand it.
Let's create a select statement that executes every minute (in fact, the query is meaningless, just as a demonstration)

CREATE EVENT test_insert   (创建名为test_insert的event)
  ON SCHEDULE EVERY 1 MINUTE   (每一分钟跑一次)
  DO                            (执行查询语句)
    SELECT NOW();

The above is the event of a single statement. Let's see if it is a compound statement, use BEGIN-END to complete.

delimiter //

CREATE EVENT test_insert
    ON SCHEDULE
      EVERY 1 MINUTE
    COMMENT '事件添加的注释'
    DO
      BEGIN
        INSERT INTO XXX;
        UPDATE XXX;
      END //

delimiter ;

If the reader has written a stored procedure, it will be easy to understand this multi-line statement, which is not much different from the syntax of writing a stored procedure, except that the process of creating an event is added.
Event can also call the stored procedure periodically, put the content to be executed in the stored procedure, and then use the event to call, rather than directly write all the logic into the event.
Because event execution will not have error messages, and there will be no error logs, it is difficult to debug. If it is in the storage process, you can manually call the storage process. If there is a problem, you can directly check the problem in the statement based on the error message.

2. Modify event:

Basically, it is the same as creating, directly paste the official specification.

ALTER
    [DEFINER = user]
    EVENT event_name
    [ON SCHEDULE schedule]
    [ON COMPLETION [NOT] PRESERVE]
    [RENAME TO new_event_name]
    [ENABLE | DISABLE | DISABLE ON SLAVE]
    [COMMENT 'string']
    [DO event_body]

3. Delete event:

Deleting an event is based on the name of the event.

DROP EVENT [IF EXISTS] event_name

4. View event metadata:

查看事件的三种方式:
SELECT * FROM INFORMATION_SCHEMA.EVENTS;
SHOW EVENTS;
SELECT * FROM mysql.event;


查看创建event的创建语句:
SHOW CREATE EVENT

5. Grant event permissions

The permission of event is called EVENT. The following is a general empowerment statement, which grants the user user the permission of the database table event on the host.

GRANT EVENT ON database.table TO user@host;

to sum up:

1.event is equivalent to the timing task that comes with mysql, which can be used to process some periodic tasks, such as calculating statistics of certain tables every month. More convenient.
2. The function of event is not powerful enough, debugging is actually more troublesome, and error messages are not printed. It is recommended to call the stored procedure in the event, so that you can directly CALL to call the stored procedure to be executed in the event to determine whether there is a problem in the statement . Another way is to write the data information of the execution event to a log table when the event is executed, so that it is clear whether the event is executed or not and whether it reports an error at a glance.
3. The interval of events should be set reasonable, because events can be executed concurrently. If the first event is not executed, and the row lock or table lock is still held, the execution of the second event will be blocked, and it will not be executed. , The next event is here again. This may degrade the performance of the database very low.

Guess you like

Origin blog.csdn.net/sc9018181134/article/details/107452844