1. 程式人生 > >mysql修復表的三個方法

mysql修復表的三個方法

mysql修復表方法 mysql修復表教程

斷電或非正常關機可能導致MySQL數據庫出現表損壞,特別是MyISAM表數據很大的時候。有三種方法,一種方法使用MySQL的repair table的sql語句,另一種方法是使用MySQL提供的myisamchk,,最後一種是mysqlcheck命令行工具。

1.repair table(建議方法,對MyISAM引擎表有用)
(1)check table tabTest;
如果出現的結果說Status是OK,則不用修復,如果有Error,可以用:

(2)repair table tabTest;
進行修復,修復之後可以在用check table命令來進行檢查。在新版本的phpMyAdmin裏面也可以使用check/repair的功能。


2. myisamchk(該工具必須運行在服務終止條件下,對MyISAM引擎表有用)
(1)myisamchk tablename.MYI
進行檢測。

(2)myisamchk -of tablename.MYI
網上說的其它方法:
那麽修復test表的方法為
myisamchk -r -q /var/lib/mysql/db/test.MYI
如果修復全部表,用這個命令
myisamchk -r -q /var/lib/mysql/db/*.MYI


3.運行mysqlcheck命令行工具(該工具可以在服務運行狀態下執行)

轉自:https://blog.csdn.net/orangleliu/article/details/63275154

檢查特定的表:
如果應用中提示某個表壞了,使用下面的命令來檢查。
$ mysqlcheck -c newmandela order -uroot -p
Enter password:
newmandela.order OK
newmandela 是庫名, order是表名,還需要輸入用戶名和密碼

檢查一個庫中的所有表:
$ mysqlcheck -c newmandela -uroot -p
Enter password:
newmandela.account OK
newmandela.alarm OK

newmandela.alarm_settings OK
newmandela.auth_group OK
newmandela.auth_group_permissions OK
newmandela.auth_permission OK

檢查所有庫中的所有表:
全部的庫和表都檢查一遍了。
$mysqlcheck -c --all-databases -uroot -p
Enter password:
apmonitor.acinfo OK
apmonitor.apdailysts OK
apmonitor.apinfo OK
apmonitor.apmonthsts OK
apmonitor.apscanlog OK
apmonitor.auth_group OK

如果只想檢查某幾個庫呢? 可以使用 –databases 參數:
$ mysqlcheck -c --databases newmandela radius -uroot -p
Enter password:
newmandela.account OK
newmandela.alarm OK
newmandela.alarm_settings OK
newmandela.auth_group OK

使用 mysqlcheck 分析表:
$ mysqlcheck -a radius payment_transactionrecord -uroot -p
Enter password:
radius.payment_transactionrecord Table is already up to date

上面的命令 用來分析 radius 庫的 payment_transactionrecord表, -a 表示 analyze
使用 mysqlcheck 優化表:
# mysqlcheck -o radius payment_transactionrecord -uroot -p
Enter password:
radius.payment_transactionrecord OK

-o 代表 optimize ,這裏是優化 radius 庫的 payment_transactionrecord 表
使用 mysqlcheck 修復表:
# mysqlcheck -r radius payment_transactionrecord -uroot -p
Enter password:
radius.payment_transactionrecord OK

-r 代表 repair ,這裏是 修復 radius 庫的 payment_transactionrecord 表
檢查,優化,修復表組合命令:
# mysqlcheck -uroot -p --auto-repair -c -o newmandela
Error: mysqlcheck doesn't support multiple contradicting commands.

上面的命令報錯了,去掉 -c
# mysqlcheck -uroot -p --auto-repair -o newmandela
Enter password:
newmandela.account
note : Table does not support optimize, doing recreate + analyze instead
status : OK
newmandela.alarm
note : Table does not support optimize, doing recreate + analyze instead
status : OK
newmandela.alarm_settings
note : Table does not support optimize, doing recreate + analyze instead
status : OK

每張表都出現了 Table does not support optimize, doing recreate + analyze instead, 代表什麽意思呢? 它的意思不是說 innodb 引擎不支持 優化,可以參考 http://stackoverflow.com/questions/30635603/what-does-table-does-not-support-optimize-doing-recreate-analyze-instead-me 的回答。
mysqlcheck 常用選項

A, –all-databases 表示所有庫
-a, –analyze 分析表
-o, –optimize 優化表
-r, –repair 修復表錯誤
-c, –check 檢查表是否出錯
–auto-repair 自動修復損壞的表
-B, –databases 選擇多個庫
-1, –all-in-1 Use one query per database with tables listed in a comma separated way
-C, –check-only-changed 檢查表最後一次檢查之後的變動
-g, –check-upgrade Check for version dependent changes in the tables
-F, –fast Check tables that are not closed properly
–fix-db-names Fix DB names
–fix-table-names Fix table names
-f, –force Continue even when there is an error
-e, –extended Perform extended check on a table. This will take a long time to execute.
-m, –medium-check Faster than extended check option, but does most checks
-q, –quick Faster than medium check option

mysql修復表的三個方法