Navicat设置MySQL定时事件
参考: 1. http://blog.csdn.net/zzhongcy/article/details/41947927
2. http://www.mamicode.com/info-detail-1040270.html
3. http://www.cnblogs.com/chenpi/p/5137310.html
1. 创建事件
选择对应数据库->'事件'->'新建事件'
2. 定义事件
2.1 定义SQL语句: 以BEGIN开头,以END结尾
BEGIN
DELETE FROM goods WHERE endtime <= CURDATE();
END
2.2 定义者: root@localhost
如果是在本地运行,Definer为root@localhost
2.3 状态: ENABLE(激活)/DISABLE(不激活)
2.4 ON COMPLETION
正常情况下,一旦事件已过期,它将立即删除。你可以通过指定 ON COMPLETION PRESERVE 来重写此性能。使用 ON COMPLETION NOT PRESERVE 只是使默认非持久性的性能明确
3. 计划
3.1 AT: 用于一次性事件
3.2 EVERY: 在定期间隔重复的行动
4. 事件SQL
CREATE DEFINER=`root`@`localhost`
EVENT `NewEvent`
ON SCHEDULE EVERY 1 DAY STARTS '2017-05-28 00:00:00'
ON COMPLETION NOT PRESERVE
ENABLE
DO
BEGIN
DELETE FROM goods WHERE endtime <= CURDATE();
END;
5. 查看事件状态
查看事件是否开启: show variables like 'event_scheduler';
6. 开启/关闭事件语句
6.1 开启: set global event_scheduler = on;
7. 让MySQL服务启动时开启时间
只需要在my.ini配置文件的[mysqld]部分加上event_scheduler=ON 即可
8. 事件示例:
DROP EVENT IF EXISTS `event_minute`;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` EVENT `event_minute` ON SCHEDULE EVERY 1 MINUTE STARTS '2016-01-17 14:49:43' ON COMPLETION NOT PRESERVE ENABLE DO
BEGIN
INSERT INTO USER(name, address,addtime) VALUES('test1','test1',now());
INSERT INTO USER(name, address,addtime) VALUES('test2','test2',now());
END
;;
DELIMITER ;