Automatically Purge MySQL Master Log
My Friend Andre Hartawan is troubling due to his mysql server at work stop operating. After quick check he found that no more disk space available on the system. Further check on /var partition he found many replication log file on disk which no longer needed but have big size (100MB). He can easily purge the logs file but wanted the server due it chores automatically. So here it’s I share my script (autopurgebinlog.sh) to purge replication logbin automatically.
#!/bin/bash
# autopurgebinlog.sh
# script to purge mysql replication log-bin
# by am3n.net
# License GNU/GPL
# number of days to keep logbin
AGE=5 # days
AGE_MINS=$[ $AGE * 60 * 24 ]
#find mysql binary log which older than $AGE_MINS
OLDLOGS=`find /var/lib/mysql/$HOSTNAME-bin.??? -cmin +$AGE_MINS | tail -n 1 | gawk -F'/' '{print $5}'`
# change mysqluser and mysqlpass with user that have priviled to purge binary log (i.e. root)
mysql -u mysqluser -pmysqlpass mysql -e "PURGE MASTER LOGS TO '$OLDLOGS'"
Put the script on crontab, run it once a month.
crontab -e 1 1 1 * * /root/autopurgebinlog.sh

Related posts:
- MySQL 5 access via old MySQL-Front 2.5 Kusma!!!, Now I know how to make old mysql front...
- Running MySQL 4 and MySQL 5 Concurrently From Howtoforge.com’s article: Running MySQL 4 And MySQL 5 Concurrently...
- SQL Error: Table ‘mysql.proc’ doesn’t exist Today I upgraded my HeidiSQL into version 4, but when...
- MySQL partition is full MySQL databases are located on /var/lib/mysql, if you create a...
- Script to Monitor Replication Since I sometimes get replication problem, so I make several...
September 9th, 2010 at 19:32
amin,thnx banget infonya,akhirnya lebaran gak ngurusin server
January 9th, 2012 at 00:14
Hi Amin,
It could be easier using expire_log_days global variable: http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html#sysvar_expire_logs_days
Keep Performing,
Moshe Kaplan