Scheduled task creation for MySQL _ MySQL

Source: Internet
Author: User
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

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.