mysql定時腳本(event),類似oracle的job
我有2張表:tb_push_data 和 tb_push_data_log
現在需要每隔一段時間將tb_push_data 符合條件的
數據備份到表 tb_push_data_log
www.2cto.com
--------------------------------------------------------------------------
一、創建存儲過程
DELIMITER $$
USE `push_server_db`$$
DROP PROCEDURE IF EXISTS `proc_tb_push_data_backup`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `proc_tb_push_data_backup`()
BEGIN
START TRANSACTION;
INSERT INTO tb_push_data_log(id,created_date,data_id,gps,have_image,message,recv_name,
recv_username,send_name,send_username,STATUS,tool,updated_date,uri,image_path,log_time)
SELECT id,created_date,data_id,gps,have_image,message,recv_name,recv_username,send_name,
send_username,STATUS,tool,updated_date,uri,image_path,NOW()
FROM tb_push_data WHERE STATUS = 'ALREADY_RECEIVE';
DELETE FROM tb_push_data WHERE STATUS = 'ALREADY_RECEIVE';
COMMIT;
END$$
DELIMITER ;
二、創建MYSQL事件
DELIMITER $$
CREATE event event_tb_push_data_backup
ON SCHEDULE
EVERY 5 MINUTE
STARTS NOW()
DO
BEGIN
CALL event_tb_push_data_backup();
END;
DELIMITER ;
三、驗證並修改數據庫是否開啟了事件 www.2cto.com
大家按照例子做完以後,可能發現沒有運行event,原因大都是因為
event_scheduler狀態沒打開
查看event_scheduler狀態
show variables like '%sche%';
修改event_scheduler狀態
set global event_scheduler=1;