Create a scheduled task for MySQL

Source: Internet
Author: User

In the actual project of creating a MySQL scheduled task, 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 by using SQL commands, event sched provided by Mysql can be easily implemented. The specific steps for www.2cto.com are 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 (delete_old_record in this example) www.2cto.com 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; after that, the database executes 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}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.