Wednesday, September 14, 2011

MySQL Bin Files Eating Lots of Disk Space

SkyHi @ Wednesday, September 14, 2011
Q. I get a large amount of bin files in the MySQL data directory called "server-bin.n" or mysql-bin.00000n, where n is a number that increments. What is MySQL Binary Log? How do I stop these files being created?


A. Usually /var/lib/mysql stores the binary log files. The binary log contains all statements that update data or potentially could have updated it. For example, a DELETE or UPDATE which matched no rows. Statements are stored in the form of events that describe the modifications. The binary log also contains information about how long each statement took that updated data.

The purpose of MySQL Binary Log

The binary log has two important purposes:
  • Data Recovery : It may be used for data recovery operations. After a backup file has been restored, the events in the binary log that were recorded after the backup was made are re-executed. These events bring databases up to date from the point of the backup.
  • High availability / replication : The binary log is used on master replication servers as a record of the statements to be sent to slave servers. The master server sends the events contained in its binary log to its slaves, which execute those events to make the same data changes that were made on the master.

Disable MySQL binlogging

If you are not replicating, you can disable binlogging by changing your my.ini or my.cnf file. Open your my.ini or /etc/my.cnf (/etc/mysql/my.cnf), enter:
# vi /etc/my.cnf

Find a line that reads "log_bin" and remove or comment it as follows:
#log_bin                        = /var/log/mysql/mysql-bin.log
 
You also need to remove or comment following lines:
#expire_logs_days        = 10
#max_binlog_size         = 100M
 
Close and save the file. Finally, restart mysql server:
# service mysql restart

 

Purge Master Logs

If you ARE replicating, then you need to periodically RESET MASTER or PURGE MASTER LOGS to clear out the old logs as those files are necessary for the proper operation of replication. Use following command to purge master logs:
$ mysql -u root -p 'MyPassword' -e "PURGE BINARY LOGS TO 'mysql-bin.03';"
OR
$ mysql -u root -p 'MyPassword' -e "PURGE BINARY LOGS BEFORE '2008-12-15 10:06:06';"

Suggested readings:

MySQL Manual - The binary logs


REFERENCES
http://www.cyberciti.biz/faq/what-is-mysql-binary-log/