当前位置: 首页 > news >正文

MySQL binlog Retention, Rotation Purge: Production Guide (2026)

TL;DR — MySQL binlog retention without disk surprises:Usebinlog_expire_logs_seconds(MySQL 8.0+, replaces deprecatedexpire_logs_days) to set retention — typical safe value is604800 seconds (7 days). Don'tPURGE BINARY LOGS BEFOREmanually unless you've confirmed every replica'sRead_Source_Log_Posis past the boundary. On AWS RDS usemysql.rds_set_configuration('binlog retention hours', 168)instead of the in-instance variable. Monitor binlog disk usage (SHOW BINARY LOGS) — flow-control on a stalled replica can let binlogs grow to TB before alerting. Rotate manually withFLUSH BINARY LOGSafter maintenance, never to 'save space'.

Binary logs are the heartbeat of MySQL replication and point-in-time recovery — but left unmanaged, they silently consume disk space until your database server grinds to a halt at 3 AM. Every production MySQL deployment eventually faces the same reckoning: gigabytes of binlog files piling up on a volume that was never sized to hold them indefinitely. Understanding how binary logs rotate, expire, and can be safely purged is not optional knowledge for a DBA — it is survival knowledge. This guide covers the full lifecycle of MySQL binary logs, from configuration through safe deletion, with the guardrails you need to avoid destroying a replication chain or losing your recovery window.

TL;DR

  • Usebinlog_expire_logs_seconds(MySQL 8.0+) instead of the deprecatedexpire_logs_daysfor automatic expiry.
  • Always checkSHOW REPLICA STATUSbefore manually purging to confirm replicas have consumed the logs you plan to drop.
  • UsePURGE BINARY LOGS TOorPURGE BINARY LOGS BEFOREfor targeted manual cleanup.
  • Setsync_binlog=1in production for durability — never trade crash safety for performance without understanding the risk.
  • On RDS, usemysql.rds_set_configuration('binlog retention hours', N)instead of native MySQL variables.
  • Monitor disk usage continuously; binlog volume spikes during heavy write workloads without warning.

What Are Binary Logs?

MySQL binary logs (binlogs) are a sequential record of every change that modifies data in the database. Unlike the general query log, binlogs capture only>How Binary Log Rotation Works

MySQL writes to a single active binlog file until one of several rotation triggers fires, at which point it closes that file and opens a new one with an incremented sequence number (e.g.,mysql-bin.000001mysql-bin.000002).

Rotation occurs when:

  • The current file reachesmax_binlog_size(default: 1 GB).
  • The server is restarted orFLUSH LOGSis executed.
  • ARESET BINARY LOGS AND GTIDS(formerlyRESET MASTER) statement is issued.

Each rotation produces a new file. The index file (mysql-bin.index) maintains the ordered list of all current binlog files on disk. This is the file MySQL consults to know which logs exist and which can be cleaned up.

Warning

Never manually delete binlog files from the filesystem usingrm. Doing so orphans entries in the index file, which corrupts MySQL's view of its own log state. Always use SQL commands or let the automatic expiry system handle deletions.

Configuring Expiry and Retention

Automatic cleanup is the first line of defense against disk exhaustion. MySQL provides two variables for this, and which one you use depends on your server version.

MySQL 8.0+: binlog_expire_logs_seconds

binlog_expire_logs_secondssets the retention window in seconds. A binary log file is eligible for automatic deletion once it is older than this threshold and a rotation event occurs.

iniCopy

# my.cnf [mysqld] log_bin = /var/lib/mysql/mysql-bin max_binlog_size = 512M binlog_expire_logs_seconds = 604800 # 7 days in seconds

You can also change it at runtime without a restart:

sqlCopy

SET GLOBAL binlog_expire_logs_seconds = 604800;

MySQL 5.7 and Earlier: expire_logs_days

On older versions, the equivalent variable isexpire_logs_days:

iniCopy

# my.cnf (MySQL 5.7) expire_logs_days = 7

Warning

In MySQL 8.0,expire_logs_daysis deprecated. If both variables are set simultaneously,binlog_expire_logs_secondstakes precedence. Do not rely onexpire_logs_daysin MySQL 8.0 deployments — it may be removed in a future release.

Choosing a Retention Window

Your retention window should be long enough to cover your backup cycle plus a comfortable recovery buffer. A common baseline is 7 days, but production teams with longer RTO requirements often extend to 14 or 30 days. The tradeoff is direct: longer retention means more disk space consumed. Size your binlog volume accordingly before extending retention.

binlog_format: ROW vs STATEMENT

The format in which events are written to the binlog significantly affects both file size and behavior:

iniCopy

binlog_format = ROW
  • ROW (recommended):Records the actual row changes. Larger files, but deterministic — the replica knows exactly which rows changed regardless of non-deterministic functions.
  • STATEMENT:Records the SQL statement itself. More compact, but unsafe withNOW(),RAND(), and other non-deterministic functions — can cause replica drift.
  • MIXED:Uses STATEMENT by default and falls back to ROW for unsafe statements. A historical compromise that most modern deployments avoid.

For production replication and PITR,ROWformat is the right choice. Expect binlog files to be larger than with STATEMENT format — factor this into your disk planning.

sync_binlog for Durability

Thesync_binlogvariable controls how often MySQL flushes the binlog to disk:

iniCopy

sync_binlog = 1 # Flush to disk after every commit (safest)

Withsync_binlog=1, MySQL callsfsync()after every transaction commit, ensuring no committed transactions are lost in a crash. The performance cost is real on high-throughput systems, but the data safety guarantee is essential for production primaries. Setting it to0delegates flushing to the OS — faster, but you risk losing the last few seconds of committed transactions on a crash.

Purging Binary Logs Safely

Automatic expiry handles routine cleanup, but there are scenarios where you need to manually reclaim disk space immediately. MySQL provides two targeted purge commands.

Step 1: Check What You Have

sqlCopy

SHOW BINARY LOGS;

This returns a list of all binlog files with their sizes. Identify the oldest files you want to remove.

Step 2: Verify Replica Status

This is the step most people skip — and it is the one that breaks replication. Before purging any log, confirm your replicas have already consumed it:

sqlCopy

-- Run on each replica SHOW REPLICA STATUS\G

Look at theSource_Log_Filefield (formerlyMaster_Log_File). This tells you which binlog file the replica is currently reading from. Never purge any file with a sequence number equal to or greater than this value.

Warning

If you purge a binlog file that a replica still needs, the replica's IO thread will fail with "Got fatal error 1236 from source when reading data from binary log." Recovering from this requires either re-syncing the replica from a backup or relying on GTID-based auto-positioning, which is not always available in every configuration.

Step 3: Purge

To purge all logs up to (but not including) a specific file:

sqlCopy

PURGE BINARY LOGS TO 'mysql-bin.000042';

To purge all logs older than a specific timestamp:

sqlCopy

PURGE BINARY LOGS BEFORE '2026-02-15 00:00:00';

Tip

When usingPURGE BINARY LOGS BEFORE, set the timestamp at least 24 hours behind the current time to account for any replication lag on your replicas. A lagging replica may still need logs that appear old by wall clock time.

RDS and Cloud-Managed MySQL

On Amazon RDS for MySQL, you cannot setbinlog_expire_logs_secondsdirectly via parameter groups. RDS manages binlog lifecycle through its own mechanism. Use the provided stored procedure to configure retention:

sqlCopy

-- Set binlog retention to 72 hours on RDS CALL mysql.rds_set_configuration('binlog retention hours', 72); -- Verify CALL mysql.rds_show_configuration();

RDS defaults to NULL (no retention limit) if this is never configured, which means binlogs accumulate indefinitely. Set this immediately after provisioning any RDS MySQL instance that uses replication or PITR.

Monitoring Disk Usage

Disk exhaustion from binary logs is a foreseeable and preventable failure. Build monitoring around it rather than reacting to it after the fact.

Checking Current Binlog Disk Usage

sqlCopy

-- Total size of all binary logs on disk SELECT COUNT(*) AS log_count, ROUND(SUM(File_size) / 1024 / 1024 / 1024, 2) AS total_size_gb FROM information_schema.FILES WHERE FILE_TYPE = 'UNDO LOG'; -- Alternative: sum from SHOW BINARY LOGS SELECT COUNT(*) AS log_count, ROUND(SUM(File_size) / 1073741824, 2) AS total_gb FROM ( SELECT File_size FROM mysql.`general_log` LIMIT 0 -- placeholder ) t;

The most reliable approach is querying the output ofSHOW BINARY LOGSdirectly or checking filesystem usage on the binlog directory:

bashCopy

du -sh /var/lib/mysql/mysql-bin.* df -h /var/lib/mysql

Automated Cleanup Script

For self-managed MySQL servers without automated expiry events, a simple cron-based approach works well. This script checks replica consumption before purging:

bashCopy

#!/bin/bash # safe-binlog-purge.sh # Purge binlogs older than 7 days, only if replicas are current MYSQL="mysql -u root -p${MYSQL_PWD}" RETENTION_DAYS=7 CUTOFF=$(date -d "-${RETENTION_DAYS} days" '+%Y-%m-%d %H:%M:%S') # Get the oldest log file still needed by any replica REPLICA_LOG=$($MYSQL -e "SHOW REPLICA STATUS\G" 2>/dev/null \ | grep "Source_Log_File" | awk '{print $2}') if [ -z "$REPLICA_LOG" ]; then echo "No replica detected or SHOW REPLICA STATUS returned empty." echo "Proceeding with time-based purge with caution." fi echo "Purging binary logs before: $CUTOFF" $MYSQL -e "PURGE BINARY LOGS BEFORE '$CUTOFF';"

Tip

Alert when the binlog directory exceeds 70% of the volume's total capacity. By the time you hit 90%, you may not have enough headroom to take an emergency backup before the disk fills completely.

Metrics to Track

  • Binlog disk usage (GB)— alert at 70% volume capacity.
  • Binlog write rate (MB/s)— spikes indicate heavy write workloads that will consume retention faster.
  • Seconds behind source— replica lag means replicas need older logs; increasing lag widens the window of logs you cannot safely delete.
  • Number of binlog files— a proxy for whether automatic expiry is functioning correctly.

Key Takeaways

Key Takeaways

  • Binary logs are essential for replication and PITR — they must be managed, not disabled, to reclaim disk space.
  • Usebinlog_expire_logs_secondson MySQL 8.0+;expire_logs_daysis deprecated and will be removed.
  • Always runSHOW REPLICA STATUSon every replica before manual purging — never guess which logs are safe to drop.
  • UsePURGE BINARY LOGS TOfor file-based targeting andPURGE BINARY LOGS BEFOREfor time-based targeting; never usermon binlog files directly.
  • Setsync_binlog=1in production to prevent committed transaction loss on crash.
  • UseROWbinlog format for deterministic replication; account for its larger file sizes in disk planning.
  • On RDS, configure retention viamysql.rds_set_configuration('binlog retention hours', N)— it defaults to unlimited and will fill your storage.
  • Build proactive disk monitoring around your binlog directory; do not wait for alerts after the volume is full.
http://www.rkmt.cn/news/1462200.html

相关文章:

  • 实战演练,基于快马平台用reasonix构建智能课程推荐系统
  • 如何快速解密RPG Maker MV游戏资源:开发者的3种终极解决方案
  • GLM-5工程化落地实测:国产大模型推理部署全链路解析
  • 深圳鑫大地:金属冰箱贴定制优选工厂,15年匠心打造有温度的纪念好物 - 中媒介
  • 今天的日常
  • 腾讯TBS X5内核集成避坑指南:从‘提取微信’到‘官方静态集成’的演进与最佳实践
  • HTTP 完全指南(一):请求与响应报文结构深度详解
  • 2026苏州吴中/高新换季瓷砖起拱翘边是什么原因?怎么根治 - 苏易修缮
  • Notepad-- 终极指南:如何快速上手这款跨平台代码编辑器
  • 3分钟掌握RPG Maker MV解密工具:新手也能轻松提取游戏资源
  • Docker 核心概念详解:从“会用”到“真正理解”
  • 英托克直流调速器ID271/35A/380V型号的跨电压应用观察
  • PDF Arranger终极指南:免费开源PDF页面管理神器
  • 2026 年 6 月证券从业自学通关秘籍:高效工具实测全解 - 讲清楚了
  • 沪上珠宝首饰回收权威榜单,蒂芙尼回收首选上海禹竞名奢汇 - 奢侈品交易观察员
  • 不如去杭州“躺平”一会儿!西湖边这条惬意漫步路线,太治愈了
  • 2026 年 6 月证券刷题神器实测:免费高效通关全攻略 - 讲清楚了
  • Micro:bit与伺服电机打造圣诞旋转木马:从硬件连接到编程控制
  • XDM浏览器扩展终极指南:如何快速安装并提升下载速度500%
  • AI数字营销实测体验,批量生产功能体验
  • MATLAB小波多尺度图像配准与融合可视化工具(含测试图+可运行GUI源码)
  • 监管新规倒计时60天:金融机构AI投资系统合规改造清单(含证监会备案自查表V2.3)
  • 从一封邮件被删除说起:Wireshark深度解析POP3协议的‘状态机’与安全启示
  • 用AI写论文为何越用越累?复盘我的踩坑经验与正确用法
  • 2026年广州/东莞搬家服务TOP5榜单:精品搬家、厂房搬迁、日式搬家及设备搬运公司实力推荐 - 品牌企业推荐师(官方)
  • 用树莓派搭建Pi-Hole:打造无广告家庭网络的完整指南
  • STM32F103驱动LD3320离线语音识别工程(Keil MDK可直接编译)
  • 2026年 工业环保设备厂家推荐榜:重庆/福建/贵州除尘净化、一体化喷淋废气及固废处理节能公司深度解析 - 品牌企业推荐师(官方)
  • PHY与MAC接口
  • 2026 北京闲置钻石、钻戒变现指南,亲测这家体验超好 - 奢侈品回收测评