---
title: "mysql定时器"
author: "thomas0988"
date: "2015-08-04T14:01:16Z"
tags: []
categories: ["数据库"]
slug: "mysql"
draft: false
lastmod: "2015-08-04T14:01:16Z"
id: 2232722
url: "https://thomas0988.iteye.com/blog/2232722"
---

5.1以后可以使用

 

ALTER EVENT \`testEvent\` ON SCHEDULE EVERY 10 SECOND STARTS now() ON COMPLETION PRESERVE ENABLE DO   
 -- 现在执行每隔10秒执行一次  
         INSERT INTO ad\_db.fh\_test\_timer ( VALUE) VALUES (now());     -- 循环要干的事情 可以调用其他存储过程 如：call test();

---------------------------------------------------完成-------------------------------------

 定时器写完了 想要执行它 需要 运行

SET GLOBAL event\_scheduler = 1;  -- 启动定时器

和

ALTER EVENT testEvent ON  COMPLETION PRESERVE ENABLE;   -- 开启事件

执行完这2条SQL后定时器就开始执行了。

\----------------------------------------------------------------------------------------------

其他命令：

/*  
SHOW VARIABLES LIKE '%sche%'; -- 查看定时器状态

SET GLOBAL event\_scheduler = 1;  -- 启动定时器

SET GLOBAL event\_scheduler = 0;  -- 停止定时器

ALTER EVENT testEvent ON  COMPLETION PRESERVE ENABLE;   -- 开启事件

ALTER EVENT testEvent ON  COMPLETION PRESERVE DISABLE;  -- 关闭事件  
\*/

==============================================================

[http://sunjia-704471770-qq-com.iteye.com/blog/1700837](http://sunjia-704471770-qq-com.iteye.com/blog/1700837)