MySQL Binary Logs and Disk Space

Browse by products and services

  • Applies to: DV 4.0
    • Difficulty: Easy
    • Time Needed: 10
    • Tools Required: root access, SSH


If your DV 4.0 is running out of disk space and binary logging (replication) is enabled in MySQL, you can purge these logs and disable future logging to prevent this from occurring again.

Checking MySQL binary logs

To check how much space is being taken up by your MySQL binary logs, you can issue the following command via SSH as the "root" user:

echo "$(du -c /var/lib/mysql/mysql-bin.* | tail -1 | cut -f 1) $(df / | tail -1 | awk '{print $4}')" | awk '{printf "MySQL binlog consuming " "%3.2f Gigabytes of disk space:\n", $1 / 1024 / 1024; printf "%3.2f percent of total disk space\n", $1 / $2 *100}'

If this gives you a large number or percentage, you likely will want to purge these binary logs.

Purge and Prevent

If you want to purge the logs, you likely don't intend to have MySQL replication turned on, so you will want to turn off binary logging altogether. Please consult with your development team and system administration team before proceeding if you are unsure whether you need replication, however, replication is typically only used for highly customized, multi-server applications.

To purge the binary logs, which will free up all of the space which was shown in the "checking" command above, and also prevent MySQL from writing these binary logs in the future, you can run the following command via SSH as the "root" user:

my -e "purge binary logs before '$(date "+%F %T")'" && sed -i.$(date +%s).bak -e "/^binlog_format\|^log-bin/s/^/#/" /etc/my.cnf && /etc/init.d/mysqld restart

The above command may take several moments to complete, depending on how large these logs have grown. Checking your available disk space now with the df -h command should confirm that your disk space issues are resolved, if the binary logs were indeed the primary culprit on your server.