mysql timing task (Navicat)

Table of contents

need

The project is online, but a function needs to be made to count the daily usage of the system. Then save the statistical results to the table.

analyze

The statistical code is very simple, but considering that the project is already online, if the statistical function is put into the java code, it needs to be redeployed, and this function is only for our development, so we do not intend to write the function Inside the java code. Then consider using mysql timing tasks. Set a fixed time every day to count the data of the previous day. Then save it to the table.

In mysql 5.1, a new feature event scheduler (Event Scheduler), referred to as the event. It can be used as a scheduled task scheduler, replacing some of the original work that can only be performed by scheduled tasks of the operating system. In addition, it is worth mentioning that the mysql event can execute a task every second, which is very practical in some environments with high real-time requirements.
The event scheduler is triggered and executed at regular intervals. From this point of view, it can also be called a "temporary trigger". However, it is different from a trigger. A trigger only executes some statements for events generated by a certain table, while an event scheduler executes some statements at a certain period (interval).

The version of the database I use is 5.7.25

accomplish

First check whether the version of mysql supports

SELECT VERSION();

insert image description here
If the version is not supported, you have to upgrade to the corresponding version

Check whether mysql has enabled event scheduling

Events are managed by a specific thread. After enabling the event scheduler, an account with SUPER privilege can see this thread by executing SHOW PROCESSLIST.

SHOW VARIABLES LIKE 'event_scheduler';

insert image description here
If the value is OFF or 1; it means it is not turned on. Events can be turned on or off with the SET GLOBAL command. Set the value of the event_scheduler parameter to ON to enable the event; if it is set to OFF, the event is disabled. (Setting system variables)
Mine has already been turned on, and the sql for turning on and off is pasted below;
turn on

SET GLOBAL event_scheduler = ON;

closure

SET GLOBAL event_scheduler = OFF;

Note: It is temporarily enabled by the above command. After mysql restarts, it will have no effect. If you want to always enable the event, you need to add it in my.ini (Windows system)/my.cnf (Linux system) event_scheduler=on.

Create an event
After opening Navicat
insert image description here
, you can click the event in the picture.
insert image description here
Click New Event
insert image description here

  • Define this block to write the code that needs to be executed. You can also define a stored procedure or function first, and call it directly here.
    insert image description here

  • Defined by: Specifies the user account to use when checking access rights when the event runs. The default definer value is the user running the CREATE EVENT statement. (Same as DEFINER = CURRENT_USER.) If a user value is given, it should be a MySQL account in the format 'user_name'@'host_name' (using the same format as the GRANT statement). Both user_name and host_name values ​​are required.

  • Status: An event can be created but can be kept inactive with the DISABLE keyword. Or you can use the default state of ENABLE, which is active.

  • ON COMPLETION: Normally, once the event has expired, it will be deleted immediately. This behavior can be overridden by specifying ON COMPLETION PRESERVE. ON COMPLETION NOT PRESERVE just makes the default non-persistent performance explicit.
    insert image description here
    The plan is the cycle of timing task execution

  • AT: Contains a date and time, or must be an expression that resolves to a datetime value. Use +INTERVAL to create an event that occurs sometime in the future relative to the current date and time.
    EVERY: For actions repeated at regular intervals, interval can be added after the EVERY clause. (+INTERVAL and EVERY cannot be used at the same time.)

  • STARTS: The EVERY clause may also contain an optional STARTS clause. The timestamp value after STARTS indicates when the action should start repeating, and the +INTERVAL interval can also be used to specify the amount of time "from now".
    Example: EVERY 3 MONTH STARTS CURRENT_TIMESTAMP + 1 WEEK means "every 3 months, starting one week from now".

  • ENDS: The EVERY clause may also contain an optional ENDS clause. The timestamp value after the ENDS keyword tells MySQL when to stop repeating events. You can also use +INTERVAL intervals with ENDS.
    Example: EVERY 12 HOUR STARTS CURRENT_TIMESTAMP + INTERVAL 30 MINUTE ENDS CURRENT_TIMESTAMP + INTERVAL 4 WEEK is equivalent to "every twelve hours starting thirty minutes from now and ending four weeks from now".
    Note: timestamp is an event that must occur in the future and cannot be scheduled in the past.

  • interval The interval consists of two parts, quantity and time unit.
    *YEAR | QUARTER | MONTH | DAY | HOUR | MINUTE |
    WEEK | SECOND | YEAR_MONTH | DAY_HOUR
    | DAY_MINUTE | DAY_SECOND
    insert image description here
    |
    insert image description here
    Once defined, there will be preview sql.
    insert image description here
    After everything is set, set the name and click Save. Then it can run at the time you specify.

Guess you like

Origin blog.csdn.net/javaXiaoAnRan/article/details/118333445