MySQL定时执行脚本(计划任务)命令实例

https://blog.csdn.net/weixin_33898876/article/details/86278994?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1.control

 

登录mysql服务器 

mysql -u root -p 

查看event是否开启 

show variables like '%sche%'; 

将事件计划开启 

set global event_scheduler =1;

创建存储过程test

CREATE PROCEDURE test () 
BEGIN 
update examinfo SET endtime = now() WHERE id = 14; 
END;

 

创建event e_test

create event if not exists e_test 
on schedule every 30 second 
on completion preserve 
do call test();

 

每隔30秒将执行存储过程test,将当前时间更新到examinfo表中id=14的记录的endtime字段中去.

 

 

关闭事件任务

 alter event e_test ON COMPLETION PRESERVE DISABLE; 

 

 

开启事件任务 

alter event e_test ON COMPLETION PRESERVE ENABLE;  

 

mysql 计划任务重启后消失

我们只要修改一配置即可

event_scheduler在mysql的config中设置为OFF有关。去mysql中将配置改为ON则就搞定了。

 

删除任务计划 

drop event if exists e_test;

 

每天定时执行任务,设置第一次执行时间为'2017-06-18 01:00:00',并且每天执行一次  

create event if not exists e_test 
on shcedule every 1 day starts '2017-06-18 01:00:00'
do call test(); 

 

 查看定时任务 

SHOW EVENTS 
select * from mysql.event

 

posted on 2021-09-05 09:41  四海骄阳  阅读(424)  评论(0编辑  收藏  举报

导航