MySQL purge the binary logs from replication (mysql-bin.xxxx files)

Want to help support this blog? Try out Oh Dear, the best all-in-one monitoring tool for your entire website, co-founded by me (the guy that wrote this blogpost). Start with a 10-day trial, no strings attached.

We offer uptime monitoring, SSL checks, broken links checking, performance & cronjob monitoring, branded status pages & so much more. Try us out today!

Profile image of Mattias Geniar

Mattias Geniar, November 19, 2011

Follow me on Twitter as @mattiasgeniar

There are times when a MySQL replication can hog up a lot of disk space, due to a sudden burst of queries that are being logged into your binary logs to be executed on the slave MySQL server(s). On your filesystem, it will look like this.

-rw-rw----  1 mysql mysql 501M Nov 15 08:15 mysql-bin.000044
-rw-rw----  1 mysql mysql 501M Nov 15 10:42 mysql-bin.000045
-rw-rw----  1 mysql mysql 501M Nov 15 12:50 mysql-bin.000046
-rw-rw----  1 mysql mysql 501M Nov 15 15:02 mysql-bin.000047
-rw-rw----  1 mysql mysql 501M Nov 15 17:14 mysql-bin.000048
-rw-rw----  1 mysql mysql 501M Nov 15 19:28 mysql-bin.000049
-rw-rw----  1 mysql mysql 501M Nov 15 21:43 mysql-bin.000050
-rw-rw----  1 mysql mysql 501M Nov 15 23:58 mysql-bin.000051

Even with a normal value in the “expire_logs_days” (how long those logs should be kept), in a single day it can increase to massive amounts when you’re working with a lot of data.

If you want to manually purge those logs, because you’re safe (yeah right) and know you won’t be needing them anymore for data recovery (yeah right), you can run the following query in MySQL.

# mysql
mysql> PURGE BINARY LOGS BEFORE '2011-11-16';
Query OK, 0 rows affected (0.57 sec)

mysql> exit

Change the data of the “BEFORE” statement to when you actually want to clear them, and the binlogs will be cleaned up nicely.



Want to subscribe to the cron.weekly newsletter?

I write a weekly-ish newsletter on Linux, open source & webdevelopment called cron.weekly.

It features the latest news, guides & tutorials and new open source projects. You can sign up via email below.

No spam. Just some good, practical Linux & open source content.