MySQL scheduled task implementation and usage examples

MySQL scheduled task implementation and usage examples

This article uses examples to illustrate the implementation and use of MySQL scheduled tasks. Share with you for your reference, the details are as follows:

MySQL 5.1.6 adds an event scheduler that can perform scheduled tasks (scheduled deletion of records, scheduled data statistics), replacing the previous system's scheduled tasks. The MySQL event scheduler can execute a task accurately per second.

The difference between event scheduler and trigger: event scheduler triggers the execution of certain tasks based on a specific time period, while trigger is triggered based on the events generated by a certain table.

1. Check whether it is turned on

> show variables like 'event_scheduler';

2. Enable the event scheduler

set global event_scheduler = on;

The settings here will be automatically closed when MySQL is restarted. If you need to keep it turned on, you need to configure it in my.ini as follows:

event_scheduler = on

3. Create event syntax

CREATE EVENT [IF NOT EXISTS ] event_name
ON SCHEDULE schedule
[ ON COMPLETION [ NOT ] PRESERVE ]
[ ENABLE | DISABLE ]
[ COMMENT 'Comment' ]
DO SQL statement;
 
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 }

event_name: event name, with a maximum length of 64 characters.

schedule: execution time.

[ON COMPLETION [NOT] PRESERVE]: Whether the event needs to be reused.

[ ENABLE | DISABLE ] : Enable or disable the event.

4. Closing Events

ALTER EVENT event_name DISABLE;

5. Opening events

ALTER EVENT event_name ENABLE;

6. Deleting events

DROP EVENT [IF EXISTS ] event_name;

7. View all events

SHOW EVENTS;

8. Event Examples

Let's create a simple test table for testing.

CREATE TABLE `test` (
 `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',
 `now` datetime DEFAULT NULL COMMENT 'time',
 PRIMARY KEY (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

There are two types of events, one is interval triggering and the other is specific time triggering.

We insert a record into the test table every second:

DROP EVENT IF EXISTS event_test;
CREATE EVENT event_test
ON SCHEDULE EVERY 1 SECOND STARTS '2017-08-22 11:57:00' ENDS '2017-08-22 12:00:00'
ON COMPLETION PRESERVE
ENABLE
COMMENT 'Insert records into the test table every second'
DO INSERT INTO test VALUES(NULL, now());

We specify the time to insert a record into the test table:

DROP EVENT IF EXISTS event_test2;
CREATE EVENT event_test2
ON SCHEDULE AT '2017-08-22 12:01:00'
ON COMPLETION PRESERVE
ENABLE
COMMENT 'Insert records into the test table at specified time'
DO INSERT INTO test VALUES(999999, now());

Readers who are interested in more MySQL-related content can check out the following topics on this site: "MySQL query skills", "MySQL transaction operation skills", "MySQL stored procedure skills", "MySQL database lock related skills summary" and "MySQL common function summary"

I hope this article will be helpful to everyone's MySQL database design.

You may also be interested in:
  • How to configure MySQL scheduled tasks (EVENT events) in detail
  • Analysis of the method of setting up scheduled tasks in mysql
  • Detailed explanation of mysql scheduled tasks (event events)
  • How to implement Mysql scheduled task backup data under Linux
  • MySQL scheduled task example tutorial
  • How to implement Mysql scheduled tasks under Linux
  • Analysis and solution of the reasons why MySQL scheduled tasks cannot be executed normally

<<:  The process of using vxe-table to make editable tables in vue

>>:  Linux file management command example analysis [display, view, statistics, etc.]

Recommend

Detailed explanation of the use of Arguments object in JavaScript

Table of contents Preface Basic Concepts of Argum...

Full HTML of the upload form with image preview

The upload form with image preview function, the ...

How to mount a disk in Linux and set it to automatically mount on boot

Knowing that everyone's time is precious, I w...

Sample code for installing ASPNET.Core3.0 runtime in Linux

# The following examples are for x64-bit runtime ...

centos 7 modify sshd | prohibit root login and sshd port script definition

1. Create a new user wwweee000 [root@localhost ~]...

Solution to the problem of installing MySQL compressed version zip

There was a problem when installing the compresse...

Videojs+swiper realizes Taobao product details carousel

This article shares the specific code of videojs+...

How to make a List in CocosCreator

CocosCreator version: 2.3.4 Cocos does not have a...

Detailed explanation of the pitfalls of mixing npm and cnpm

Table of contents cause reason Introduction to NP...

Practical record of solving MySQL deep paging problem

Table of contents Preface Why does limit deep pag...

How to use vs2019 for Linux remote development

Usually, there are two options when we develop Li...

Detailed explanation of MySQL delayed replication library method

Simply put, delayed replication is to set a fixed...

VS2019 connects to mysql8.0 database tutorial with pictures and text

1. First, prepare VS2019 and MySQL database. Both...

Detailed example of locating and optimizing slow query sql in MySQL

Table of contents 1. How to locate and optimize s...

Basic concepts and common methods of Map mapping in ECMAScript6

Table of contents What is a Mapping Difference be...