• mysql定时器


    #查看数据库的event功能是否开启 因为在数据库中的event默认是关闭的

    show VARIABLES LIKE '%sche%';

    #如果value显示为off或者0说明是关闭的,这时我们需要手动打开定时器

    SET GLOBAL event_scheduler = 1;

    #创建测试表

    create table test
    (
    id int(11) not null auto_increment primary key,
    time datetime not null
    ) engine=innodb default charset=utf8;

    #这是判断我们要执行的文本是否存在,如果存在,就删除这个文本(本质就是我们的定时器定时执行的 代码块)

    delimiter //
    drop procedure if exists test_proce//
    #创建event要调用的存储过程test_proce (其实就是创建文本/代码块。)
    create procedure test_proce()
    begin
    #向test表里面添加当前时间(代码块中的执行命令 我选择的是一个添加,因为容易看到效果)
    insert into test(time) values(now());
    end//
    delimiter ;

    #创建事件test_event(其作用:每隔一秒自动调用test_proce()存储过程)

    create event test_event
    #这句话是设置时间多长时间执行一次
    on schedule every 1 second
    on completion preserve disable
    #这个是指定要执行的代码块,在上面已经定义过了
    do call test_proce();

    #2020-01-10 11:10:00启动定时器,每隔12小时执行一次

    create event test_event2
    on schedule every 12 hour starts timestamp '2020-01-10 11:10:00' 
    on completion preserve disable
    do call test_proce();

    #开启事件test_event 因为创建的事件的启用属性默认是关闭的,我们将他的属性设置为开启
    #就可以使用当前定时器 test_event 是要执行的事件名字

    alter event test_event on completion preserve enable;

    #关闭事件 test_event 是要关闭的事件名字

    alter event test_event on completion preserve disable;

    整体流程简介:


      1.开启mysql数据库的event功能。
      2.创建一个类似于要执行的代码块。
      3.创建一个事件,这个事件中有诸多属性,可以设置执行时间间隔以及指定执行的代码块。
      4.因为创建的事件的执行属性默认是关闭的,所以我们要去修改这个事件的属性为开启。

  • 相关阅读:
    LeetCode第[66]题(Java):Plus One
    LeetCode第[62]题(Java):Unique Paths 及扩展
    localhost不能访问127.0.0.1可以访问的原因以及解决办法
    LeetCode第[56]题(Java):Merge Intervals
    LeetCode第[55]题(Java):Jump Game
    LeetCode第[54]题(Java):Spiral Matrix
    LeetCode第[53]题(Java):Maximum Subarray
    LeetCode第[50]题(Java):Pow(x, n)
    LeetCode第[49]题(Java):Group Anagrams
    Keras 资源
  • 原文地址:https://www.cnblogs.com/rong0912/p/12175466.html
Copyright © 2020-2023  润新知