Mysql timer regularly deletes table data

     Because there is a log table in the test environment, the program inserts data wildly for 2 minutes without a regular schedule. As a result, this log table takes up 6.7G of space in less than a month. However, the log is refreshed quickly, and some logs are useless. Write a timer to delete the data of this table regularly

    First check whether mysql has the timed task switch turned on

    # SHOW VARIABLES LIKE 'event_scheduler';

    

If the Value is ON, it is turned on, and if it is OFF, it is turned off.

 If it is OFF, turn it on first:

#  SET GLOBAL event_scheduler = ON;

Then create the timer we want

DELIMITER $$
DROP EVENT IF EXISTS deleteLog;
CREATE EVENT  deleteLog
ON SCHEDULE EVERY 300 SECOND
ON COMPLETION PRESERVE
DO BEGIN
  delete from ftp_log where TO_DAYS(now())-TO_DAYS(createOn)>2;
END$$
DELIMITER ;

The script means: execute the plan every 300 seconds, and the action is to delete the data two days ago

After the creation is complete, view the timer

# select * from  mysql.event;


Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326858460&siteId=291194637