MySQL 添加定时执行任务

参考自:https://www.cnblogs.com/laowu-blog/p/5073665.html

命令说明:

delimiter //        将SQL语句块的终结符号设置成 //  因为在创建执行函数的时候回用到默认的 ; 事后需要改回来
mysql> create procedure myfunc()
    -> begin
    -> delete from information where id > 2;
    -> end//

创建过程,begin 和 end 之间就是该过程的内容

mysql> create event del_info
    -> on schedule every 600 second starts timestamp ‘2020-05-14 18:28:00‘
    -> on completion preserve
    -> do
    -> begin
    -> call myfunc();
    -> end//

创建定时任务 del_info:

每600秒执行一次,起始时间点为  2020-05-14 18:28:00

完成后,该任务保留, 不保留可以使用  on completion not preserve

执行

begin和end之间为执行的内容

call myfunc()    执行上面定义的过程

最后需要把 SQL语句终结符号改回来

delimiter ;

然后使用 show events;命令,就能查看到刚刚添加定时任务

 删除过程和事件(任务)

mysql> drop event del_info;
Query OK, 0 rows affected (0.00 sec)

mysql> show events;
Empty set (0.00 sec)

mysql> drop procedure myfunc;
Query OK, 0 rows affected (0.00 sec)

相关推荐