修改 my.cnf 增加 innodb_log_file_size=256M 後重啟 MySQL 失敗,報錯:
InnoDB: Error: log file ./ib_logfile0 is of different size 0 5242880 bytes
InnoDB: than specified in the .cnf file 0 268435456 bytes!
[ERROR] Plugin 'InnoDB' init function returned error.
[ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
若配置 default-storage-engine=InnoDB,則整個 MySQL 拒絕啟動;若默認 MyISAM,MySQL 能啟動但 InnoDB 引擎不可用(SHOW ENGINES 顯示 InnoDB DISABLED)。
innodb_log_file_size 定義 Redo Log 文件(ib_logfile0, ib_logfile1...)的大小。InnoDB 啟動時會比較**磁盤上現有文件大小**與**配置值**,不一致即拒絕啟動,**防止誤配置導致數據損壞**。
systemctl stop mysqld
# 或
mysqladmin -u root -p shutdown
cd /var/lib/mysql # 或你的 datadir
mv ib_logfile0 ib_logfile0.bak
mv ib_logfile1 ib_logfile1.bak
# 若有更多 ib_logfileN 同理
# /etc/my.cnf
[mysqld]
innodb_log_file_size = 256M
innodb_log_files_in_group = 2 # 默認 2,可調整
systemctl start mysqld
啟動過程中 InnoDB 檢測到無 Redo Log 文件,按新尺寸創建 ib_logfile0、ib_logfile1,正常進入恢復階段(應用 Redo Log 為空,直接完成),服務啟動成功。
SHOW VARIABLES LIKE 'innodb_log_file_size';
SHOW ENGINES; -- InnoDB | DEFAULT | SUPPORTS TRANSACTIONS
rm -f ib_logfile*.bak
MySQL 5.6.8+ 支持在線調整(需 innodb_log_file_size 為動態參數):
SET GLOBAL innodb_log_file_size = 268435456; -- 256M 字節
-- MySQL 會在後台異步創建新文件、切換、刪除舊文件,無需重啟。
innodb_log_files_in_group 不變、磁盤空間足夠、無長事務阻塞。| 參數 | 建議值 | 說明 |
|---|---|---|
innodb_log_file_size | 256M - 2G | 大寫入量環境增大減少 Checkpoint 壓力,總大小 innodb_log_files_in_group * innodb_log_file_size 建議 ≤ Buffer Pool 25% |
innodb_log_files_in_group | 2 - 4 | 循環寫入組數 |
innodb_log_buffer_size | 16M - 64M | 大事務增大,減少刷盤頻率 |
innodb_flush_log_at_trx_commit | 1 (安全) / 2 (性能) | 1=每提交刷盤,2=每秒刷盤 |
innodb_flush_method | O_DIRECT | 避免雙重緩存,配合 RAID/SSD |
修改 innodb_log_file_size **必須配合刪除/備份舊 Redo Log 文件**重啟,或使用 5.6.8+ 在線動態調整。切勿嘗試手動截斷/擴展文件、或忽略報錯強行啟動(會導致數據損壞)。標準化流程:停庫 → 備份舊 Log → 改配置 → 啟動 → 驗證 → 清理備份。