简体   繁体   English

如何在 MySQL 中调度存储过程

[英]How to schedule a stored procedure in MySQL

I have this stored procedure.我有这个存储过程。 How can I run this for example with intervals of 5 seconds?例如,我如何以 5 秒的间隔运行它? Like a routine for eliminate data with a time-stamp older than one day?就像消除时间戳超过一天的数据的例程一样?

DROP PROCEDURE IF EXISTS `delete_rows_links` 
GO

CREATE PROCEDURE delete_rows_links
BEGIN 

    DELETE activation_link
    FROM activation_link_password_reset
    WHERE  TIMESTAMPDIFF(DAY, `time`, NOW()) < 1 ; 

END 

GO

You can use mysql scheduler to run it each 5 seconds.您可以使用 mysql 调度程序每 5 秒运行一次。 You can find samples at http://dev.mysql.com/doc/refman/5.1/en/create-event.html您可以在http://dev.mysql.com/doc/refman/5.1/en/create-event.html找到样品

Never used it but I hope this would work:从未使用过它,但我希望这会起作用:

CREATE EVENT myevent
    ON SCHEDULE EVERY 5 SECOND
    DO
      CALL delete_rows_links();

I used this query and it worked for me:我使用了这个查询,它对我有用:

CREATE EVENT `exec`
  ON SCHEDULE EVERY 5 SECOND
  STARTS '2013-02-10 00:00:00'
  ENDS '2015-02-28 00:00:00'
  ON COMPLETION NOT PRESERVE ENABLE
DO 
  call delete_rows_links();

In order to create a cronjob, follow these steps:要创建 cronjob,请执行以下步骤:

  1. run this command: SET GLOBAL event_scheduler = ON;运行这个命令:SET GLOBAL event_scheduler = ON;

  2. If ERROR 1229 (HY000): Variable 'event_scheduler' is a GLOBAL variable and should be set with SET GLOBAL: mportant如果 ERROR 1229 (HY000): 变量 'event_scheduler' 是一个全局变量,应该使用 SET GLOBAL: 重要

It is possible to set the Event Scheduler to DISABLED only at server startup.只能在服务器启动时将事件调度程序设置为禁用。 If event_scheduler is ON or OFF, you cannot set it to DISABLED at runtime.如果 event_scheduler 为 ON 或 OFF,则不能在运行时将其设置为 DISABLED。 Also, if the Event Scheduler is set to DISABLED at startup, you cannot change the value of event_scheduler at runtime.此外,如果 Event Scheduler 在启动时设置为 DISABLED,则无法在运行时更改 event_scheduler 的值。

To disable the event scheduler, use one of the following two methods:要禁用事件调度程序,请使用以下两种方法之一:

  1. As a command-line option when starting the server:作为启动服务器时的命令行选项:

     --event-scheduler=DISABLED
  2. In the server configuration file (my.cnf, or my.ini on Windows systems): include the line where it will be read by the server (for example, in a [mysqld] section):在服务器配置文件(my.cnf,或 Windows 系统上的 my.ini)中:包括服务器将读取它的行(例如,在 [mysqld] 部分中):

     event_scheduler=DISABLED

    Read MySQL documentation for more information.阅读 MySQL 文档了解更多信息。

     DROP EVENT IF EXISTS EVENT_NAME; CREATE EVENT EVENT_NAME ON SCHEDULE EVERY 10 SECOND/minute/hour DO CALL PROCEDURE_NAME();

If you're open to out-of-the-DB solution: You could set up a cron job that runs a script that will itself call the procedure.如果您愿意接受数据库外解决方案:您可以设置一个 cron 作业来运行一个脚本,该脚本本身会调用该过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM