event.sql 2 KB
-- 连接mysql
mysql -h 192.168.102.168 -u root -proot webapm

--创建存储过程
DELIMITER $$
CREATE PROCEDURE `p_del_slow`(IN `date_inter` INT)
BEGIN
    DELETE FROM slow_duration WHERE (TO_DAYS(NOW()) - TO_DAYS(FROM_UNIXTIME(create_time/1000)))>=date_inter;
END$$
DELIMITER ;

DELIMITER $$
CREATE PROCEDURE `p_del_error`(IN `date_inter` INT)
BEGIN
    DELETE FROM error_report WHERE (TO_DAYS(NOW()) - TO_DAYS(FROM_UNIXTIME(create_time/1000)))>=date_inter;
END$$
DELIMITER ;

-- test
DELIMITER $$
CREATE PROCEDURE `i_sert`(IN `date_inter` INT)
BEGIN
    INSERT INTO slow_duration(route) values('huangtao');
END$$
DELIMITER ;

-- 删除存储过程
drop procedure if exists p_del_error;

-- 列出所有存储过程
select specific_name from mysql.proc;

-- 查看某一个存储过程的具体内容
select body from mysql.proc where specific_name = 'p_del_slow';

-- 调用存储过程
call p_del_slow();

-- 查看定时配置开启
show variables like '%event_sche%';
set global event_scheduler=1;

-- 创建定时任务
CREATE EVENT `e_del_slow_route`
ON SCHEDULE EVERY 1 DAY STARTS '2017-12-7 0:0:0'
ON COMPLETION PRESERVE DISABLE
DO CALL p_del_slow (7);

CREATE EVENT `e_del_error_report`
ON SCHEDULE EVERY 1 DAY STARTS '2017-12-7 0:0:0'
ON COMPLETION PRESERVE DISABLE
DO CALL p_del_error (7);

-- test
CREATE EVENT `i_second`
ON SCHEDULE EVERY 1 SECOND
ON COMPLETION PRESERVE DISABLE
DO CALL i_sert (7);

-- 数目
select count(*) from slow_duration;

--查看本机所有的事件
SELECT event_name,event_definition,interval_value,interval_field,status FROM information_schema.EVENTS;

--开启定时任务
alter event e_del_slow_route on completion preserve enable;
alter event e_del_error_report on completion preserve enable;

--关闭定时任务
alter event e_del_slow_route on completion preserve disable;
alter event e_del_error_report on completion preserve disable;

--删除存储过程
drop event if exists e_del_slow_route;
drop event if exists e_del_error_report;

-- http://jiyiren.github.io/2016/03/27/Mysql_schedule/