博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
22.10. 事件调度器(EVENT)
阅读量:6002 次
发布时间:2019-06-20

本文共 4901 字,大约阅读时间需要 16 分钟。

22.10.1. 启用 EVENT

set GLOBAL event_scheduler=ON;

my.cnf 配置

event_scheduler=on

查看状态

mysql> select @@GLOBAL.event_scheduler;+--------------------------+| @@GLOBAL.event_scheduler |+--------------------------+| ON                       |+--------------------------+1 row in set (0.00 sec)mysql> SHOW VARIABLES LIKE 'event_scheduler';+-----------------+-------+| Variable_name   | Value |+-----------------+-------+| event_scheduler | ON    |+-----------------+-------+1 row in set (0.01 sec)

22.10.2. 创建 EVENT

DROP EVENT IF EXISTS `captcha`;DELIMITER //CREATE DEFINER=`neo`@`%` EVENT `captcha` ON SCHEDULE EVERY 5 MINUTE STARTS '2013-07-08 16:27:03' ON COMPLETION PRESERVE ENABLE DO BEGIN	delete from captcha where ctime < DATE_ADD(now(), INTERVAL -5 MINUTE);END//DELIMITER ;

22.10.3. 禁用/启用

ALTER EVENT captcha DISABLE;
ALTER EVENT captcha ENABLE;

22.10.4. show events

mysql> show events;+--------+-------------+---------+-----------+-----------+------------+----------------+----------------+---------------------+------+---------+------------+----------------------+----------------------+--------------------+| Db     | Name        | Definer | Time zone | Type      | Execute at | Interval value | Interval field | Starts              | Ends | Status  | Originator | character_set_client | collation_connection | Database Collation |+--------+-------------+---------+-----------+-----------+------------+----------------+----------------+---------------------+------+---------+------------+----------------------+----------------------+--------------------+| netkiller | captcha     | neo@%   | SYSTEM    | RECURRING | NULL       | 5              | MINUTE         | 2013-07-08 16:27:03 | NULL | ENABLED |          1 | utf8                 | utf8_general_ci      | utf8_general_ci    || netkiller | sms_ips_log | neo@%   | SYSTEM    | RECURRING | NULL       | '0 5'          | DAY_HOUR       | 2013-07-09 14:39:51 | NULL | ENABLED |          1 | utf8                 | utf8_general_ci      | utf8_general_ci    |+--------+-------------+---------+-----------+-----------+------------+----------------+----------------+---------------------+------+---------+------------+----------------------+----------------------+--------------------+2 rows in set (0.00 sec)mysql> show events \G;*************************** 1. row ***************************                  Db: netkiller                Name: captcha             Definer: neo@%           Time zone: SYSTEM                Type: RECURRING          Execute at: NULL      Interval value: 5      Interval field: MINUTE              Starts: 2013-07-08 16:27:03                Ends: NULL              Status: ENABLED          Originator: 1character_set_client: utf8collation_connection: utf8_general_ci  Database Collation: utf8_general_ci*************************** 2. row ***************************                  Db: netkiller                Name: sms_ips_log             Definer: neo@%           Time zone: SYSTEM                Type: RECURRING          Execute at: NULL      Interval value: '0 5'      Interval field: DAY_HOUR              Starts: 2013-07-09 14:39:51                Ends: NULL              Status: ENABLED          Originator: 1character_set_client: utf8collation_connection: utf8_general_ci  Database Collation: utf8_general_ci2 rows in set (0.00 sec)ERROR:No query specified

22.10.5. 实例·每月创建一个表

每月创建一张新表,适用于分表的场景

CREATE DEFINER=`neo`@`netkiller` EVENT `logging`	ON SCHEDULE		EVERY 1 MONTH STARTS '2017-12-11 15:51:00'	ON COMPLETION PRESERVE	ENABLE	COMMENT '每月自动创建表'DO BEGIN	declare _table_date varchar(10);	select date_format(date_add(curdate(),interval 1 month),'%Y%m') into _table_date;	call logging(_table_date);END
CREATE DEFINER=`neo`@`netkiller` PROCEDURE `logging`(	IN `table_date` VARCHAR(10))LANGUAGE SQLNOT DETERMINISTICCONTAINS SQLSQL SECURITY DEFINERCOMMENT ''BEGIN	set @_table_name = CONCAT('log_',table_date);	set @_create = "CREATE TABLE If Not Exists ";	set @_param = "(			`id` INT(11) NOT NULL AUTO_INCREMENT,			`type` VARCHAR(255) NULL DEFAULT NULL COMMENT '日志类型 1:网站 2:IOS 3:Android',			`url` VARCHAR(640) NULL DEFAULT NULL COMMENT '用户访问url',			`serverIp` VARCHAR(255) NULL DEFAULT NULL COMMENT '服务器ip',			`bodyBytesSent` VARCHAR(255) NULL DEFAULT NULL,			`bytesSent` VARCHAR(255) NULL DEFAULT NULL COMMENT '参数字节数',			`browser` VARCHAR(255) NULL DEFAULT NULL COMMENT '浏览器信息',			`ctime` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,			`mtime` TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,			PRIMARY KEY (`id`),			INDEX `ctime` (`ctime`, `deviceType`,`isFirst`),			INDEX `userIp` (`userIp`),			INDEX `deviceId` (`deviceId`),			INDEX `account` (`account`)		)		COMMENT='APP 访问记录'		COLLATE='utf8_general_ci'		ENGINE=InnoDB		;";		SET @sql = CONCAT(@_create,@_table_name,@_param);	PREPARE stmt FROM @sql;	EXECUTE stmt;	Deallocate prepare stmt;END

原文出处:Netkiller 系列 手札
本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。

你可能感兴趣的文章
git设置socks代理
查看>>
桶排序
查看>>
石化数字化交付
查看>>
ACER一键恢复系统联想到的备份还原方案
查看>>
如何用windows Live writer 撰写blog
查看>>
RHEL6入门系列之十九,硬盘分区与格式化
查看>>
Ajax在java前台中怎么运用
查看>>
Linux下升级 OpenSSH
查看>>
标准功能模块组件 -- 名片管理组件,C\S 版本的标准用例程序,可以参考权限实现方法...
查看>>
zygote进程图
查看>>
webkit支持跨域的方法
查看>>
ldap快速配置
查看>>
docker之docker-machine用法
查看>>
IIS 7启用static JSON文件能POST方法
查看>>
P5205 【模板】多项式开根
查看>>
微博mini for Windows Phone 8 开发那些事
查看>>
redis文章索引
查看>>
OpenSSH利用处理畸形长度密码造成的时间差,枚举系统用户(CVE-2016-6210)
查看>>
Javascript回调函数
查看>>
可能是最简单的面向对象入门教程(二)为什么要有类型
查看>>