zoukankan      html  css  js  c++  java
  • MySql定时器,亲测可用

    1. 查看数据库的event功能是否开启,在MySql中event默认是关闭的,需要查看并且要确保event处于开启状态

    sql:show VARIABLES LIKE '%sche%';

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

    sql:SET GLOBAL event_scheduler = 1;

    这时再输入

    sql:show VARIABLES LIKE '%sche%';

    event_scheduler就应该是ON或这是1了;即可以进行后面的操作。

    2. 创建测试表

    create table user1

    (

    id int(11) not null auto_increment primary key,

    username varcher(15) not null

    )

    3. 创建event要调用的存储过程user1_test

    create procedure user1_test()

    begin

    #为方便查看测试结果,推荐使用now()函数获取当前时间进行插入

    insert into user1(username ) values(now());

    end

    4. 创建事件user1_event

    create event user1_event
    #这句话是设置时间多长时间执行一次(本设置是1S一次)
    on schedule every 1 second
    on completion preserve disable
    #这个是指定要执行的代码块,在上面已经定义过了(即为3.创建的储存过程)
    do call user1_test();

    5. 开启事件user1_event,因为在创建之后是默认关闭的

    #开启

    alter event user1_event on completion preserve enable;

    #关闭(关闭时间自行控制,推荐开启约10S左右关闭,这样可以很直观的看到执行后的数据)

    alter event user1_event on completion preserve disable;

    6. 重头戏开始了,查看你的数据

    1 2019-06-10 10:35:56
    2 2019-06-10 10:35:57
    3 2019-06-10 10:35:58
    4 2019-06-10 10:35:59
    5 2019-06-10 10:36:00

    是不是多出了好多条数据,恭喜你成功了!

    参考:https://blog.csdn.net/ICanForYou/article/details/88957567

  • 相关阅读:
    李洪强经典面试题43
    李洪强经典面试题42
    李洪强经典面试题41-iOS选择题
    HTTP头部解析
    iOS
    内网安全工具之hscan扫描
    跟着百度学PHP[4]OOP面对对象编程-17-多态
    绕过注入学习笔记
    引用/别名
    跟着百度学PHP[4]OOP面对对象编程-16-switch逻辑就语句
  • 原文地址:https://www.cnblogs.com/CaptainFM/p/11014227.html
Copyright © 2011-2022 走看看