MySQL資料庫 Event 定時執行任務

2022-01-31 12:40:11 字數 2949 閱讀 7045

由於專案的業務是不斷往前跑的,所以難免資料庫的表的量會越來越龐大,不斷的擠占硬碟空間。即使再大的空間也支撐不起業務的增長,所以定期刪除不必要的資料是很有必要的。在我們專案中由於不清理資料,乙個表佔的空間竟然達到了4g之多。想想有多可怕...

這裡介紹的是用mysql 建立乙個定時器event,定期清除掉之前的不必要事件。

#1

、建立儲存過程供事件呼叫

delimiter

//drop

procedure

ifexists middle_proce//

create

procedure

middle_proce()

begin

delete

from jg_bj_comit_log where comit_time < subdate(now(),interval 2

month

);optimize

table

jg_bj_comit_log;

delete

from jg_bj_order_create where created_on < subdate(now(),interval 3

month

);optimize

table

jg_bj_order_create;

delete

from jg_bj_order_match where created_on < subdate(now(),interval 3

month

);optimize

table

jg_bj_order_match;

delete

from jg_bj_order_cancel where created_on < subdate(now(),interval 3

month

);optimize

table

jg_bj_order_cancel;

delete

from jg_bj_operate_arrive where created_on < subdate(now(),interval 3

month

);optimize

table

jg_bj_operate_arrive;

delete

from jg_bj_operate_depart where created_on < subdate(now(),interval 3

month

);optimize

table

jg_bj_operate_depart;

delete

from jg_bj_operate_login where created_on < subdate(now(),interval 3

month

);optimize

table

jg_bj_operate_login;

delete

from jg_bj_operate_logout where created_on < subdate(now(),interval 3

month

);optimize

table

jg_bj_operate_logout;

delete

from jg_bj_operate_pay where created_on < subdate(now(),interval 3

month

);optimize

table

jg_bj_operate_pay;

delete

from jg_bj_position_driver where created_on < subdate(now(),interval 3

month

);optimize

table

jg_bj_position_driver;

delete

from jg_bj_position_vehicle where created_on < subdate(now(),interval 3

month

);optimize

table

jg_bj_position_vehicle;

delete

from jg_bj_rated_passenger where created_on < subdate(now(),interval 3

month

);optimize

table

jg_bj_rated_passenger;

end//

delimiter;#2

、開啟event(要使定時起作用,mysql的常量global event_schduleer 必須為on 或者1)

show variables

like

'event_scheduler

'set global event_scheduler='on

'#3、建立evnet事件

drop event if

exists

middle_event;

create

event middle_event

on schedule every 1

day starts '

2017-12-05 00:00:01'on

completion preserve enable

do call middle_proce();#4

、開啟event 事件

alter event middle_event on

completion preserve enable;#5

、關閉event 事件

alter event middle_event on completion preserve disable;

資料庫 mysql資料庫定時備份

操作步驟 1 將指令碼放到任意位置下,不容易被誤刪即可 2 定時任務,一周執行一次指令碼 指令碼如下 bin bash backuppath data mysql backup date date y m d h m sqluser root sqlpwd password mysqldump us...

MySQL資料庫定時備份

建立shell指令碼 在 usr local mysql 資料夾下建立backupdb.s 件 指令碼檔案可以自定義位置 vim backupdb.sh 建立指令碼內容如下 bin sh db user root db passwd 123123 db name userdb name date y...

mysql資料庫定時備份

找乙個目錄新建乙個指令碼,這裡選擇 var bin vim var bin mysql data backup.sh指令碼 以下配置資訊請自己修改 mysql user root mysql備份使用者 mysql password root mysql備份使用者的密碼 mysql host loca...