Sunday, 22 February 2015

MySQL (safe) Binlog Removal

We have a MySQL Master-Slave setup, which has been running for many years without issue.

Recently, the disk space on the Master has been filling up with the binlog files, and so I started looking for a way to automate the process of removing old ones.

Here some background info first:

MySQL on the Master has files named eg mysql-bin.002465

These are rotated once a day, and as they have sizes of 100-500MB and quickly use up the free diskpace on the server.

However, these files are used by any MySQL slaves that point to this master.
If there are no network or hardware issues, the slave is normally up to speed with the master and so the delay behind the master is normally 0s.

If this is the case, then any files not currently being used can be removed without a problem.

However, if there has been (network/hardware) issues with the slave, then it could be many seconds or even hours behind the master, and therefore still using an older binlog file.

If an old binlog file is removed before a slave has finished processing it, the slave cannot continue and breaks consistency with the master, so it is important to make sure that only the files that have been finished by the slave are removed.

Removing shouldn't be done by deleting files, as the mysq-bin.index keeps track of files used, and deleting them doesn't remove them from this list.
The correct way to remove them is from within MySQL run the command:
mysql>PURGE BINARY LOGS TO 'mysql-bin.002499'
where mysql-bin.002499 is the filename on the master that the slave is currently using (ie has already processed the files before it).

In order to find out where the slave is up to in the binlog files, this MySQL command needs to be run on the Slave:
mysql>SHOW SLAVE STATUS\G
In amongst all of the data that comes out is the line:
Relay_Master_Log_File: mysql-bin.002499
This is the current file that is being processed by this slave.

So, a solution is needed to find the filename from the output line of a command on the MySQL Slave, and to pass that into a command for the MySQL Master to purge the files until that point.

After a bit of research, the following solution was found:
#!/bin/bash
eval `{ echo "mysql -uroot -e "; echo "'PURGE BINARY LOGS TO \""; mysql -h 192.168.0.251 -uroot -e 'SHOW SLAVE STATUS\G' | grep 'Relay_Master_Log_File' | cut -c30-; echo "\"';"; } | sed ':a;N;$!ba;s/\n//g'`
 This is saved as the file purge-mysql-logs-after-slave.sh in the /root directory with chmod +x to make it executable.
To schedule it daily, it is added to crontab like this:
# m h  dom mon dow   command
30 20 * * * /root/purge-mysql-logs-after-slave.sh
and so runs at 8.30pm every day (backup of mysql happens 9pm).
This is important as otherwise we will be removing all binary logs with data in, as the backup flushes the logs, creating this creates a new log file. Doing this before the backup happens means the purge leaves the current full binlog in tact, and the backup causes a new one to be created. Therefore there will always be 2 binlogs on the master at any time.

No comments:

Post a Comment