首先确定event功能是否打开.
SELECT @@event_scheduler;
默认情况下是OFF,这时候可以将变量设置为ON
SET GLOBAL event_scheduler = ON;
这时event可以正常运行,但是如果重启了mysql,你会发现变量重新变成了OFF.
如果希望重启后,变量默认状态为ON,则需要对configure文件进行设置.
将/usr/share/mysql/下的一个cnf文件拷贝至/etc/,命名为my.cnf(查找cnf文件使用命令find -iname *.cnf)
打开文件,在[mysqld]后增加event_scheduler = ON.
还有一种办法是写一个mysql启动脚本 mysql restart --event_scheduler = ON;
附:使用rpm在suse安装mysql
mysql启动脚本位置为/etc/init.d/mysql
工具位于/usr/bin下
数据文件位于/var/lib/mysql下,按数据库名称分目录存储
CREATE TABLE t_test_event
(id INT PRIMARY KEY AUTO_INCREMENT,
TIME TIMESTAMP DEFAULT CURRENT_TIMESTAMP)
AUTO_INCREMENT=1;
DELIMITER $$
-- SET GLOBAL event_scheduler = ON$$ -- required for event to execute but not create
CREATE /*[DEFINER = { user | CURRENT_USER }]*/ EVENT `virus`.`ev_get_net_virus`
ON SCHEDULE
/* uncomment the example below you want to use */
-- scheduleexample 1: run once
-- AT 'YYYY-MM-DD HH:MM.SS'/CURRENT_TIMESTAMP { + INTERVAL 1 [HOUR|MONTH|WEEK|DAY|MINUTE|...] }
-- scheduleexample 2: run at intervals forever after creation
-- EVERY 1 [HOUR|MONTH|WEEK|DAY|MINUTE|...]
-- scheduleexample 3: specified start time, end time and interval for execution
/*EVERY 1 [HOUR|MONTH|WEEK|DAY|MINUTE|...]
STARTS CURRENT_TIMESTAMP/'YYYY-MM-DD HH:MM.SS' { + INTERVAL 1[HOUR|MONTH|WEEK|DAY|MINUTE|...] }
ENDS CURRENT_TIMESTAMP/'YYYY-MM-DD HH:MM.SS' { + INTERVAL 1 [HOUR|MONTH|WEEK|DAY|MINUTE|...] } */
/*[ON COMPLETION [NOT] PRESERVE]
[ENABLE | DISABLE]
[COMMENT 'comment']*/
EVERY 1 MINUTE STARTS CURRENT_TIMESTAMP+INTERVAL 1 MINUTE
DO
BEGIN
INSERT INTO virus.`t_test_event` VALUES(NULL,NULL);
END$$
DELIMITER ;