db / 未分类 · 2016年7月26日

mysqlcheck mysql检查和修复

Mysqlcheck在mysqld运行时使用,其内嵌check/repair/analyze/optimize table命令;
在执行时相应表被锁定,analyze/check table使用读锁,其它均为写锁;
1. Check a Specific Table in a Database
If your application gives an error message saying that a specific table is corrupted, execute the mysqlcheck command to check that one table.
The following example checks employee table in thegeekstuff database.
# mysqlcheck -c thegeekstuff employee -u root -p
2. Check All Tables in a Database
To check all the tables in a particular database, don’t specify the table name. Just specify the database name.
The following example checks all the tables in the alfresco database.
shell> mysqlcheck -c alfresco -u root -p
3. Check All Tables and All Databases
To check all the tables and all the databases use the “–all-databases” along with -c option as shown below.
# mysqlcheck -c -u root -p –all-databases
If you want to check all tables of few databases, specify the database names using “–databases”.
The following example checks all the tables in thegeekstuff and alfresco database.
# mysqlcheck -c -u root -p –databases thegeekstuff alfresco
Repair Tables using Mysqlcheck
The following repairs employee table that is located in thegeekstuff database.
# mysqlcheck -r thegeekstuff employee -u root -p
Additional Useful Mysqlcheck Options
The following are some of the key options that you can use along with mysqlcheck.
-A, –all-databases Consider all the databases
-a, –analyze Analyze tables
-1, –all-in-1 Use one query per database with tables listed in a comma separated way
–auto-repair Repair the table automatically it if is corrupted
-c, –check Check table errors
-C, –check-only-changed Check tables that are changed since last check
-g, –check-upgrade Check for version dependent changes in the tables
-B, –databases Check more than one databases
-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
-o, –optimize Optimize tables
-q, –quick Faster than medium check option
-r, –repair Fix the table corruption
Mysqlcheck db_name [tbl_name …]
Mysqlcheck –databases db_name
Mysqlcheck –all-databases
–analyze:执行analyze table
–check:执行check table
–check-only-changed:只对上次操作以来变化过的表执行check table
–check-upgrade:执行check table for upgrade
–optimize:执行optimize table
–repair:除了unique key包含重复数据,可以修复其它大部分错误
Analyze table
可用于分区表,alter table … analyze partition
如果自上次analyze以来表没有改动,则analyze table并不会被实际执行;
Check table
For upgrade/quick/fast/medium/extened/changed
对于MyISMA默认为medium,等同于Myisamchk –m tbl_name
可用于分区表,alter table … check partition
第一,常用的 check 命令
mysqlcheck -c -A -uroot -ppassword
mysqlcheck -c -uroot -ppassword --all-databases
mysqlcheck -c --database dbname -uroot -p
mysqlcheck -c -u root -ppassword --all-databases
mysqlcheck -r table # myisam表才可以

第二 恢复
1.首先 导入当天的冷备份 那就回到当然冷备的时候 一般是凌晨3点什么的
2. 查看系统当机时间   –start-datetime 冷备开始时间 ; –stop-datetime 系统当机时间 , mysql-bin.000201 binlog文件 这个也要看对应时间
mysqlbinlog -d games_s25 --start-datetime="2016-07-25 02:05:00" --stop-datetime="2016-07-25 04:40:00" "mysql-bin.000201" | mysql -uroot -ppassword
from http://www.thegeekstuff.com/2011/12/mysqlcheck/
mysqlbinlog 恢复数据的四种方法