MySQL设置定时任务

1.查看event 事件是否开启

show variables like '%sche%';

2.如没开启,则开启。需要数据库超级权限

set global event_scheduler =1;

3.创建定时任务

创建每隔一分钟执行的事件

create event if not exists event_insertdt on schedule every 1 minute   
on completion preserve   
do insert into test_data (receivetime, distance)values(current_timestamp, '0.3');

创建每天凌晨1点执行的事件

create event if not exists event_insertdt  
on schedule every 1 day starts date_add(date_add(curdate(), interval 1 day), interval 1 hour)   
on completion preserve enable  
do insert into test_data (receivetime, distance)values(current_timestamp, '0.3');

4.查看定时任务

select * from information_schema.events;

5.关闭定时任务

drop event event_insertdt;

你可能感兴趣的:(数据库)