Skip to content

Managing MySQL binlog size on a cPanel server without losing point-in-time recovery

Binlogs grow fast on write-heavy workloads. The `expire_logs_days` default is set up to be safe for replication and wrong for everyone else — and PURGE BINARY LOGS has its own traps.

The first sign of a binlog problem on a cPanel server is usually /var/lib/mysql 95% full and df -h blaming mysql-bin.000847 for 20GB of space. Disabling binlog “just to free disk” cancels your point-in-time recovery window and breaks any downstream replica. The right answer is to manage retention, not turn it off — and to make sure the retention setting matches your actual recovery needs and downstream consumers.

What’s happening

When binlog is enabled, every write generates a binlog event. Binlogs roll over by size (max_binlog_size, default 1GB) and accumulate in /var/lib/mysql. Three things expire them:

  • expire_logs_days (legacy) — automatic deletion after N days. Default 0 means never.
  • binlog_expire_logs_seconds (newer, MySQL 8.0+ / MariaDB 10.6+) — same concept, in seconds.
  • PURGE BINARY LOGS — manual purge to a named file or before a timestamp.

The catch: if a replica is connected and reading from log file N, you can’t safely purge N or later — the replica will lose its position and need re-snapshotting. expire_logs_days honors this; PURGE BINARY LOGS BEFORE does not always.

For point-in-time recovery, you need the binlogs from the last full backup to “now.” If your full backup runs nightly and you keep 24 hours of binlog, you can restore to any point in the last 24 hours after restoring the previous night’s dump. Below that retention, point-in-time recovery has gaps.

How to verify

mysql -e "SHOW VARIABLES LIKE 'expire_logs_days'"
mysql -e "SHOW VARIABLES LIKE 'binlog_expire_logs_seconds'"
mysql -e "SHOW VARIABLES LIKE 'max_binlog_size'"
mysql -e "SHOW BINARY LOGS"
mysql -e "SHOW MASTER STATUS"
mysql -e "SHOW SLAVE HOSTS"
du -sh /var/lib/mysql/mysql-bin.*
df -h /var/lib/mysql

The first three show retention policy. SHOW BINARY LOGS lists current files with sizes. SHOW MASTER STATUS shows the current write position. SHOW SLAVE HOSTS lists replicas that are reading the binlog — purge anything they’re still on and replication breaks.

The fix

  1. Set retention based on backup cadence. If you do nightly full backups, set:
    /etc/my.cnf.d/cpanel.cnf:
    [mysqld]
    binlog_expire_logs_seconds = 172800  # 48 hours
    max_binlog_size = 256M
    48 hours gives you a buffer if a backup fails or the most recent one is corrupt. 256MB per file is small enough that you’re not handling 1GB files but big enough to avoid excessive file rotation overhead. Restart MySQL.
  2. For active replication, check that the retention is longer than the worst-case replica lag plus your recovery procedure. If your replica goes offline overnight and you need to restart it 12 hours later, you need at least 12 hours of binlog retained.
  3. Manual purge when you’re sure no replica needs it:
    mysql -e "PURGE BINARY LOGS TO 'mysql-bin.000840'"
    This deletes binlogs strictly before mysql-bin.000840. The TO form is safer than BEFORE <timestamp> because it’s explicit about which file is the cutoff. Confirm no replica is on those files first.
  4. Never delete binlog files from the filesystem with rm. MySQL maintains an internal index (mysql-bin.index) that lists active binlogs. Deleting files directly leaves the index pointing at missing files, and the next PURGE or FLUSH LOGS errors out. Always use PURGE BINARY LOGS through SQL.
  5. For accounts using binlog for change capture (Debezium, Maxwell), retention has to cover the consumer’s downtime window. If Debezium goes down for a weekend and binlogs only retain 24 hours, the consumer needs a re-snapshot when it returns.
  6. For audit retention compliance, push expired binlogs to off-host storage before purge. Add a daily cron that copies mysql-bin.000* files older than 24 hours to S3 or a backup target, then purges. Restoration is now slower (download, replay) but storage on the host is bounded.

For cPanel hosts where binlog management is part of the disaster recovery posture, Stack Harbor’s environment management team sizes retention against backup cadence and downstream consumer needs, and runs binlog-replay drills against the dump archive quarterly.