Run a mySQL query as a cron job?

后端 未结 4 1934
伪装坚强ぢ
伪装坚强ぢ 2020-11-27 11:12

I would like to purge my SQL database from all entires older than 1 week, and I\'d like to do it nightly. So, I\'m going to set up a cron job. How do I query mySQL without h

4条回答
  •  清酒与你
    2020-11-27 11:26

    I personally find it easier use MySQL event scheduler than cron.

    Enable it with

    SET GLOBAL event_scheduler = ON;
    

    and create an event like this:

    CREATE EVENT name_of_event
    ON SCHEDULE EVERY 1 DAY
    STARTS '2014-01-18 00:00:00'
    DO
    DELETE FROM tbl_message WHERE DATEDIFF( NOW( ) ,  timestamp ) >=7;
    

    and that's it.

    Read more about the syntax here and here is more general information about it.

提交回复
热议问题