MySQL数据库中部分数据损坏恢复过程,MySQL数据库中部分数据损坏恢复过程,MySQL
1 c+ W+ ]' ^* v" I3 T) D
7 s/ a* y: y. k4 @ E& `; F7 | 前几天因为MySQL数据库部分数据损坏原因,我尝试了下恢复数据,之后整理以下文档,供各位参考,以备各位同事以后如有类似问题,可以少走些弯路,尽快解决问题。
, a; Z+ k$ C1 `- f) o; e环境:Windows2003
) v* @2 X0 x) C1 b% k' T0 k
数据库:MySQL
, _+ m5 z9 l2 h1 m' K
损坏数据文件名:function_products
: b+ Y* W3 x+ b 将数据库内容物理文件直接导入到mysql\data下,每只表各3个文件,依次分别为:.frm .myd .myi。首先我第一想到的是去网上搜索,寻找类似的工具,试图通过工具来恢复已损坏的文件,于是我在google上查找,找到一款名为mysqlrecovery的工具,安装后我用其进行恢复,只可惜效果太不理想,几十MB大的数据文件,恢复之后它提示我竟然只有几十KB。我又想到了MySQL下应有自己本身的修复程序等,于是想通过其来进行恢复,心想应不会太差劲吧,在网上查找了资料,提示:由于临时断电,使用kill -9中止MySQL服务进程,或者是MySQL正在高速运转时进行强制备份操作时等,所有的这些都可能会毁坏MySQL的数据文件。如果在被干扰时,服务正在改变文件,文件可能会留下错误的或不一致的状态。因为这样的毁坏有时是不容易被发现的,当你发现这个错误时可能是很久以后的事了。
3 u; a# y. u* W9 n% a* z% Q
于是,当你发现这个问题时,也许所有的备份都有同样的错误。我想我现在碰到的问题可能是这个问题,因为备份的数据也是有部分损坏的数据,所以导致不能完全运行, 意识到myisamchk程序对用来检查和修改的MySQL数据文件的访问应该是唯一的。如果MySQL服务正在使用某一文件,并对myisamchk正在检查的文件进行修改,myisamchk会误以为发生了错误,并会试图进行修复--这将导致MySQL服务的崩溃!这样,要避免这种情况的发生,通常我们需要在工作时关闭MySQL服务。作为选择,你也可以暂时关闭服务以制作一个文件的拷贝,然后在这个拷贝上工作。当你做完了以后,重新关闭服务并使用新的文件取代原来的文件(也许你还需要使用期间的变更
日志)。
" t5 c M! A+ }, A MySQL数据目录不是太难理解的。每一个数据库对应一个子目录,每个子目录中包含了对应于这个数据库中的数据表的文件。每一个数据表对应三个文件,它们和表名相同,但是具有不同的扩展名。tblname.frm文件是表的定义,它保存了表中包含的数据列的内容和类型。tblname.myd文件包含了表中的数据。tblname.myi文件包含了表的索引(例如,它可能包含lookup表以帮助提高对表的主键列的查询)。 要检查一个表的错误,只需要运行myisamchk(在MySQL的bin目录下)并提供文件的位置和表名,或者是表的索引文件名:
+ u L. o8 K g+ C0 d0 G% myisamchk /usr/local/mysql/var/dbname/tblname
5 C6 H$ M8 E7 p$ A
% myisamchk /usr/local/mysql/var/dbname/tblname.myi
- n0 u, f# t' h% ] @! h7 c上面的两个命令都可以执行对指定表的检查。要检查数据库中所有的表,可以使用通配符:
6 p* K8 \2 b1 C/ ?6 [( }
% myisamchk /usr/local/mysql/var/dbname/*.myi
) u& b. @; a6 l$ w$ q
要检查所有数据库中的所有表,可以使用两个通配符:
0 n7 r# @$ V: a) q$ a& V' _. `( M
% myisamchk /usr/local/mysql/var/*/*.myi
5 M0 h' l: Q+ m0 ]; Q j* W如果不带任何选项,myisamchk将对表文件执行普通的检查。如果你对一个表有怀疑,但是普通的检查不能发现任何错误,你可以执行更彻底的检查(但是也更慢!),这需要使用--extend-check选项:
5 P) N$ {( u# `/ T: | \
% myisamchk --extend-check /path/to/tblname
; c( A+ @- @2 O/ Z+ j% \' j
对错误的检查是没有破坏性的,这意味着你不必担心执行对你的数据文件的检查会使已经存在的问题变得更糟。另一方面,修复选项,虽然通常也是安全的,但是它对你的数据文件的更改是无法撤消的。因为这个原因,我们强烈推荐你试图修复一个被破坏的表文件时首先做个备份,并确保在制作这个备份之前你的MySQL服务是关闭的。
1 n" m4 @6 Y6 E" U# x& f我在Win2003下通过命令提示符,输入:
6 B$ ^' W& z1 j
( |+ q3 _4 j+ z. o
注:此为记录我当时操作的全部过程
! Q0 W( }7 `" Y$ r! ]/ F: s3 R
d:\documents and settings\administrator>c:
9 k, r- f, f7 R/ p9 @! c! c' Ic:\>cd MySQL
* \5 {$ w: F% B8 `' ~: z
c:\mysql>cd data
" m# n& b/ r ~' A( F" Sc:\mysql\data>cd hw_enterprice
, l7 g+ U6 F2 B6 x4 z, Fc:\mysql\data\hw_enterprice>myisamchk function_products.frm
4 O( v5 l9 _6 c2 e+ v; a' T'myisamchk' 不是内部或外部命令,也不是可运行的程序或批处理文件。
, j* @ t* l& c* ?3 _8 O, x" ac:\mysql\data\hw_enterprice>cd\
& d, \/ c+ b6 B) P; ?. d9 T1 pc:\>cd mysql
* Z' Q+ m4 _* Q# H5 |' oc:\mysql>cd bin
$ ?! X( z( Z D% m4 b7 x注:查看myisamchk的帮助信息
& ~! W' F* a6 s* H$ P ?: e! u( U
c:\mysql\bin>myisamchk
# `- b3 n4 i. M7 V4 [6 L; D o) q
myisamchk ver 2.6 for win95/win98 at i32
* z& Q( H( N N& |by monty, for your professional use
; x( }5 n* i4 O! s1 i! c
this software comes with no warranty: see the public for details.
k5 l7 c6 Q K' k9 X i3 [
description, check and repair of isam tables.
7 x B6 ], l; l5 u, b% I" Q9 h! ?
used without options all tables on the command will be checked for errors
0 ~* u6 h3 ~# M8 L
usage: myisamchk [options] tables[.myi]
' x' b& X I. gglobal options:
- ~: a8 c% V9 U m8 |# ^
-#, --debug=... output debug log. often this is 'd:t

,filename'
* X% @" L0 d. a5 o. _ m; V -?, --help display this help and exit.
. W1 K; v' _3 x; \
-o, --set-variable var=option
0 F0 d% A& P( d" A k8 { change the value of a variable. please note that
$ t: f5 o6 U3 K+ }& Z- I, i% Z this option is deprecated; you can set variables
4 _+ b0 O/ G2 R, [* V" B directly with '--variable-name=value'.
: g& y! t6 q; c3 D9 ~# |, r' u
-t, --tmpdir=path path for temporary files
) o% {; |( j4 S6 H" `+ H
-s, --silent only print errors. one can use two -s to make
+ |8 J# e* ]. g myisamchk very silent
5 t+ c, j' {/ v -v, --verbose print more information. this can be used with
1 a) Y' _' w5 U1 G/ |& Q
--description and --check. use many -v for more verbosity!
* b$ T- O6 s) M( R -v, --version print version and exit.
' U8 z' u9 s( ~$ @ -w, --wait wait if table is locked.
! y h. v7 z% i$ v$ |# o8 o4 D% T( h
check options (check is the default action for myisamchk):
) x4 ]# \' R$ Q5 O- P8 {
-c, --check check table for errors
$ B! H# Y4 h: k( a
-e, --extend-check check the table very throughly. only use this in
* v; l; j0 b1 i+ X. J8 }& U
extreme cases as myisamchk should normally be able to
- L) @1 G, S0 N; P* Q3 m0 g
find out if the table is ok even without this switch
0 |9 Y6 h. \# o
-f, --fast check only tables that haven't been closed properly
4 m( b/ L3 {: J `. x! o
-c, --check-only-changed
2 c. \' T- n! c9 X' M9 K: Q9 }7 y check only tables that have changed since last check
+ j! W2 c+ a7 I- ?) R# V -f, --force restart with '-r' if there are any errors in the table.
4 D8 M5 X, d7 H6 a
states will be updated as with '--update-state'
2 K* _, ^8 j0 n: h0 b: J
-i, --information print statistics information about table that is checked
& k2 E1 R0 m6 O8 m
-m, --medium-check faster than extend-check, but only finds 99.99% of
* I+ T3 O2 M2 @3 @ all errors. should be good enough for most cases
2 b6 G8 Y& j1 l8 k( c4 [5 D+ [
-u --update-state mark tables as crashed if you find any errors
; P( B" h0 V0 h! p9 e. Y* R& v9 f -t, --read-only don't mark table as checked
" J" r: V. s6 r u$ u( \. r& L1 urepair options (when using '-r' or '-o')
' D8 j: u% y$ M3 Y
-b, --backup make a backup of the .myd file as 'filename-time.bak'
! H. H9 l' |9 `( }- n/ r
--correct-checksum correct checksum information for table.
: B" h- `* `1 c& v/ c% f G
-d, --data-file-length=# max length of data file (when recreating data
1 M) h% E. t% y4 t0 k: Z( b6 s file when it's full)
' R( K3 K; C; G) l' I
-e, --extend-check try to recover every possible row from the data file
: z- g( O0 g$ _( { normally this will also find a lot of garbage rows;
: W; Z: f. _/ Z+ }0 @6 @+ [ don't use this option if you are not totally desperate.
, N7 J5 V) w2 D* {( @9 \ -f, --force overwrite old temporary files.
& j3 s1 C1 _2 B' L5 ?
-k, --keys-used=# tell myisam to update only some specific keys. # is a
6 A" {3 f) l$ M( N" U4 d) H
bit mask of which keys to use. this can be used to
' _2 H- M/ Y% T3 k/ z' D6 L
get faster inserts!
$ e5 Q5 K* X3 W -r, --recover can fix almost anything except unique keys that aren't
+ j) H) Q3 d$ A: v% s' z unique.
0 P4 F/ X; {8 j2 O8 z
-n, --sort-recover forces recovering with sorting even if the temporary
, c4 i% @( m8 u9 E: X1 ~, G
file would be very big.
8 M0 \) x/ n3 D1 k
-p, --parallel-recover
% G! H, f: t' }6 d L/ P8 `# S
uses the same technique as '-r' and '-n', but creates
4 L( h, u: n2 o all the keys in parallel, in different threads.
. {. `' y- K7 z" l2 r% J$ X0 e8 N; V
this is alpha code. use at your own risk!
{! R1 p x j/ d; E* e% K
-o, --safe-recover uses old recovery method; slower than '-r' but can
2 d, P2 d9 R1 l6 a. s
handle a couple of cases where '-r' reports that it
! U1 S2 L* }- T/ a3 J. ~
can't fix the data file.
8 m1 G1 s# Q: V+ e4 f$ k$ I; M; X3 D
--character-sets-dir=...
8 P: |: B" j7 p6 E3 I directory where character sets are
. U; |- @7 ~# R' C* y
--set-character-set=name
, F# u! }# Z( u$ l' ]
change the character set used by the index
- O3 a' L D2 U" S' Q4 o5 ] -q, --quick faster repair by not modifying the data file.
; M6 D$ A$ k% ^, \! V' Q7 c one can give a second '-q' to force myisamchk to
4 L& A8 o4 y6 N: F( Y modify the original datafile in case of duplicate keys
* c1 B7 B: X9 o* h- @0 d
-u, --unpack unpack file packed with myisampack.
' A& d7 n6 g; s6 K: T1 c" xother actions:
! Z' L9 E5 Z3 X( }
-a, --analyze analyze distribution of keys. will make some joins in
8 `' C# S+ `- T
MySQL faster. you can check the calculated distribution
/ \& V! F/ `' L1 b by using '--description --verbose table_name'.
* Q2 |( v- K: @+ }1 S0 a
-d, --description prints some information about table.
: y) Q0 ], ^: o& A" ~" E& i1 c -a, --set-auto-increment[=value]
1 K& f+ s. }5 O5 O( q# w4 t% J
force auto_increment to start at this or higher value
& x# J) A0 v' l5 c: N2 b$ x if no value is given, then sets the next auto_increment
. q- {- t: w6 b4 [* e0 O8 m value to the highest used value for the auto key 1.
. @' a: Y2 m4 m" V# D0 L -s, --sort-index sort index blocks. this speeds up 'read-next' in
8 k$ c5 y" W6 {# d. A2 i2 Q0 ^( ^ applications
, u2 H; T. _+ u
-r, --sort-records=#
, A" r- j! e1 P0 C# [
sort records according to an index. this makes your
8 O/ j; a: m$ k6 |9 X" r( q/ [
data much more localized and may speed up things
( U0 I8 E+ y' l- y! {c:\mysql\bin>myisamchk c:\mysql\data\hw_enterprice\function_products.frm
' C' J5 x3 ^2 W) _& |
myisamchk: error: 'c:\mysql\data\hw_enterprice\function_products.frm' is not a m
- h- E: i8 b9 G5 ayisam-table
! i: j1 r0 q. z! y* d1 d. T( B
c:\mysql\bin>myisamchk c:\mysql\data\hw_enterprice\function_products.myi
1 V' A* B3 x( c" Achecking myisam file: c:\mysql\data\hw_enterprice\function_products.myi
8 Q/ h* D8 F8 Ldata records: 85207 deleted blocks: 39
7 Y+ g* O; X$ F8 r0 b
myisamchk: warning: table is marked as crashed
: s d+ T: ]+ y, n6 v% g Q
myisamchk: warning: 1 clients is using or hasn't closed the table properly
) S6 T$ ?/ R& n X3 G
- check file-size
1 [( I! {6 h+ Z X
- check key delete-chain
& ^0 [5 y4 ]/ H6 c
- check record delete-chain
5 M5 y- O0 p( K6 D9 Cmyisamchk: error: record delete-link-chain corrupted
, v+ q1 W4 r& m1 K+ }
- check index reference
8 P2 J7 \3 x! b+ \, A. e6 P
- check data record references index: 1
. d' w4 E- z; R& t- q# O+ l
- check data record references index: 2
* p; {& u7 d0 |: R3 p( z" ~3 W
- check data record references index: 3
9 Y* p! K) W/ f- check record links
8 ~5 C; `$ i7 G. N2 L4 lmyisamchk: error: wrong bytesec: 0-195-171 at linkstart: 841908
! ~# n! W& H* S' s) t7 r) xmyisam-table 'c:\MySQL\data\hw_enterprice\function_products.myi' is corrupted
4 C& N# ]3 d; g0 g$ S9 A
fix it using switch "-r" or "-o"
* ^4 [% d/ v1 m d r+ X, o. j% u3 x
+ l; M7 R" k$ |+ i$ V, b3 a. |) x继续进行操作:
+ B3 o6 X9 Y. g$ L2 ]
F; h2 S) ~! {& S
c:\MySQL\bin>myisamchk --recover --quick c:\MySQL\data\hw_enterprice\function_p
; K; G: c. w9 P) Q Broducts.myi
8 O C2 d" y% h
- check key delete-chain
" Z; k$ A; H t/ [4 g2 D1 z- check record delete-chain
4 U& M) B7 p# `8 m7 F, x0 X, T
myisamchk: error: record delete-link-chain corrupted
/ M% s# w1 p! O6 w smyisamchk: error: quick-recover aborted; run recovery without switch 'q'
4 ]3 e) p7 P" C) L
updating myisam file: c:\MySQL\data\hw_enterprice\function_products.myi
! R! ?. i* }' y1 m. L: I! _myisam-table 'c:\MySQL\data\hw_enterprice\function_products.myi' is not fixed be
3 x4 G: R3 R+ D: A7 |. c2 \- m
cause of errors
3 M+ b) y# o( v4 u3 M
try fixing it by using the --safe-recover (-o) or the --force (-f) option
5 K4 u3 [* W- r" j5 l / c& V: w$ s0 z
系统提示我使用--safe-recover (-o) or the --force (-f) option进行修复操作,于是
6 V+ l8 I' L9 K) h3 F
1 J! L' i( C+ z. sc:\MySQL\bin>myisamchk --safe-recover c:\MySQL\data\hw_enterprice\function_prod
0 ~; {: f! I8 @ |/ R! K
ucts.myi
. R* h! _7 R5 P4 H9 p* x2 M4 B
- recovering (with keycache) myisam-table 'c:\MySQL\data\hw_enterprice\function_
7 }3 t$ f+ X2 g: L% k! q
products.myi'
g. J7 W+ y c
data records: 85207
" Y. n, D0 Z4 q5 |% w+ j- w+ t
wrong bytesec: 0-195-171 at 841908; skipped
9 }- S. e* K s2 z! Edata records: 85215
, A8 \# v5 S0 y' Z% P# a
2 O* [* J$ a( j8 e8 L+ K) ^ 将修复后的物理文件复制到mysql\data下之后,通过phpmyadmin进行访问,正常!本次数据修复操作成功,数据已被正常恢复,总计85215条记录,其中恢复数据共计85207条。
$ f2 h; ~1 }0 L+ n( x1 y
总结本次经验及查找资料,如下:
7 r) M- H0 p- q' g- O$ f1 j
当你试图修复一个被破坏的表的问题时,有三种修复类型。如果你得到一个错误信息指出一个临时文件不能建立,删除信息所指出的文件并再试一次--这通常是上一次修复操作遗留下来的。
% H1 o- G: `- q. L
这三种修复方法如下所示:
) j& k6 Y* L' N0 ]# X1 e* h5 W9 s$ F9 ]9 ]( y2 D
% myisamchk --recover --quick /path/to/tblname
. H4 C9 i4 M$ \6 m$ j+ ]3 F) A
% myisamchk --recover /path/to/tblname
4 a* x1 ?* I5 h3 y; A. \
% myisamchk --safe-recover /path/to/tblname
. K- |. H/ P" u2 k4 \( q4 A
* ^( m8 z& P( z6 l5 W4 m ?+ U) h- X( d
第一种是最快的,用来修复最普通的问题;而最后一种是最慢的,用来修复一些其它方法所不能修复的问题。
$ p8 G5 E! v6 e2 g, K 如果上面的方法无法修复一个被损坏的表,在你放弃之前,你还可以试试下面这两个技巧:
9 b8 P! E% U* }7 ^ 如果你怀疑表的索引文件(*.myi)发生了不可修复的错误,甚至是丢失了这个文件,你可以使用数据文件(*.myd)和数据格式文件(*.frm)重新生成它。首先制作一个数据文件(tblname.myd)的拷贝。重启你的MySQL服务并连接到这个服务上,使用下面的命令删除表的内容:
7 N: f/ n/ J% s6 LMySQL> delete from tblname;
/ }6 d8 O' D$ p q 在删除表的内容的同时,会建立一个新的索引文件。退出登录并重新关闭服务,然后用你刚才保存的数据文件(tblname.myd)覆盖新的(空)数据文件。最后,使用myisamchk执行标准的修复(上面的第二种方法),根据表的数据的内容和表的格式文件重新生成索引数据。
r( y/ n# h6 n; [9 a 如果你的表的格式文件(tblname.frm)丢失了或者是发生了不可修复的错误,但是你清楚如何使用相应的create table语句来重新生成这张表,你可以重新生成一个新的.frm文件并和你的数据文件和索引文件(如果索引文件有问题,使用上面的方法重建一个新的)一起使用。首先制作一个数据和索引文件的拷贝,然后删除原来的文件(删除数据目录下有关这个表的所有记录)。
" k4 L" v" g3 n; |2 p) s
启动MySQL服务并使用当初的create table文件建立一个新的表。新的.frm文件应该可以正常工作了,但是最好你还是执行一下标准的修复(上面的第二种方法)。
/ W5 w) R; Q" ~& z, ]/ U8 H% n
如果有类似问题,建议自己先分析问题根源,查找资料,自己动手解决,不但可以多学更多知识技巧,更重要的是,自己也在解决问题的同时得到了快乐。