Mysql5.7 creates timed events

1. First introduce the basic operation process

1 Introduction

Use the Navicat tool to write stored procedures and execute them regularly. This article is based on my own experience and is only used as a record.

2 steps

2.1 New process

2.2 Write the code you need to execute in the function body

1

2

3

4

5

6

7

8

9

10

11

12

13

14

CREATE DEFINER=`root`@`localhost` PROCEDURE `test`()

BEGIN

    #Routine body goes here...

    #DECLARE int;

    SET @time = NOW();

    SET @item = 'num';

    SET @type = 'day';

     

    select content into @content from rank where  uid = 123456 and num >0;

     

        INSERT INTO rank2(time,item,type,content) VALUES(@time,@item,@type,@content);

 

        UPDATE rank SET num=0 WHERE userid >0;

END       

2.3 Create an event

2.3.1 New event

2.3.2 Define the procedure to be called

2.3.3 Fill in your timing plan

The code preview is as follows:

1

2

3

4

CREATE EVENT `nodejs`.`Untitled`

ON SCHEDULE

EVERY '1' DAY STARTS '2018-07-25 00:00:00'

DO call test();    

3 Abnormal

If you find that you have set all the steps and it hasn't worked, it may be that the timer switch is not turned on.

Check if it is enabled, the method show variables like'event%';

If it is OFF, use SET GLOBAL event_scheduler = 1; or SET GLOBAL event_scheduler = ON; enable. After it is turned on, its Value is ON.

 

The above is reproduced from: https://www.cnblogs.com/fanbi/p/9361204.html

2. Stop and start timed events 

1. Create an event

DELIMITER $$ #Create

a timed distribution order event
CREATE event jobOrderSplit #Open

timed
ON SCHEDULE

#3 minutes to run
EVERY 3 MINUTE #Every day at 9:10

, 3 minutes to split orders
STARTS '2015-09-23 09:10 :00'

DO

BEGIN #Call

the stored procedure of automatic splitting
CALL automaticSplitting();

END;

DELIMITER;

 

2. Turn on the event

#Set global variables for startup

set global event_scheduler=1/ON; 

#Start a single event

ALTER EVENT eventName ON COMPLETION PRESERVE ENABLE;

 

3. Stop the event

#Set closed global variables

set global event_scheduler=0/OFF; 

#Start a single event

ALTER EVENT eventName ON COMPLETION PRESERVE DISABLE;

 

Another: if all variables of the event are closed, even if a separate event is opened, it will not be executed

Check whether the MySQL global variable event is turned on ON: On OFF: Off

SHOW VARIABLES LIKE 'event_scheduler';

The above is reproduced from: https://www.iteye.com/blog/wangduorong-2300769

Three. Possible problems

Timing event event_scheduler problem

1.查看事件是否开启

    1-1.show variables like 'event_scheduler';    

    1-2.select @@event_scheduler;

    1-3.show processlist;

    如果event_scheduler=off 没有开启  on开启

    1-4开启event_scheduler

        set global event_scheduler = 'on';

    //注意:还是要在my.cnf中添加event_scheduler=ON。否则mysql重启事件又会回到原来的状态了。

 2.查看事件

       select * from mysql.event

以上转自:https://blog.csdn.net/w892824196/article/details/86622287

Guess you like

Origin blog.csdn.net/ClearLoveQ/article/details/106672838