How do I purge/expire my MySQL binary logs?
I have unfeasibly large mysql .bin logs filling up my /var partition! How do I free up the space they are taking up? Can I prevent the build up in future?
The problem may show up in some cases as something like:
# du -sh /var/lib/mysql/*
(tested on Bright 7.0)
The binary logs can be listed and deleted from the mysql command line.
First log into mysql as root by running this command on the head node:
# mysql -u root -p
To show which files are being used to store binary logs, you can run this command on the mysql command line:
> SHOW BINARY LOGS;
To delete (purge) old binary logs, you can use the “PURGE BINARY LOGS” statement. The following command would, for example, purge the logs from last week:
> PURGE BINARY LOGS BEFORE DATE(NOW() - INTERVAL 7 DAY) + INTERVAL 0 SECOND;
The MySQL documentation covers this usage in further detail.
In addition to purging the logs, it is possible to configure mysql to purge the files after a given number of days, as described in https://dev.mysql.com/doc/refman/5.7/en/purge-binary-logs.html15
For example, if you want the binary logs to expire after 7 days, add the following line to /etc/my.cnf:
You will also need to log into mysql as root and run this command (if you don't want to restart MySQL) to make the new setting active:
> SET GLOBAL expire_logs_days = 7;
In the same way, you can also configure my.conf to set the maximum binary log size, for example:
max_binlog_size = 100M
> SET GLOBAL max_binlog_size = 100M;
Log rotation can then tuned to remove logs when there are too many logs. The details of how to do that depends on the operating system and database version.
If you don't have the mysql root password, you can reset it as described in http://kb.brightcomputing.com/faq/index.php?action=artikel&id=3216