MySQL timing execution script (scheduled task) command instance

In mysql, we can directly set some parameters to make it perform some tasks for us regularly. Although this can be achieved by using scheduled tasks in windows or linux, mysql itself can also be completed.

Check if event is enabled

Copy the code The code is as follows:


show variables like '%sche%';

Activate the event plan

Copy the code The code is as follows:

set global event_scheduler =1;


 

Create stored procedure test

 

Copy the code The code is as follows:


CREATE PROCEDURE test () 
BEGIN 
update examinfo SET endtime = now() WHERE id = 14; 
END;

Create event e_test

Copy the code The code is as follows:


create event if not exists e_test 
on schedule every 30 second 
on completion preserve 
do call test();

The stored procedure test will be executed every 30 seconds to update the current time to the endtime field of the record with id=14 in the examinfo table.

Close event task

Copy the code The code is as follows:


alter event e_test ON 
COMPLETION PRESERVE DISABLE;

Account opening event task

Copy the code The code is as follows:


alter event e_test ON 
COMPLETION PRESERVE ENABLE;

The above tests are all successful, the test environment is mysql 5.4.2-beta-community mysql community server (GPL)

The above related content is an introduction to MySQL timing execution, I hope you can gain something.

mysql scheduled task disappears after restart

We just need to modify a configuration

event_scheduler is set to OFF in mysql's config. Go to mysql and change the configuration to ON and you will be done.

For more details, you can see below

A new feature, EVENT, was introduced in MySQL 5.1.x. As the name implies, it is an event and timed task mechanism, which executes specific tasks in a specified time unit. Therefore, in the future, some timed operations on data will no longer rely on external programs, but directly Use the functionality provided by the database itself.

To check whether the event scheduler is currently enabled, execute the following SQL:

SHOW VARIABLES LIKE 'event_scheduler';

SELECT @@event_scheduler;

SHOW PROCESSLIST;
if displayed:

+-----------------+-------+
| Variable_name | Value |
+----------------- +-------+
| event_scheduler | OFF |
+-----------------+-------+
is executable

SET GLOBAL event_scheduler = 1;

SET GLOBAL event_scheduler = ON;
to open, you can also directly add "--event_scheduler=1" to the startup command, for example:

mysqld ... --event_scheduler=1


[mysqld] in my.ini or my.cnf
add event_scheduler=ON

Create event (CREATE EVENT)
first look at its 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}

1) Let's first look at a simple example to demonstrate inserting one record per second into the data table

USE test;
CREATE TABLE aaa (timeline TIMESTAMP);
CREATE EVENT e_test_insert
ON SCHEDULE EVERY 1 SECOND 
DO INSERT INTO test.aaa VALUES (CURRENT_TIMESTAMP);
wait for 3 seconds, and then execute the query successfully.

2) Clear the test table after 5 days:

CREATE EVENT e_test
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 5 DAY
DO TRUNCATE TABLE test.aaa;
3) Clear the test table at 12:00 on July 20, 2007:

CREATE EVENT e_test
ON SCHEDULE AT TIMESTAMP '2007-07-20 12:00:00'
DO TRUNCATE TABLE test.aaa;
4) Clear the test table regularly every day:

CREATE EVENT e_test
ON SCHEDULE EVERY 1 DAY
DO TRUNCATE TABLE test.aaa;
5) After 5 days, open the test table to clear the test table regularly every day:

CREATE EVENT e_test
ON SCHEDULE EVERY 1 DAY
STARTS CURRENT_TIMESTAMP + INTERVAL 5 DAY
DO TRUNCATE TABLE test.aaa;

6) Clear the test table regularly every day, and stop executing it after 5 days:

CREATE EVENT e_test
ON SCHEDULE EVERY 1 DAY
ENDS CURRENT_TIMESTAMP + INTERVAL 5 DAY
DO TRUNCATE TABLE test.aaa;

7) After 5 days, open the test table to clear the test table every day, and stop executing it after one month:

CREATE EVENT e_test
ON SCHEDULE EVERY 1 DAY
STARTS CURRENT_TIMESTAMP + INTERVAL 5 DAY
ENDS CURRENT_TIMESTAMP + INTERVAL 1 MONTH
DO TRUNCATE TABLE test.aaa;
[ON COMPLETION [NOT] PRESERVE] You can set whether this event is executed once or persistently, the default is NOT PRESERVE .

8) Clear the test table regularly every day (execute only once, and terminate the event after the task is completed):

CREATE EVENT e_test
ON SCHEDULE EVERY 1 DAY
ON COMPLETION NOT PRESERVE
DO TRUNCATE TABLE test.aaa;
[ENABLE | DISABLE] But it can be set whether the state is turned on or off after the event is created, the default is ENABLE.
[COMMENT 'comment'] can add a comment to this event.

ALTER EVENT
ALTER EVENT event_name
[ON SCHEDULE schedule]
[RENAME TO new_event_name]
[ON COMPLETION [NOT] PRESERVE]
[COMMENT 'comment']
[ENABLE | DISABLE]
[DO sql_statement]
1) Temporary shutdown event

ALTER EVENT e_test DISABLE;
2) Enable event

ALTER EVENT e_test ENABLE;
3) Change the test table to be emptied every day to once every 5 days:

ALTER EVENT e_test
ON SCHEDULE EVERY 5 DAY;

The DROP EVENT
syntax is simple as follows:

DROP EVENT [IF EXISTS] event_name
For example, delete the previously created e_test event

DROP EVENT e_test;
Of course, the premise is that this event exists, otherwise ERROR 1513 (HY000): Unknown event error will be generated, so it is best to add IF EXISTS

DROP EVENT IF EXISTS e_test;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324983561&siteId=291194637