mysql自动删除过期数据
开启事件
查询事件是否开启
show variables like '%sc%';
查看 event_sheduler 是否为ON
临时开启
SETGLOBALevent_scheduler =ON;
或 SETGLOBALevent_scheduler =1;
修改配置永久开启
修改配置文件my.cnf增加配置项
[mysqld]
event_scheduler=ON
重启服务生效
创建事件
如:
每10分钟执行一次 删除过期60分钟的日志记录
create event e_delete_operation_log on schedule every 10 second do delete from operation_log where tcreated_at < (CURRENT_TIMESTAMP() + INTERVAL -60 MINUTE);
每1天执行一次,删除过期180天的消息记录
create event e_delete_message_history on schedule every 1 day do delete from operation_log where created_at < (CURRENT_TIMESTAMP() + INTERVAL -180 DAY);
启用事件
alter event e_delete_operation_log on completion preserve enable;
关闭事件
alter event e_delete_operation_log on completion preserve disable;
删除事件
drop event if exists e_delete_operation_log;
清理磁盘碎片
delete语句只是将数据标记为已删除,未删除磁盘占用,
使用一下命令清理数据磁盘占用。
OPTIMIZE TABLE operation_log;
备份数据库
mysqldump -uroot -pcloudsuite#Passw0rd --databases my_db --lock-tables=0 > ./all.sql
参考:
https://www.nhooo.com/note/qa3g1z.html
https://blog.csdn.net/weixin_64403990/article/details/131697592