Purge MySQL Binary Logs

If you have binary logging enabled in MySQL, for example if you are using replication, there is a good chance that these binary logs will grown pretty big and fill the disk.  It’s a good idea to set some limits in my.cnf

expire-logs-days    = 20
max_binlog_size         = 104857600

If you find the need to purge existing logs:

To delete all binary logs older than 7 days:

mysql> PURGE BINARY LOGS BEFORE DATE_SUB( NOW( ), INTERVAL 7 DAY);

To purge all logs before a specific date:

mysql> PURGE MASTER LOGS BEFORE ‘2008-01-01 00:00:00′;

To purge logs automatically (i.e every Monday at 3am) you could use a cron job:

0 3 * * mon mysql -uroot -e “PURGE BINARY LOGS BEFORE DATE_SUB( NOW( ), INTERVAL 7 DAY);”

You can leave a response, or trackback from your own site.

Leave a Reply