zoukankan      html  css  js  c++  java
  • 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.因为创建的事件的执行属性默认是关闭的,所以我们要去修改这个事件的属性为开启。

  • 相关阅读:
    [WPF]WPF3.5 SP1 TreeView的Bug
    2010年自我总结
    LINQ to SQL 异常:SQL Server does not handle comparison of NText, Text, Xml, or Image data types
    [翻译] javascript缩进
    【转】疑难解答:调试 Windows 服务
    【翻译】MSDN文档对类DefaultValueAttribute的描述有些混淆
    二叉排序树——算法系列
    索引查找——算法系列
    android软电话开发预备知识
    adb server is out of date.killing
  • 原文地址:https://www.cnblogs.com/rong0912/p/12175466.html
Copyright © 2011-2022 走看看