MySQL錯誤修復記錄:Table xx is marked as crashed and should be repaired
昨晚入睡後,收到鬆哥的 QQ 訊息,說鬆鬆商城開啟報錯,於是手機 QQ 上打開了首頁地址,發現有如下報錯:
MySQL server error report:Array ( [0] => Array ( [message] => MySQL Query Error ) [1] => Array ( [sql] => SELECT u.user_name, og.goods_number, oi.add_time, IF(oi.order_status IN (2, 3, 4), 0, 1) AS order_status FROM `hide_songsong`.`ecs_order_info` AS oi LEFT JOIN `hide_songsong`.`ecs_users` AS u ON oi.user_id = u.user_id, `hide_songsong`.`ecs_order_goods` AS og WHERE oi.order_id = og.order_id AND og.goods_id = 213 ORDER BY oi.add_time DESC LIMIT 50 ) [2] => Array ( [error] => Table 'ecs_users' is marked as crashed and should be repaired ) [3] => Array ( [errno] => 1194 ) )
關鍵報錯資訊:
Table 'ecs_users' is marked as crashed and should be repaired
提示這張表損壞了,必須修復,登陸伺服器之後,開始修復,以下記錄備忘。
1、嘗試使用 myisamchk 命令對所有表索引檔案進行修復:
myisamchk --safe-recover /path/to/*.MYI
- recovering (with sort) MyISAM-table 'ecs_users.MYI' Data records: 2593 - Fixing index 1 - Fixing index 2 - Fixing index 3 - Fixing index 4 - Fixing index 5 *******
2、重新整理首頁依然報錯,於是登陸 MySQL,執行 REPAIR TABLE ecs_users;
mysql> REPAIR TABLE ecs_users; +-------------------------+--------+----------+-------------------------------------------------------------------------------------------------------------+ | Table | Op | Msg_type | Msg_text | +-------------------------+--------+----------+-------------------------------------------------------------------------------------------------------------+ | hide_songsong.ecs_users | repair | error | 1 when fixing table | | hide_songsong.ecs_users | repair | Error | Can't change ownership of the file '/HIDE_songsong/ecs_users.MYD' (Errcode: 1) | | hide_songsong.ecs_users | repair | status | Operation failed | +-------------------------+--------+----------+-------------------------------------------------------------------------------------------------------------+ 3 rows in set (0.02 sec)
3、提示無法修改擁有著屬性,應該是上一步使用 myisamchk 命令時檔案所屬變成了 root 了,於是用 chown 更該擁有者:
chown -R mysql:mysql /HIDE_songsong/*
4、然後繼續登陸 MySQL 執行 REPAIR TABLE ecs_users; 成功:
mysql> use hide_songsong;
Database changed
mysql> REPAIR TABLE ecs_users;
+-------------------------+--------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+-------------------------+--------+----------+----------+
| hide_songsong.ecs_users | repair | status | OK |
+-------------------------+--------+----------+----------+
1 row in set (0.02 sec)
mysql>
在重新整理網站,已經正常:
事後總結:一般這個報錯都是因為資料庫表索引檔案損壞導致的,發現這類錯誤可以嘗試使用 phpMyAdmin 的 Repair 自動修復功能。如果不行,先將資料庫檔案做好備份,然後按照本文的步驟嘗試修復吧。