文章

MySQL 创建 event 实现简单定时任务

MySQL 创建 event 实现简单定时任务。

# 查看事件调度器状态
SHOW VARIABLES LIKE 'event_scheduler';
# 未开启则开启
set persist event_scheduler=on;

# 6小时后执行
CREATE EVENT my_insert_event
    ON SCHEDULE AT CURRENT_DATE + INTERVAL 6 HOUR
    DO
        INSERT INTO your_table (column1, column2) VALUES ('value1', 'value2');
# 每天6点执行
CREATE EVENT my_daily_insert_event
    ON SCHEDULE EVERY 1 DAY
    STARTS TIMESTAMP(CURRENT_DATE, '06:00:00')
    DO
        INSERT INTO your_table (column1, column2) VALUES ('value1', 'value2');
# 明天6点执行
CREATE EVENT my_daily_insert_event
    ON SCHEDULE EVERY 1 DAY
    STARTS CONCAT(CURRENT_DATE + INTERVAL 1 DAY, ' 06:00:00')
    DO
        INSERT INTO your_table (column1, column2) VALUES ('value1', 'value2');
# 维护
# 查看 events 信息
SELECT * FROM information_schema.EVENTS;
# 删除
DROP EVENT my_insert_event;
# 停用
ALTER EVENT my_insert_event DISABLE;

License:  CC BY 4.0