首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫

MySQL 從 MyISAM 轉換 InnoDB 完整遷移指南與錯誤排查實戰

     編輯:MySQL 資料庫相關知識

在 WordPress 專案中,預設使用 InnoDB 儲存引擎以支援交易與外鍵約束。若初期資料庫以 MyISAM 建立,後續遷移至 InnoDB 時常會遭遇服務啟動失敗、記憶體無法讀取等棘手問題。本文詳細記錄一次完整的遷移排查過程,從錯誤日誌分析到最終解決方案,提供實戰參考。

一、問題背景與現象

原資料庫使用 MyISAM,安裝 WordPress 並匯入 SQL 均正常。但在新增第二個 WordPress 站點、頻繁存取 phpMyAdmin 後,MySQL 服務頻繁崩潰,錯誤訊息為:

Mysqld-nt.exe 應用程式錯誤:「0x005346c4」指令引用的「0x00786000」記憶體,該記憶體不能為 read…

服務停止後無法重啟,整個資料庫不可存取。

二、初步嘗試:重新執行設定嚮導

嘗試透過 MySQL Instance Configuration Wizard 將資料庫類型改為支援 InnoDB(選擇「Transactional Database Only」),並指定 InnoDB Tablespace 路徑至 D:\MySQL InnoDB Datafiles。但在 Start Service 階段失敗,錯誤代碼 1067:程序意外中止。

嘗試在 services.msc 手動啟動亦失敗。卸載重裝 MySQL 服務、重啟系統後重新配置,雖然服務啟動成功,但在 Applying Security Settings 階段報錯:ERROR 2003: Can't connect to MySQL server on 'localhost' (10061),顯示 MySQL 實際上仍未正常運行。

三、關鍵突破:分析錯誤日誌

錯誤日誌位於 MySQL安裝目錄\Data\電腦名稱.err。第一輪日誌顯示:

InnoDB: Error: log file .\ib_logfile0 is of different size 0 52428800 bytes
InnoDB: than specified in the .cnf file 0 10485760 bytes!
[ERROR] Default storage engine (InnoDB) is not available
[ERROR] Aborting

刪除 ib_logfile0 後重啟,日誌改為:

InnoDB: Log file .\ib_logfile0 did not exist: new to be created
InnoDB: Setting log file .\ib_logfile0 size to 109 MB
InnoDB: Error: log file .\ib_logfile1 is of different size 0 52428800 bytes
InnoDB: than specified in the .cnf file 0 114294784 bytes!
[ERROR] Default storage engine (InnoDB) is not available

同樣刪除 ib_logfile1 後仍失敗,必須重啟作業系統 才能讓新建的 log file 生效。重啟後日誌顯示:

InnoDB: Cannot initialize created log files because
InnoDB: data files are corrupt, or new data files were created when the database was started previous time but the database was not shut down normally

四、最終解決:清除所有 InnoDB 核心檔案

根據官方文件 Error creating InnoDB 提示,刪除資料目錄下所有 ibdata* 與 ib_logfile* 檔案,再重啟服務。日誌終於出現正常啟動序列:

InnoDB: The first specified data file D:\MySQL InnoDB Datafiles\ibdata1 did not exist: new database to be created!
InnoDB: Setting file D:\MySQL InnoDB Datafiles\ibdata1 size to 10 MB
InnoDB: Log file .\ib_logfile0 did not exist: new to be created
InnoDB: Setting log file .\ib_logfile0 size to 109 MB
InnoDB: Progress in MB: 100
InnoDB: Log file .\ib_logfile1 did not exist: new to be created
InnoDB: Setting log file .\ib_logfile1 size to 109 MB
InnoDB: Progress in MB: 100
InnoDB: Doublewrite buffer not found: creating new
InnoDB: Creating foreign key constraint system tables
InnoDB: Foreign key constraint system tables created
InnoDB: Started; log sequence number 0 0
[Note] mysqld-nt: ready for connections. Version: '5.0.18-nt' port: 3306

五、經驗總結與最佳實踐

  1. 務必先備份資料,再進行引擎遷移。
  2. 修改 my.ini 中的 default-storage-engine=InnoDB 並設定 innodb_data_file_path、innodb_log_file_size 等參數,避免使用嚮導產生的不穩定配置。
  3. 遷移前停止服務,手動刪除舊的 ibdata*、ib_logfile*,再啟動服務讓 InnoDB 自動建立乾淨的系統表空間。
  4. 善用 .err 錯誤日誌,它是診斷 InnoDB 啟動失敗的最直接線索。
  5. 配置變更後必須重啟作業系統,單純重啟 MySQL 服務不足以釋放舊的記憶體映射檔案鎖定。

耗時一天終於完成遷移,也練就了閱讀 InnoDB 錯誤日誌的能力。若初期就依照上述最佳實踐操作,可避開絕大多數坑點。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved