用shell写一下定时清理relay log的MySQL数据库脚本
时间: 2023-05-16 13:06:59 浏览: 87
可以使用以下脚本来定时清理MySQL数据库中的relay log:
```
#!/bin/bash
# Set the MySQL credentials
MYSQL_USER="username"
MYSQL_PASSWORD="password"
# Set the path to the MySQL bin directory
MYSQL_BIN="/usr/bin/mysql"
# Set the path to the MySQL data directory
MYSQL_DATA="/var/lib/mysql"
# Set the number of days to keep relay logs
DAYS_TO_KEEP=7
# Get the current date
CURRENT_DATE=$(date +%Y-%m-%d)
# Calculate the date to delete relay logs before
DELETE_DATE=$(date +%Y-%m-%d -d "$DAYS_TO_KEEP days ago")
# Delete the relay logs
$MYSQL_BIN -u $MYSQL_USER -p$MYSQL_PASSWORD -e "PURGE BINARY LOGS BEFORE '$DELETE_DATE';"
# Delete the relay log index file
rm -f $MYSQL_DATA/relay-log.info
# Create a new relay log index file
$MYSQL_BIN -u $MYSQL_USER -p$MYSQL_PASSWORD -e "FLUSH LOGS;"
```
将以上脚本保存为一个文件,比如说"clean_relay_logs.sh",然后使用cron来定时运行该脚本。例如,如果你想每天凌晨2点清理relay log,可以使用以下命令来编辑cron表:
```
crontab -e
```
然后在文件末尾添加以下行:
```
0 2 * * * /path/to/clean_relay_logs.sh
```
保存并退出文件即可。现在,cron将在每天凌晨2点运行"clean_relay_logs.sh"脚本,以清理MySQL数据库中的relay log。
阅读全文