mysql修複表的三個方法

來源:互聯網
上載者:User

標籤: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命令列工具(該工具可以在服務運行狀態下執行)

轉自: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修複表的三個方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.