The implementation of mysql timing task

Keywords: MySQL Stored Procedure Linux crontab

1. Requirements: from 10:00 p.m. to 5:00 a.m. every day, execute the stored procedure regularly every 10 minutes.

2. Implementation mode: there are two ways to achieve it:

One is more conventional. Using mysql event timing task, first introduce the related commands of event timing task in mysql,

To see if event is enabled:

  1. SELECT @@event_scheduler;  
  2. SHOW VARIABLES LIKE 'event%';  

To start a scheduled task:

  1. set GLOBAL event_scheduler = 1;  
  2. SET GLOBAL event_scheduler = ON;  

To establish a scheduled task:

  1. DROP EVENT IF EXISTS JOB_ALARM;  
  2. CREATE EVENT JOB_ALARM  
  3.  ON SCHEDULE EVERY 10 MINUTE  
  4. DO  
  5.   BEGIN  
  6.    if(date_format(current_time(),'%H')>22 || date_format(current_time(),'%H')<5) THEN  
  7.      CALL  PRO_ALARM();  
  8.    END IF;  
  9. END  

To establish a stored procedure:

  1. DROP PROCEDURE IF EXISTS PRO_ALARM;  
  2. CREATE PROCEDURE PRO_ALARM()  
  3.   BEGIN  
  4.     DECLARE userId VARCHAR(32);  
  5.   
  6.     #This is used to handle the cursor reaching the last row
  7.     DECLARE s INT DEFAULT 0;  
  8.   
  9.     #Declare cursor "cursor" name (cursor "name is a multi row result set)
  10.     DECLARE cursor_data CURSOR FOR  
  11.       SELECT tmp.USER_ID  
  12.       FROM (  
  13.              SELECT  
  14.                e.USER_ID,  
  15.                MAX(e.TIME_GMT) TIME_GMT  
  16.              FROM EVENTS e  
  17.              GROUP BY e.USER_ID  
  18.              HAVING MAX(e.TIME_GMT) < UNIX_TIMESTAMP() - 60 * 30  
  19.                     AND MAX(e.TIME_GMT) > UNIX_TIMESTAMP() - 60 * 60 * 24) tmp  
  20.         INNER JOIN EVENTS t ON tmp.USER_ID = t.USER_ID  
  21.                                      AND tmp.TIME_GMT = t.TIME_GMT  
  22.       WHERE TYPE_ID != '34001';  
  23.   
  24.     #Set a termination flag
  25.     DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET s = 1;  
  26.   
  27.     OPEN cursor_data;  
  28.   
  29.     #Get the record of the current cursor pointer, read a row of data and pass it to variables A and B
  30.     FETCH cursor_data  
  31.     INTO userId;  
  32.   
  33.     #Start the loop to determine whether the cursor has reached the end as a loop condition
  34.     WHILE s <> 1 DO  
  35.       INSERT INTO EVENTS_NOTIFICATION VALUES (NULL, SYSDATE(), UNIX_TIMESTAMP(SYSDATE()),  
  36.                                                     UNIX_TIMESTAMP(SYSDATE()), '00000', userId, '1''0');  
  37.       #Read data from next row
  38.       FETCH cursor_data  
  39.       INTO userId;  
  40.     END WHILE;  
  41.     #Close cursor
  42.     CLOSE cursor_data;  
  43.   
  44.   END;  

There are some limitations when configuring timers, and since mysql used by bloggers enables skip grant tables, when setting event on, it always reports an error, so the second implementation method is proposed. The specific error information is as follows:

  1. [HY000][1290] The MySQL server is running with the --event-scheduler=DISABLED or --skip-grant-tables option so it cannot execute this statement  


The second way is to use the timing task of linux,

Basic command of linux timing task:

View scheduled tasks: crontab -l

Edit scheduled task: crontab -e

* / 10 22-23,0-5 * * mysql -u user name - p password - e "use db_name;CALL PRO_ALARM();"

Or save use db_name;CALL PRO_ALARM(); in the sql script. The editing timing task is as follows:

* / 10 22-23,0-5 * * mysql -u user name - p password < / application / job_mysql.sql

Posted by elibizif on Wed, 01 Apr 2020 15:46:14 -0700