怎樣使用Mysqlcheck來檢查和修復, 優化表的詳解

mysqlcheck 是 MySQL 自帶的一個工具,作用就是保養 表,其實就是檢查,分析,修復和優化了。下面來介紹 mysqlcheck 工具的簡單使用,官方文檔在這里

以下的例子都是基于 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?-pEnter?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?-pEnter?password:  radius.payment_transactionrecord???????????????????Table?is?already?up?to?date

上面的命令 用來分析 radius 庫的 payment_transactionrecord表, -a 表示 analyze

使用 mysqlcheck 優化表

#?mysqlcheck?-o?radius?payment_transactionrecord??-uroot?-pEnter?password:  radius.payment_transactionrecord???????????????????OK

-o 代表 optimize ,這里是優化 radius 庫的 payment_transactionrecord 表

使用 mysqlcheck 修復表

#?mysqlcheck?-r?radius?payment_transactionrecord??-uroot?-pEnter?password:  radius.payment_transactionrecord???????????????????OK

-r 代表 repair ,這里是 修復 radius 庫的 payment_transactionrecord 表

檢查,優化,修復表組合命令

#?mysqlcheck?-uroot?-p?--auto-repair?-c?-o?newmandelaError:??mysqlcheck?doesn't?support?multiple?contradicting?commands.

上面的命令報錯了,去掉 -c

#?mysqlcheck?-uroot?-p?--auto-repair?-o?newmandelaEnter?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 引擎不支持 優化,

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

? 版權聲明
THE END
喜歡就支持一下吧
點贊14 分享