Purge MySQL binary logs

Topics: MySQL replication

MySQL binary logs record database changes and can consume disk space if retained indefinitely. This note shows how to purge them by age or file name after checking which logs replicas still need.

Subtopics

Purge by age

A shell command to remove binary logs older than 4 hours:

mysql -e "PURGE BINARY LOGS BEFORE '$(date -d "-4hours" "+%Y-%m-%d %H:%M:%S")'"

Purge by file name

The most effective way to free up disk space is to delete all binary logs up to the Master_Log_File the follower node is currently processing. This is safe because all these logs have already been processed. For example, if SHOW SLAVE STATUS\G on the follower node displays mysql-bin.001234 as the current binary log, you free up disk space on the leader node as follows:

purge binary logs to 'mysql-bin.001234';

Bookmarks

Questions