# 靜態配置 (持久化需寫入 my.cnf) SHOW GLOBAL VARIABLES LIKE 'binlog%'; SHOW GLOBAL VARIABLES LIKE 'max_binlog%'; SHOW GLOBAL VARIABLES LIKE 'sync_binlog'; # 動態狀態 (運行時統計) SHOW GLOBAL STATUS LIKE 'Binlog%';
監控判斷:
SHOW GLOBAL STATUS LIKE 'Binlog_cache%'; -- Binlog_cache_use: 內存緩存命中次數 -- Binlog_cache_disk_use: 寫磁盤次數 (應趨近 0)
調優建議: 若 Binlog_cache_disk_use 持續增長,增大至 8M-32M (視事務大小)。
Multi-statement transaction required more than 'max_binlog_cache_size' bytes of storage| 值 | 行為 | 性能 | 安全性 | 適用場景 |
|---|---|---|---|---|
| 0 | OS 決定刷盤 (通常由 dirty_ratio/expire 控制) | 最快 (基準 100%) | 最低 (Crash 丟失 Cache 內所有事務) | 非核心日志、可容忍數據丟失 |
| 1 | 每次提交 fsync | 最慢 (約 20-30% 基準) | 最高 (最多丟 1 個未提交事務) | 核心業務、金融、強一致 |
| N (100-1000) | 每 N 次提交 fsync | 折中 | 折中 (最多丟 N 個事務) | 高併發非核心、可接受秒級丟失 |
高併發寫入場景 (SysBench OLTP RW):
sync_binlog=0:QPS 基準 100%sync_binlog=1:QPS 下降 50-70% (視磁盤 IOPS)=1 損耗降至 10-20%[mysqld] # Binlog 基礎 log-bin = mysql-bin binlog_format = ROW expire_logs_days = 7 # 緩存優化 (根據監控調整) binlog_cache_size = 8M max_binlog_cache_size = 1G max_binlog_size = 512M # 持久化 (核心業務必開 1) sync_binlog = 1 # 可選:減少 fsync 系統調用開銷 (需硬件支援) # innodb_flush_log_at_trx_commit = 1 # 保持雙 1 保證 ACID
0.01
labels:
severity: warning
annotations:
summary: "Binlog fsync 延遲過高"
description: "檢查磁盤 IOPS/RAID 卡 BBU 狀態"
sync_binlog=1 + innodb_flush_log_at_trx_commit=1 雙 1 安全Binlog_cache_disk_use 動態調整 binlog_cache_size
3. 大事務場景預調 max_binlog_cache_size
4. 硬件層面:SSD/NVMe + RAID 卡 BBU 是 Binlog 性能關鍵