MySQL定時(shí)任務(wù)EVENT事件的使用方法
一、查看定時(shí)策略是否開(kāi)啟
show variables like '%event_scheduler%';
* on這里是啟用的
查看進(jìn)程:會(huì)看到一個(gè)用戶為event_scheduler,執(zhí)行狀態(tài)為Waiting on empty queue的進(jìn)程
show PROCESSLIST;
開(kāi)啟定時(shí)策略:
set global event_scheduler = 1;
關(guān)閉定時(shí)策略:
set global event_scheduler = 0;
在my.cnf配置文件中開(kāi)啟事件(永久開(kāi)啟,設(shè)置后需要重啟MySQL才會(huì)生效)
配置文件的[mysqld]部分加上event_scheduler=ON
二、創(chuàng)建定時(shí)任務(wù)
-- 創(chuàng)建定時(shí)任務(wù) create EVENT sync_lastday_attendance ON SCHEDULE EVERY '1' DAY STARTS '2021-09-19 05:00:00' DO call sync_lastday_attendance()
解釋:
1、create event sync_lastday_attendance:是創(chuàng)建名為sync_lastday_attendance的事件
2、EVERY '1' DAY 創(chuàng)建周期定時(shí)的規(guī)則,意思是每天執(zhí)行一次
3、STARTS '2021-09-19 05:00:00'表示在具體某個(gè)時(shí)間執(zhí)行,是2021-09-19凌晨5點(diǎn)整開(kāi)始執(zhí)行
3、可選 on completion preserve disable是表示創(chuàng)建后并不開(kāi)始生效。
4、do call sync_lastday_attendance()是該event(事件)的操作內(nèi)容,這里是調(diào)用名為sync_lastday_attendance()的存儲(chǔ)過(guò)程
三、定時(shí)任務(wù)操作
查看所有定時(shí)任務(wù)
SELECT event_name,event_definition,interval_value,interval_field,status FROM information_schema.EVENTS;
查看指定定時(shí)任務(wù)創(chuàng)建詳細(xì)
show create EVENT sync_lastday_attendance;
修改任務(wù)
-- 創(chuàng)建定時(shí)任務(wù) 8點(diǎn)執(zhí)行 調(diào)用test()存儲(chǔ)過(guò)程 ALTER EVENT sync_lastday_attendance ON SCHEDULE EVERY '1' DAY STARTS '2021-09-19 08:00:00' DO call test()
開(kāi)啟定時(shí)任務(wù)
alter event sync_lastday_attendance on completion preserve enable;//開(kāi)啟定時(shí)任務(wù)
關(guān)閉定時(shí)任務(wù)
alter event sync_lastday_attendance on completion preserve disable;//關(guān)閉定時(shí)任務(wù)
刪除定時(shí)任務(wù)
drop event sync_lastday_attendance; //刪除定時(shí)任務(wù)
四、定時(shí)規(guī)則
1、周期執(zhí)行(EVERY)
參數(shù)單位有:second、minute、hour、day、week(周)、quarter(季度)、month、year
on schedule every 1 week //每周執(zhí)行1次 on schedule every 1 day //每天執(zhí)行1次
2、在具體某個(gè)時(shí)間執(zhí)行(AT)
on schedule at current_timestamp()+interval 5 day //5天后執(zhí)行 on schedule at '2021-09-19 05:00:00' //在2021年9月19日,5點(diǎn)整執(zhí)行
3、在某個(gè)時(shí)間段執(zhí)行(STARTS ENDS)
on schedule every 1 day starts current_timestamp()+interval 3 day ends current_timestamp()+interval 1 month //3天后開(kāi)始每天都執(zhí)行一次到下個(gè)月底結(jié)束 on schedule every 1 day ends current_timestamp()+interval 3 day //從現(xiàn)在起每天執(zhí)行,執(zhí)行3天
到此這篇關(guān)于MySQL定時(shí)任務(wù)EVENT事件的使用方法的文章就介紹到這了,更多相關(guān)MySQL定時(shí)任務(wù)EVENT事件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- MySQL 使用事件(Events)完成計(jì)劃任務(wù)
- MySQL定時(shí)任務(wù)(EVENT事件)如何配置詳解
- mysql定時(shí)任務(wù)(event事件)實(shí)現(xiàn)詳解
- MySQL數(shù)據(jù)庫(kù)Event定時(shí)執(zhí)行任務(wù)詳解
- 淺談MySQL event 計(jì)劃任務(wù)
- 詳解MySQL用事件調(diào)度器Event Scheduler創(chuàng)建定時(shí)任務(wù)
- MySQL定時(shí)器EVENT學(xué)習(xí)筆記
- mysql中event的用法詳解
- MySQL查看event執(zhí)行記錄的實(shí)現(xiàn)
相關(guān)文章
關(guān)于Mysql查詢帶單引號(hào)及插入帶單引號(hào)字符串問(wèn)題
本文主要介紹的是用mysql_real_escape_string對(duì)用戶提交的表單數(shù)據(jù)進(jìn)行轉(zhuǎn)義處理和通過(guò)addslashes以及mysql_escape_string這3個(gè)類似功能的函數(shù)用法區(qū)別2013-04-04mysql 獲取當(dāng)前日期函數(shù)及時(shí)間格式化參數(shù)詳解
這篇文章主要介紹了mysql 獲取當(dāng)前日期函數(shù)now()及時(shí)間格式化DATE_FROMAT函數(shù)以及參數(shù)詳細(xì)介紹,需要的朋友可以參考下2014-08-08