Mysql 存储过程+事件 定时备份表

阅读更多
转自:https://blog.csdn.net/xlxxcc/article/details/52487617

1. 定义存储过程bakOpLog
CREATE DEFINER=`sa`@`%` PROCEDURE `bakOpLog`()
BEGIN
create table operationlog_temp like operationlog;
set @i=current_date();
--执行rename table operationlog to operationlog_yyyy-mm-dd
set @sqlstr=CONCAT('rename table operationlog to `operationlog_',cast(@i as char),'`');
select @sqlstr;
PREPARE renameOpLog FROM @sqlstr;
EXECUTE renameOpLog;
rename table operationlog_temp to operationlog;
END;

2.存储过程用到的一些语法
set @i=current_date();  //将全局变量i赋值为当前日期
set @sqlstr=CONCAT('rename table operationlog to `operationlog_',cast(@i as char),'`'); //sqlstr=rename table operationlog to operationlog_yyyy-mm-dd
PREPARE renameOpLog FROM @sqlstr;   //定义预处理语句
EXECUTE renameOpLog;    //执行预处理语句

查看创建的事件
SHOW EVENTS;
也可以在mysql库中产看event表

1) 临时关闭事件
ALTER EVENT e_test DISABLE;
2) 开启事件
ALTER EVENT e_test ENABLE;
3) 将每天清空test表改为5天清空一次:
ALTER EVENT e_test
ON SCHEDULE EVERY 5 DAY;
4) 删除事件(DROP EVENT)
DROP EVENT [IF EXISTS] event_name
例如删除前面创建的e_test事件
DROP EVENT e_test;
当然前提是这个事件存在,否则会产生ERROR 1513 (HY000): Unknown event错误,因此最好加上IF EXISTS
DROP EVENT IF EXISTS e_test;

你可能感兴趣的:(Mysql 存储过程+事件 定时备份表)