我必须每天在某个时刻从我的数据库中清空一个表(比如00:00:00)。我在程序中有这个:
CREATE DEFINER=`root`@`%` PROCEDURE `delete_day`()
BEGIN
TRUNCATE TABLE qmsos.module_queuemanagement_queue;
END
我尝试制作一个每天调用并重复此事件的事件:
CREATE
EVENT `delete_queue_daily`
ON SCHEDULE EVERY 1 DAY STARTS '2017-05-12 00:00:00'
ON COMPLETION PRESERVE
DO CALL delete_day();
但它不起作用。我尝试在特定的时间执行一次只有一次,但是如果我想每天都运行它,那就不行了。
答案 0 :(得分:1)
您的活动定义看起来不错。请执行以下命令验证event_scheduler是否已启用:
SHOW VARIABLES LIKE 'event_scheduler';
如果它关闭,那么:
SET GLOBAL event_scheduler = ON;
全局event_scheduler系统变量确定是否在服务器上启用并运行事件调度程序。它具有这3个值中的一个,它们会影响事件调度,如下所述:
OFF:事件计划程序已停止。事件调度程序线程确实如此 没有运行,没有显示在SHOW PROCESSLIST的输出中,没有 预定的事件被执行。 OFF是默认值 event_scheduler。
当事件调度程序停止时(event_scheduler为OFF),它可以 通过将event_scheduler的值设置为ON来启动。 (见下 项)。
ON:启动事件调度程序;事件调度程序线程运行并执行所有计划的事件。
当事件调度程序为ON时,将列出事件调度程序线程 在SHOW PROCESSLIST的输出中作为守护进程
禁用:此值使事件计划程序无法运行。当事件调度程序为DISABLED时,事件调度程序线程不会运行(因此不会出现在SHOW PROCESSLIST的输出中)。此外,无法在运行时更改事件调度程序状态。
<强>更新强>
要调试,请尝试简化表达式,并通过执行以下命令检查调度程序线程是否正在运行:
SHOW FULL PROCESSLIST;
对于ie:
CREATE
EVENT `delete_queue_daily_debug`
ON SCHEDULE EVERY 1 DAY
STARTS TIMESTAMP(NOW() + INTERVAL 1 MINUTE)
DO CALL delete_day();
答案 1 :(得分:1)
我无法重现这个问题。
在基本测试中,存储过程每10秒执行一次:
mysql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 5.7.18 |
+-----------+
1 row in set (0.00 sec)
mysql> SET @@GLOBAL.event_scheduler = ON;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW VARIABLES LIKE 'event_scheduler';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| event_scheduler | ON |
+-----------------+-------+
1 row in set (0.00 sec)
mysql> USE `qmsos`;
Database changed
mysql> DROP EVENT IF EXISTS `delete_queue_daily`;
Query OK, 0 rows affected (0.00 sec)
mysql> DROP PROCEDURE IF EXISTS `delete_day`;
Query OK, 0 rows affected (0.00 sec)
mysql> DROP TABLE IF EXISTS `module_queuemanagement_queue`,
-> `audit_log`;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE IF NOT EXISTS `module_queuemanagement_queue` (
-> `id` BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY
-> );
Query OK, 0 rows affected (0.01 sec)
mysql> INSERT INTO `module_queuemanagement_queue`
-> (`id`)
-> VALUES
-> (NULL), (NULL), (NULL), (NULL), (NULL);
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> CREATE TABLE IF NOT EXISTS `audit_log` (
-> `id` BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
-> `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
-> );
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE EVENT `delete_queue_daily`
-> ON SCHEDULE EVERY 10 SECOND STARTS '2017-05-12 00:00:00'
-> ON COMPLETION PRESERVE
-> DO CALL `delete_day`;
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER //
mysql> CREATE PROCEDURE `delete_day`()
-> BEGIN
-> INSERT INTO `audit_log` (`id`) VALUES (NULL);
-> TRUNCATE TABLE `module_queuemanagement_queue`;
-> END//
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER ;
mysql> SHOW EVENTS\G
*************************** 1. row ***************************
Db: qmsos
Name: delete_queue_daily
Definer: user@127.0.1.1
Time zone: SYSTEM
Type: RECURRING
Execute at: NULL
Interval value: 10
Interval field: SECOND
Starts: 2017-05-12 00:00:00
Ends: NULL
Status: ENABLED
Originator: 0
character_set_client: utf8mb4
collation_connection: utf8mb4_general_ci
Database Collation: utf8mb4_general_ci
1 row in set (0.00 sec)
mysql> SELECT `id`, `created_at`
-> FROM `audit_log`;
Empty set (0.00 sec)
mysql> SELECT `id`
-> FROM `module_queuemanagement_queue`;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
+----+
5 rows in set (0.00 sec)
mysql> SELECT `id`, `created_at` FROM `audit_log`;
+----+---------------------+
| id | created_at |
+----+---------------------+
| 1 | 2017-05-12 00:00:10 |
+----+---------------------+
1 row in set (0.00 sec)
mysql> SELECT `id` FROM `module_queuemanagement_queue`;
Empty set (0.00 sec)