錯誤現象
InnoDB: Error: log file ./ib_logfile0 is of different size 0 5242880 bytes
InnoDB: Could not open or create the system tablespace. If you tried to add new data files to the system tablespace, and it failed here, you should now edit innodb_data_file_path in my.cnf back to what it was, and remove the new ibdata files InnoDB created in this failed attempt. InnoDB only wrote those files full.
根因分析
- 修改
my.cnf 中的 innodb_log_file_size 參數 (如從 5MB 改為 256MB)。
- 舊的
ib_logfile0, ib_logfile1 檔案大小與新配置不符。
- InnoDB 啟動時檢測到大小不一致,為保護資料完整性拒絕啟動。
標準修復步驟 (零資料遺失)
- 備份舊 Log 檔案 (而非直接刪除,防萬一需回滾)
mkdir -p /backup/iblog_$(date +%F)
mv /var/lib/mysql/ib_logfile* /backup/iblog_$(date +%F)/
- 確認 my.cnf 配置已更新
[mysqld]
innodb_log_file_size = 256M
innodb_log_files_in_group = 2
- 啟動 MySQL
systemctl start mysqld
# 或
mysqld_safe --user=mysql &
- 驗證新 Log 檔案大小
ls -lh /var/lib/mysql/ib_logfile*
# -rw-r----- 1 mysql mysql 256M Jan 1 12:00 ib_logfile0
# -rw-r----- 1 mysql mysql 256M Jan 1 12:00 ib_logfile1
- 檢查錯誤日誌確認正常啟動
grep -i "innodb" /var/log/mysql/error.log | tail -20
# InnoDB: Log file ./ib_logfile0 did not exist: new to be created
# InnoDB: Setting log file ./ib_logfile0 size to 268435456 bytes
# InnoDB: Log file ./ib_logfile1 did not exist: new to be created
# InnoDB: Setting log file ./ib_logfile1 size to 268435456 bytes
常見誤區與避坑
| 誤區 | 後果 | 正確做法 |
| 直接刪除 ib_logfile* 再啟動 | 若備份失敗或配置錯誤,無法回滾;InnoDB 會重建但可能觸發雙重寫入檢查 | 先 mv 備份,確認新檔建立正常再清理舊備份 |
| 修改 innodb_log_file_size 但未重啟 | 配置不生效,錯誤持續 | 必須重啟 mysqld 進程 |
| 調大 Log 檔案但未調整 innodb_log_files_in_group | 總 Redo Log 容量可能不足,頻繁檢查點影響效能 | 總量建議 1-4GB,組數 2-3 |
| 在主從複寫環境直接修改 Master | Slave 同步中斷,需重建 | 先修改 Slave,驗證無誤後再修改 Master,或維護窗口同步修改 |
為什麼要調大 Redo Log?
- 預設 5MB (5.6-) / 48MB (5.7+) 過小,高寫入負載下頻繁檢查點 (
checkpoint) 導致 fsync 風暴、IO 抖動、延遲飆升。
- 增大至 256MB-1GB 可平滑檢查點頻率,平衡恢復時間與運行效能。
- 監控指標:
Innodb_log_waits > 0 表示 Redo Log 過小導致等待。
線上調整 (MySQL 8.0.30+ 支援動態調整)
-- 僅支援增加,不支援減少
SET GLOBAL innodb_log_file_size = 536870912; -- 512MB
SET GLOBAL innodb_log_files_in_group = 3;
-- 實際生效需重啟,但 8.0.30+ 可線上擴容無需重啟 (檢查版本發布說明)
Redo Log 大小調整是 InnoDB 調優高頻操作,建立標準化 SOP:備份 → 修改配置 → 重啟 → 驗證 → 清理備份,納入變更管理流程,避免手抖誤刪導致資料不可用。