mysql如何创建一个定时事件

来源:互联网 发布:喝一次酒影响精子,知乎 编辑:程序博客网 时间:2024/05/02 04:50
mysql要实现定时执行sql语句就要用到Event具体操作如下:

先看看看event 事件是否开启show variables like '%sche%';如没开启,则开启。

需要数据库超级权限set global event_scheduler =1;

创建存储过程 update_a (注:********就是你要执行的sql语句)
create procedure update_a() ********;

创建一个定时任务:event updates

mysql> create event if not exists updates on schedule every60 second on completion preserve do call update_a();

(创建这个定时任务(如果存在的话)每隔60秒调用一次update_a()这个存储过程)

drop procedure update_a;
(关闭存储过程)

 ---执行update_a()存储过程创建Event之后,sql语句就定时执行一次。

关闭事件任务alter event updates ON -> COMPLETION PRESERVEDISABLE;

开启事件任务alter event updates ON -> COMPLETION PRESERVEENABLE;


简单实例:

创建表 CREATE TABLE test(endtime DATETIME);

创建存储过程  test CREATE PROCEDURE test() updateexaminfo SET endtime = now() WHERE id = 1;

创建event e_test CREATE EVENT if not exists e_test on scheduleevery 30 second on completion preserve do call test();

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

create table cdat(id INT(20) not null AUTO_INCREMENT,primarykey (id));
0 0
原创粉丝点击