Create bitsCN.com for MySQL scheduled tasks
Create a scheduled task for MySQL
In actual projects, you only want to save the records of the last seven days in the MySQL database. it is troublesome and inefficient to delete the records using SQL commands, event sched provided by Mysql can be easily implemented.
The procedure is as follows:
1: log on to the MySQL console as a super user
# Mysql-uroot
2: enable event_scheduler (disabled by default)
Mysql> set global event_scheduler = ON;
3: Create an event (in this example, name it delete_old_record)
Mysql> create event delete_old_record
On schedule every 1 day starts now ()
DO
-- Delete the old records of demo_partition table
Delete from demo_partition table WHERE datediff (NOW (), log_timestamp)> = 7;
-- Delete the old records of puma_2_table
Delete from demo_2_table WHERE datediff (NOW (), log_timestamp)> = 7;
-- Delete the old records of puma_3_table
Delete from demo_3_table WHERE datediff (NOW (), log_timestamp)> = 7;
-- Delete the old records of puma_4_table
Delete from demo_4_table WHERE datediff (NOW (), log_timestamp)> = 7;
4: Start the created event
Mysql> alter event delete_old_record ENABLE;
In this way, the database will execute the jobs after DO every day to delete the records of each table 7 days ago.
Appendix: The create event format is as follows:
CREATE
[DEFINER = {user | CURRENT_USER}]
EVENT
[If not exists]
Event_name
On schedule schedule
[On completion [NOT] PRESERVE]
[ENABLE | disable on slave]
[COMMENT 'comment']
DO event_body;
Schedule:
AT timestamp [+ INTERVAL interval]...
| EVERY interval
[STARTS timestamp [+ INTERVAL interval]...]
[ENDS timestamp [+ INTERVAL interval]...]
Interval:
Quantity {YEAR | QUARTER | MONTH | DAY | HOUR | MINUTE |
WEEK | SECOND | YEAR_MONTH | DAY_HOUR | DAY_MINUTE |
DAY_SECOND | HOUR_MINUTE | HOUR_SECOND | MINUTE_SECOND}
BitsCN.com