在mysql中设置event_scheduler

时间:2015-05-06 16:10:47

标签: java mysql sql jdbc

我正在尝试将事件设置为on用于总线表,但它不起作用。正在创建表和事件,但事件未被触发。我已经在mysql- my.ini中设置了这一行event_scheduler = ON;并重启服务器。

    // Create bus table
    stt.execute("CREATE TABLE IF NOT EXISTS bus"
            + "(id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,"
            + "mac VARCHAR(30) NOT NULL UNIQUE,"
            + "route int(11) NOT NULL,"
            + "latitude FLOAT(10,6) NOT NULL,"
            + "longitude FLOAT(10,6) NOT NULL,"
            + "created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)");

    stt.execute("CREATE EVENT IF NOT EXISTS  AutoDelete "
            + "ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 3 MINUTE "
            + "DO "
            + "DELETE FROM bus WHERE created_at < (NOW() - INTERVAL 3 MINUTE)");
    stt.execute("SET GLOBAL event_scheduler = ON");

1 个答案:

答案 0 :(得分:1)

引用reference manual

  

要定期重复操作,请使用EVERY子句。 [...]例如,EVERY 6 WEEK表示“每六周一次”。

在您的情况下,如果您想每三分钟重复此操作,您的活动应按以下方式创建:

CREATE EVENT IF NOT EXISTS  AutoDelete 
ON SCHEDULE EVERY 3 MINUTE
-- OPTIONAL: STARTS CURRENT_TIMESTAMP + INTERVAL 1 HOUR
-- OPTIONAL: ENDS CURRENT_TIMESTAMP + INTERVAL 1 MONTH
DO 
    DELETE FROM bus WHERE created_at < (NOW() - INTERVAL 3 MINUTE)

要检查事件调度程序是否正在运行,您可以使用以下选项之一:

<强> 1。 SHOW PROCESSLIST

show processlist;
+-----+-----------------+-----------+------+---------+------+------------------------+------------------+
| Id  | User            | Host      | db   | Command | Time | State                  | Info             |
+-----+-----------------+-----------+------+---------+------+------------------------+------------------+
| 243 | root            | localhost | NULL | Query   |    0 | NULL                   | show processlist |
| 244 | event_scheduler | localhost | NULL | Daemon  |    6 | Waiting on empty queue | NULL             |
+-----+-----------------+-----------+------+---------+------+------------------------+------------------+

请注意,事件调度程序是&#34; DAEMON&#34;过程

<强> 2。 SHOW VARIABLES

show global variables like 'event_scheduler';
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| event_scheduler | ON    |
+-----------------+-------+