Database

Mysql DB 가 깨졌을때 살리는법 (myisamchk)

페이지 정보

본문

어느날 아침 로긴이 안되거나 혹은 게시판에서 데이터 베이스 엑세스를 할수 없습니다.. 이런 메세지를 볼수 있다. 난감하기 그지 없다... 어떻게 극복을 할것인가... ?
데이터 베이스는 항상 깨지기 마련이다. 1년은 운영한다면.. 적어도 1~2회 정도는 화일을 열수 없습니다.


에러 메세지 :
ERROR 1016: Can't open file: 'xxxxxx.MYD'. (errno: 145)

만약 /usr/local/mysql/var/데이터 베이스에 들어 가면 각각의 부분에 대하여
*.FRM :  테이블 정보
*.MYI  : 인덱스 정보
*.MYD : 데이타 정보

[root@ilinuxbay ]# /usr/local/mysql/bin/myisamchk sessions.*
myisamchk: error: 'sessions.MYD' is not a MyISAM-table

---------

Checking MyISAM file: sessions.MYI
Data records:      6  Deleted blocks:      6
myisamchk: warning: Table is marked as crashed
- check file-size
- check key delete-chain
- check record delete-chain
myisamchk: warning: Not used space is supposed to be: 896 but is: 624
myisamchk: error: record delete-link-chain corrupted
- check index reference
- check data record references index: 1
myisamchk: error: Found 4 keys of 6
- check record links
myisamchk: error: Record-count is not ok; is 4            Should be: 6
myisamchk: warning: Found          4 deleted blocks      Should be: 6
myisamchk: warning: Found          8 parts                Should be: 11 parts
MyISAM-table 'sessions.MYI' is corrupted
Fix it using switch "-r" or "-o"

---------

누가 지웠나.. 혹은 화일이 없는 경우 다음과 같은 명령어를 이용 확인 복구를 할 수 있다.


]#myisamchk  옵션을 보면

# -r -o로 복구가 안되면다 -f 옵션을 이용한다.
-f, --force        Overwrite old temporary files.

#복구
-r, --recover      Can fix almost anything except unique keys that aren't unique.

#안전하게 복구
-o, --safe-recover  Uses old recovery method; Slower than '-r' but can
                      handle a couple of cases where '-r' reports that it
                      can't fix the data file.

#데이터 정렬 및 속도 증가 -R1 저도 주면 될듯 싶네요
-R, --sort-records=#
                      Sort records according to an index.  This makes your
                      data much more localized and may speed up things

일반적으로 -r 을 기본으로 사용하며 대상 화일은 * 확장자를 잡으면 된다.
그럼 복구를 해보자.


[root@ilinuxbay ]# /usr/local/mysql/bin/myisamchk -r sessions.*
myisamchk: error: 'sessions.MYD' is not a MyISAM-table

---------

- recovering (with sort) MyISAM-table 'sessions.MYI'
Data records: 6
- Fixing index 1
Data records: 4

---------

혹 구버전인 경우 isamchk 명령어를 이용 복구 가능합니다. 가능한 데이터베이스를 정지하고 하세요.
그럼.. 무운을 빕니다. 전 복구 했어요.








얼마전 그런 비슷한 일이 있었는데 특정 테이블에 대해서만 유독 에러가 발생하더군요. MySQL은 테이블당 파일을 생성하니까 역시 Can't Open Error 가 발생했구요. 다음과 같은 쿼리를 날려서 해결했습니다.

REPAIR TABLE 테이블

실제 사용법은
REPAIR TABLE 테이블1,테이블2,테이블3

과 같이 쓸 수 있고, MyISAM 타입에만 적용됩니다. 매뉴얼 보니 "myisamchk -r 테이블명" 과 같은 작업을 한다고 하네요.
매뉴얼만 보면 다 아는 사실일텐데 혹시 모르시는 분이 있을까 싶어 사족을 붙였습니다. :)




저도 경험한 바에 의하면 MySQL에서 특정 테이블이 깨지는 경우는 running중인 서버의 데이타 파일을 OS상에서 카피하거나 압축할 경우에 많이 그러더군요. 데이타 파일을 카피하거나 압축할때는 반드시 mysqlhotcopy 명령을 써야 하더군요.

관련자료

nuno님의 댓글

  복구 방법

 

1. 우선 해당 테이블에 lock 을 걸어놓는다.


 lock tables 테이블명 write;
 cf) mysql>lock tables zetyx_board_applecafe write;

 

2. mysql 의 복구 명령어를 이용하여 테이블을 복구한다.


 /usr/local/mysql/bin/myisanchk -acu 해당 유저의 해당테이블명
 cf) $ ./myisamchk - acu swingme/board.MYI

(myisamchk -r 옵션보다는 -acu 명령이 확실!!)


3. lock을 걸어놓은 테이블을 해제한다.


 unlock tables;

 

4. 그래도 안된다면 mysql 에 접속하여 repair 명령으로 복구한다.
 repair table 테이블명 으로 복구한다.


Today's proverb
친구들에게서 기대하는 것을 친구들에게 베풀어야 한다. (아리스토텔레스)