【问题标题】:How to create an event that runs every 24 hours?如何创建每 24 小时运行一次的活动?
【发布时间】:2009-11-14 06:01:49
【问题描述】:

我需要每 24 小时运行一次:

delete tags from tags left join tagowners on tags.id=tagowners.tagId
where tagowners.tagId is null;

【问题讨论】:

    标签: mysql events


    【解决方案1】:
    CREATE EVENT cleartags
        ON SCHEDULE EVERY 24 HOUR
        DO 
          delete tags from tags left join tagowners on tags.id=tagowners.tagId
        where tagowners.tagId is null;
    

    【讨论】:

    【解决方案2】:

    首先在 SQl 查询浏览器中执行此操作

    设置全球 event_scheduler = ON;

    然后执行这个。它会在每 24 小时凌晨 12:00 触发一次

    创建事件事件 1 按计划每“1”天 开始'2013-01-21 00:00:00' 做 从 tags 中删除标签 left join tagowners on tags.id=tagowners.tagId 其中 tagowners.tagId 为空;

    【讨论】:

      【解决方案3】:

      cron:

      mysql -uUSER -pPWD -hDB-HOSTNAME/IPADDRESS -e "delete tags from tags left join tagowners on tags.id=tagowners.tagId where tagowners.tagId is null;"
      

      【讨论】:

        【解决方案4】:

        如果您使用的是 linux 服务器,您可以创建一个 cronjob,一个计划任务,通过 php 可执行文件执行 php 脚本。创建一个 cron 任务很容易,通过 shell 执行“crontab -e”,然后将你的命令添加到文件的底部。

        来自http://mkaz.com/ref/unix_cron.html 的示例 cron 条目

        #Run command at 7:00am each weekday [mon-fri]
        00 07 * * 1-5 mail_pager.script 'Wake Up'
        
        #Run command on 1st of each month, at 5:30pm
        30 17 1 * * pay_rent.script
        
        #Run command at 8:00am,10:00am and 2:00pm every day
        00 8,10,14 * * * do_something.script
        
        #Run command every 5 minutes during market hours
        */5 6-13 * * mon-fri get_stock_quote.script
        
        #Run command every 3-hours while awake
        0 7-23/3 * * * drink_water.script 
        

        如果你想每天执行一次 php 脚本...

        0 0 * * * /path/to/php.exe myscript.php
        

        请记住,您是通过 CLI 执行脚本,因此 $_GET/$_POST/$_SERVER 超级全局变量将不存在(您可以使用 wget 解决此问题)。

        如果您在 Windows 上,您可以使用 Windows 任务计划程序来完成相同的操作。

        【讨论】:

        • 在这些 cron 条目的每行前面放置四个空格。这样代码格式就会接管,你的答案看起来并不像废话。
        猜你喜欢
        • 1970-01-01
        • 2020-09-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-02
        • 2020-06-15
        相关资源
        最近更新 更多