In this section, managing the binary logs in a replication environment is covered. Basic binary log handling is already covered in Chapter 6, Binary Logging, using the PURGE BINARY LOGS
command and expire_logs_days
variable.
Using those methods is unsafe in a replication environment because if any one of the slaves has not consumed the binary logs and you have deleted them, the slave will go out of sync and you'll need to rebuild it.
The safe way to delete the binary logs is by checking which binary logs have been read on each slave and deleting them. You can use the mysqlbinlogpurge
utility to achieve this.
Execute the mysqlbinlogpurge
script on any of the servers and specify the master and slave hosts. The script connects to all the slaves and finds out the latest binary log applied. Then it purges the master binary logs until that point. You need a superuser to connect to all slaves:
- Connect to any of the servers and execute the
mysqlbinlogpurge
script...