`

Mysql 定时器

阅读更多
这篇文章主要教大家怎样写定时器,在Mysql中,有几点是需要注意的。

1. 确定是打开了定时器
 用这个sql可以查询,我自己在本地上第一次查询是OFF

 show VARIABLES LIKE 'event%';
 如果是OFF,则用下面的命令进行设置

 SET GLOBAL event_scheduler = 1;


2. 定时器是需要调用存储过程的

drop table if exists test;
create table test
(
id int(11) not null auto_increment primary key,
time datetime not null
) engine=innodb default charset=utf8;

delimiter //
drop procedure if exists test_proce//
create procedure test_proce()
begin
insert into test(time) values(now());
end//
delimiter ;


上面的delimiter是分隔符,Mysql默认的结束符是;,如果我们希望以一个整体来执行,就得用到delimiter了。

3. 创建定时器
drop event if exists test_event;
create event test_event
on schedule every [b]1 second[/b]
on completion preserve [b]enable[/b]
do call test_proce();


如果不写enable,就不会写到数据库里去,还有周期是可以自己定义的,这里为了测试,就写成1秒钟执行一次。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics