萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 資料庫 >> MySQL 教學

MySQL InnoDB 表損壞 #145 錯誤修復:REPAIR TABLE 與 phpMyAdmin 圖形化操作

錯誤現象

斷電後 phpMyAdmin 顯示表「使用中」,查詢報錯:

#145 - Table './shbc/user_order_logs' is marked as crashed and should be repaired

命令行修復流程

  1. 確認損壞:
    mysql> CHECK TABLE user_order_logs;
    +----------------------+-------+----------+---------------------------------------------------------+
    | Table                | Op    | Msg_type | Msg_text                                                |
    +----------------------+-------+----------+---------------------------------------------------------+
    | shbc.user_order_logs | check | warning  | Table is marked as crashed                              |
    | shbc.user_order_logs | check | error    | Size of datafile is: 1368064  Should be: 1368188        |
    | shbc.user_order_logs | check | error    | Corrupt                                                 |
    +----------------------+-------+----------+---------------------------------------------------------+
  2. 執行修復:
    mysql> REPAIR TABLE user_order_logs;
    +----------------------+--------+----------+------------------------------------------------------+
    | Table                | Op     | Msg_type | Msg_text                                             |
    +----------------------+--------+----------+------------------------------------------------------+
    | shbc.user_order_logs | repair | info     | Found block that points outside data file at 1368024 |
    | shbc.user_order_logs | repair | status   | OK                                                   |
    +----------------------+--------+----------+------------------------------------------------------+
  3. 驗證:SELECT * FROM user_order_logs LIMIT 1; 正常返回即修復成功。

phpMyAdmin 圖形化修復

  1. 進入資料庫 → 勾選損壞的表。
  2. 頁面底部下拉選單選擇 Repair table(修復表)。
  3. 執行完成提示 OK。

phpMyAdmin 修復表操作界面


重要說明

  • REPAIR TABLE 僅支援 MyISAM、ARCHIVE、CSV 引擎。
  • InnoDB 不支援 REPAIR TABLE(會報錯)。InnoDB 依靠 崩潰恢復 自動修復;若無法自動恢復,需從備份恢復或使用 innodb_force_recovery 導出數據重建。
  • 本文案例表引擎實為 MyISAM(phpMyAdmin 顯示「使用中」是 MyISAM 特有鎖標記)。

預防建議

  1. 關鍵業務強制使用 InnoDB(支持事務、崩潰恢復)。
  2. 配置 innodb_flush_log_at_trx_commit=1 + sync_binlog=1 保證持久性。
  3. 部署 UPS 電源、定期備份、主從架構。
  4. 監控 Innodb_buffer_pool_pages_dirty、Innodb_log_waits 等指標。
copyright © 萬盛學電腦網 all rights reserved