定期主動檢查表狀態,而非等待故障發生。建立預防性維護排程,自動發現並修正潛在問題。
myisamchk/isamchk)#!/bin/sh
cd DATADIR
myisamchk --silent --fast */*.MYI
isamchk --silent */*.ISM
--silent:僅報錯;--fast:跳過未修改表。
#!/bin/sh
datadir=DATADIR
find $datadir -name "*.MYI" -print | xargs myisamchk --silent --fast
find $datadir -name "*.ISM" -print | xargs isamamchk --silent
su mysql
vi check_tables
chmod +x check_tables
./check_tables # 手動測試,理想情況無輸出
su mysql
crontab -l > /tmp/entries
echo "0 0 * * 0 /usr/local/mysql/bin/check_tables" >> /tmp/entries
crontab /tmp/entries
每週日 00:00 執行,有錯誤才發郵件通知(因 --silent)。
在啟動 MySQL 服務前呼叫 check_tables。
建立 mysql.check 腳本處理 start/stop 參數:
#!/bin/sh
case "$1" in
start)
echo -n "Checking MySQL tables:"
if [ -x /usr/local/mysql/bin/check_tables ]; then
/usr/local/mysql/bin/check_tables
fi;;
stop) ;;
*) echo "Usage: $0 {start|stop}"; exit 1;;
esac
exit 0
安裝連結:S98mysql.check → mysql.check(早於 S99mysql.server)。
Linux 綜合 BSD/System V,建議使用 rc.local 簡單法。
myisamchk,使用 CHECK TABLE SQL 指令ANALYZE TABLE 更新統計資訊,優化查詢計劃