mysqlcheck 是 MySQL 自帶的一個工具,作用就是
保養表,其實就是檢查,分析,修復和優化了。下面來介紹 mysqlcheck 工具的簡單使用,官方文檔在這里原文網址:http://blog.csdn.net/orangleliu/article/details/63275154
以下的例子都是基於 MySQL 5.6 版本運行狀態下(mysqlcheck是個在線工具), 不同的存儲引擎對於這個命令的支持程度不同(指的是 check, repair, analyze, optimize),下面內容偏於操作,主要基於 innodb 引擎。
提示:OPTIMIZE 在大表時候可能會消耗很多時間,不清楚原理情況下請謹慎使用!!! innodb 一般不用 OPTIMIZE,請參見 Using MySQL OPTIMIZE tables? For InnoDB? Stop
檢查特定的表
注意在shell中執行,不是在mysql的交互環境下
如果應用中提示某個表壞了,使用下面的命令來檢查。
$ 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-1Use one query per database with tables listed in a comma separated way-C, –check-only-changed檢查表最后一次檢查之后的變動-g, –check-upgradeCheck for version dependent changes in the tables-F, –fastCheck tables that are not closed properly–fix-db-namesFix DB names–fix-table-namesFix table names-f, –forceContinue even when there is an error-e, –extendedPerform extended check on a table. This will take a long time to execute.-m, –medium-checkFaster than extended check option, but does most checks-q, –quickFaster than medium check option
