昨天由于机房掉电自己的mysql数据库中大量表都出现了无法访问或正使用的情况,后来查了可以使用mysql myisamchk命令进行修复,下面我来介绍一下.
Posted on December 31, 2009 by axl MyISAM是MySQL的预设storage engine. MyISAM table很容易烂掉(corrupted)。
此文章将教你如何检查/修复这些烂掉的MyISAM tables.
每次你在MySQL DB 制造一个 table,将会在档案系统上同时制造出*.frm、*.MYD,跟*.MYI等三种格式的档案.
*.frm = 用来储存资料表格式(file to store table format)
*.MYD(MyData) = 用来储存资料(file to store data)
*.MYI(MyIndex) =用来储存索引(file to store index)
如何检查DB?哪个table 需要修复,用root 执行以下指令,假设要检查DB1下的各个table,代码如下:
#myisamchk /var/lib/mysql/DB1/*.MYI >> /tmp/myisamchk.log
萤幕输出中,如果发现以下字样,该资料表就应修复.
- myisamchk:error:Keypointersandrecordpositionsdoesn’tmatch
- MyISAM-table‘/var/lib/mysql/DB1/news_post_comment.MYI’iscorrupted
- Fixitusingswitch“-r”or“-o”
- myisamchk:warning:1clientisusingorhasn’tclosedthetableproperly
- MyISAM-table‘/var/lib/mysql/DB1/news_post.MYI’isusablebutshouldbefixed
- myisamchk:warning:1clientisusingorhasn’tclosedthetableproperly
- MyISAM-table‘/var/lib/mysql/DB1/news_post_push_log.MYI’isusablebutshouldbefixed
Redirect出来的档案中会显示更多资讯,代码如下:
- CheckingMyISAMfile:/var/lib/mysql/DB1/yam_bbauth.MYI
- Datarecords:14Deletedblocks:0
- -checkfile-size
- -checkrecorddelete-chain
- -checkkeydelete-chain
- -checkindexreference
- -checkdatarecordreferencesindex:1
- -checkrecordlinks
如何利用myisamchk修复烂掉的tables,找出烂掉的table之后,用以下指令修复,代码如下:
- #myisamchk–r/var/lib/mysql/DB1/news_post_comment.MYI
- -recovering(withsort)MyISAM-table‘/var/lib/mysql/DB1/news_post_comment.MYI
- Datarecords:1
- -Fixingindex1
如果table正被某个application使用,你可能会看到,clients are using or haven’t closed the table properly.
所以建议修复前将mysqld关掉或用FLUSH TABLES锁住TABLES,防止修复时有application对DB TABLE做更动.
如何让检查跟修复同时进行,代码如下:
- #myisamchk--silent--force--fast--update-state/var/lib/mysql/DB1/*.MYI
- myisamchk:MyISAMfile/var/lib/mysql/DB1/groups.MYI
- myisamchk:warning:1clientisusingorhasn’tclosedthetableproperly
- myisamchk:MyISAMfile/var/lib/mysql/DB1/profiles.MYI
- myisamchk:warning:1clientisusingorhasn’tclosedthetableproperly
- --options的意义如下:
- •s,–silentoption:Printsonlyerrors.Youcanusetwo-stomakemyisamchkverysilent.
- •-f,–forceoption:Restartmyisamchkautomaticallywithrepairoption-r,ifthereareanyerrorsinthetable.
- •-F,–fastoption:Checkonlytablesthathaven’tbeenclosedproperly.
- •-U–update-stateoption:Markstablesascrashed,whenitfindsanyerror.
修复时手动分配记忆体给庞大的DB,庞大的table,修复需要很长的时间,修复时可以手动增加记忆体参数,代码如下:
- #myisamchk--silent--force--fast--update-state--key_buffer_size=512M--sort_buffer_size=512M
- -read_buffer_size=4M--write_buffer_size=4M/var/lib/mysql/DB1/*.MYI
用myisamchk 获取table资讯,代码如下:
- #myisamchk-dvvprofiles.MYI
- MyISAMfile:profiles.MYI
- Recordformat:Packed
- Characterset:latin1_swedish_ci(8)
- File-version:1
- Creationtime:2007-08-1618:46:59
- Status:open,changed,analyzed,optimizedkeys,sortedindexpages
- Autoincrementkey:1Lastvalue:88
- Datarecords:88Deletedblocks:0
- Datafileparts:118Deleteddata:0
- Datafilepointer(bytes):4Keyfilepointer(bytes):4
- Datafilelength:6292Keyfilelength:6144
- Maxdatafilelength:4294967294Maxkeyfilelength:4398046510079
- Recordlength:2124
- tabledescription:
- KeyStartLenIndexTypeRec/keyRootBlocksize
- 123uniqueint24110241024
- 25765uniquecharpackedstripped120484096
- FieldStartLengthNullposNullbitType
- 111
- 223nozeros
- 35765noendspace
最后一招:#myisamchk --help,MySQL 使用 myisamchk 修复 *.MYI 时,出现下述讯息:
- myisamchk:Diskisfullwriting'/tmp/ST3lJjsW'(Errcode:28).Waitingforsomeonetofreespace...(Expectupto60secsdelayforservertocontinueafterfreeingdiskspace)
- myisamchk:Retryin60secs.Messagereprintedin600secs
解法1:
■由讯息可以知道, myisamchk 需要使用到 /tmp,/tmp 容量不够就会出现此讯息,所以如果 /tmp 可以清出空间,清出 /tmp 的空间就可以解决了,但是如果 /tmp 本身切的就不够大,可以使用下述解法.
解法2:此解法取自: free disk space error using myisamchk
■myisamchk -rq --sort_buffer_size=256M --key_buffer_size=256M --read_buffer_size=32M --write_buffer_size=32M --sort-index --analyze --tmpdir=/more-disk-space-directory/ /var/lib/mysql/database/T1.MYI
■?: 修改 --tmpdir=/more-disk-space-directory/ 指定到空间比较大的地方去即可.