mysql 建立定時器

來源:互聯網
上載者:User

標籤:

mysql定時器是系統給提供了event,而oracle裡面的定時器是系統給提供的job。廢話少說,下面建立表:create table mytable (id int auto_increment not null,name varchar(100) not null default ‘‘,introduce text not null,createtime timestamp not null,constraint pk_mytable primary key(id))建立預存程序,這裡的預存程序主要提供給mysql的定時器event來調用去執行:create procedure mypro()BEGINinsert into mytable (name,introduce,createtime) values (‘1111‘,‘inner mongolia‘,now());end;這裡只是簡單的寫了一下,只是為了說明例子。緊接著建立mysql的定時器event:create event if not exists eventJob on schedule every 1 second on completion PRESERVEdo call mypro();這裡設定為每一秒執行一次至此所有的準備工作已經寫完了,做完這些,mysql要想利用定時器必須的做準備工作,就是把mysql的定時器給開啟了:SET GLOBAL event_scheduler = 1;  -- 啟動定時器SET GLOBAL event_scheduler = 0;  -- 停止定時器緊接著還要開啟事件:ALTER EVENT eventJob ON  COMPLETION PRESERVE ENABLE;   -- 開啟事件ALTER EVENT eventJob ON  COMPLETION PRESERVE DISABLE;  -- 關閉事件SHOW VARIABLES LIKE ‘%sche%‘; -- 查看定時器狀態至此,你去資料庫裡面的表mytable裡面看下,系統會每隔一秒去插入一條資料,嘻嘻,任務完成了。select * from mytable

 

mysql 建立定時器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.