清理Mysql binlog日志的脚本
时间: 2023-05-18 16:03:23 浏览: 126
MySQL bin-log 日志清理方式
以下是一个清理 MySQL binlog 日志的脚本示例:
#!/bin/bash
# 设置变量
MYSQL_USER="root"
MYSQL_PASSWORD="password"
MYSQL_HOST="localhost"
MYSQL_PORT="3306"
BINLOG_DIR="/var/lib/mysql/"
BINLOG_PREFIX="mysql-bin"
BINLOG_DAYS_TO_KEEP="7"
# 获取当前日期
CURRENT_DATE=$(date +"%Y-%m-%d %H:%M:%S")
# 获取需要删除的 binlog 文件名
BINLOG_FILES_TO_DELETE=$(mysql -u${MYSQL_USER} -p${MYSQL_PASSWORD} -h${MYSQL_HOST} -P${MYSQL_PORT} -e "SHOW BINARY LOGS;" | awk '{if($1 < DATE_SUB(NOW(), INTERVAL ${BINLOG_DAYS_TO_KEEP} DAY)) print $1}' | grep ${BINLOG_PREFIX})
# 删除 binlog 文件
for BINLOG_FILE in ${BINLOG_FILES_TO_DELETE}
do
echo "${CURRENT_DATE} - Deleting binlog file ${BINLOG_FILE}"
rm -f ${BINLOG_DIR}/${BINLOG_FILE}
done
echo "${CURRENT_DATE} - Done."
阅读全文