MySQL/Table복구]MySQL의 Table(MyISAM)손상과 복구 - myisamchk > MySQL Tip

본문 바로가기
 

MySQL/Table복구]MySQL의 Table(MyISAM)손상과 복구 - myisamchk

페이지 정보

작성자 no_profile 차동박 쪽지보내기 메일보내기 홈페이지 자기소개 아이디로 검색 전체게시물 댓글 0건 조회 40,650회 작성일 09-08-16 12:59

본문

MySQL을 운영하다보면 여러가지 이유로 Table에 손상을 입어 MySQL을 사용할 수 없을 때가 종종 발생한다.
참 당황스럽기도 하고, 처음 접할때는 눈앞이 노랗게 보이기도 한다.
그렇지만 무엇보다 중요한 것은 침착함이다.
경험을 할수록 침착해지고 잘 대처 할 수 있어 지기는 하지만...가능하면 일어나지 말았으면 하는 일이다.

이런 일에 대처하기 위해
1. 왜 Table이 문제가 생기는지?
2. 어떻게 복구해야 하는지?
3. 어떻게 예방해야 하는지?

알아보고자 한다.


1. Table(MyISAM) 손상이 발생하는 원인

 MyISAM Table은 SQL문장이 리턴 되기전에 모두 기록되기 때문에 믿을만 하다고 mysql.com에서는 밝히고 있으며 다음과 같은 이유로 문제가 발생 할 수 있다고 한다.

1) 기록(write)되는 중간에 mysqld이 죽었을 때.

2) 갑작스럽게(의도하지 않은) 컴퓨터가 shutdown 되었을 때.

3) 하드웨어 오류

4) 외부프로그램에 의해 Table을 변경하는 동시에 서버에 의해 변경작업이 이뤄질 때.

5) MySQL이나 MyISAM코드의 버그.


전형적인 증세는 다음과 같다.

1) Table로 부터 데이터를 Select할때 아래와 같은 error가 발생.
    Incorrect key file for table: '...'. Try to repair it

2) rows를 찾을 수 없거나 불완전한 결과를 반환.




2. 어떻게 복구해야 하는지?

 MyISAM Table의 상태는 CHECK TABLE 쿼리로 확인 할 수 있으며, REPAIR TABLE로 깨진(corrupted) MyISAM Table을 복구 할 수 있다. 만약 mysqld이 실행중이지 않다면, myisamchk명령어를 사용해 check와 복구를 할 수 있다.

자세한 정보는 아래 링크를 참조하기 바란다.
http://dev.mysql.com/doc/refman/6.0/en/check-table.html
http://dev.mysql.com/doc/refman/6.0/en/repair-table.html
http://dev.mysql.com/doc/refman/6.0/en/myisamchk.html


 myisamchk는 repair table와 check table을 사용하는 것 보다 빠른 속도를 제공하지만 몇가지 주의를 해야 한다.
myisamchk는 반드시 mysqld을 죽이거나 해당 Table에 Lock을 걸어놓고 실행해야 한다. 그리고 Lock을 건 경우 반드시 Lock을 다시 풀어주어야 한다.

쿼리(repair table와 check table)에 의한 체크와 복구는 위의 링크를 통해 쉽게 이해 할 수 있으므로 여기에서는 myisamchk를 이용한 복구에 대해서 알아보고자 한다.

 myisamchk는 database의 table들의 정보를 가져오거나 체크(check), 복구(repair), 최적화(optimize)등을 할 수 있는 유틸리티이다. 이는 MyISAM Table에서만 사용이 가능하고, Patition Table에서의 사용은 지원하지 않는다.
(Table들은 각각 .MYD.MYI파일을 가지며, 각각 데이터와 인덱스를 위한 파일이다.)

사용법
shell> myisamchk [options] tbl_name ...

옵션에 대해서는 myisamchk --help를 통해 알 수 있다.
tbl_name은 체크나 복구를 원하는 Table의 이름이다.

가장 간단한 사용법은 정보를 보는 다음과 같은 명령이다.

shell> myisamchk user
Checking MyISAM file: user
Data records:       8   Deleted blocks:       0
- check file-size
- check record delete-chain
- check key delete-chain
- check index reference
- check data record references index: 1
- check record links


shell> myisamchk -d user
MyISAM file:         user
Record format:       Packed
Character set:       latin1_swedish_ci (8)
Data records:                    8  Deleted blocks:                 0
Recordlength:                  344
table description:
Key  Start  Len  Index    Type
1     1       180   unique  char packed stripped
      181     48               char stripped     

아무런 옵션이 없으면 기본적으로 대상 Table을 check한다.
-d 옵션은 Table의 간략한 정보를 출력하라는 의미이다.

[myisamchk -d user]는 [myisamchk -d user.MYI]로 하는 것과 동일하다.

이는 하나의 Table을 대상으로 할때 Table의 이름만 써도 되는 것을 의미 한다. 여러개의 테이블을 함께 체크하기 위해서는
myisamchk -d *.MYI
와 같은 형식으로 사용하면 된다. 물론 다른 디렉토리에 파일이 있다면 경로를 적어 주어야 한다.


주의 할 사항을 위에서도 잠시 언급하였지만 다시 한번 아래와 같이 정리를 하고자 한다.

주의 :
 myisamchk가 동작하는 동안 Table을 사용하는 다른 프로그램(주로 mysqld)이 있어서는 안된다. 즉, myisamchk가 동작하는 동안는 누구도 그 Table에 접근하지 않는 것이 좋다는 것이다. MySQL server를 종료하거나 모든 Table들을 Lock을 거는 것이 가장 좋다.
 간혹, myisamchk가 동작중에 다음과 같은 error가 발생할때가 있다.
warning: clients are using or haven't closed the table properly

이는 다른 프로그램에 의해 아직 파일이 닫히지 않았거나 닫기 없이 죽었을 때 그 Table을 대상으로 myisamchk를 돌렸다는 의미이다. 때때로 이런 것들이 MyISAM Table을 손상시키기도 한다.
만약 mysqld가 동작중이라면, Table의 변경사항이 없도록 하거나, 메모리에 있는 버퍼를 FLUSH TABLES를 사용해 비워 주어야 한다.
그러나 myisamchk를 이용해 check만을 할 경우 가장 쉬운 방법은 mysql내에서 CHECK TABLE을 사용하는 것이다.

다음은 myisamchk의 옵션들이다.

Table 4.9. myisamchk Option Reference

Format Config File Description Introduction --analyze analyze Analyze the distribution of key values   --backup backup Make a backup of the .MYD file as file_name-time.BAK   --block-search=offset block-search Find the record that a block at the given offset belongs to   --check check Check the table for errors   --check-only-changed check-only-changed Check only tables that have changed since the last check   --correct-checksum correct-checksum Correct the checksum information for the table   --data-file-length=len data-file-length Maximum length of the data file (when re-creating data file when it is full)   --debug[=debug_options] debug Write a debugging log   decode_bits=# decode_bits Decode_bits   --description description Print some descriptive information about the table   --extend-check extend-check Do a repair that tries to recover every possible row from the data file   --extended-check extended-check Check the table very thoroughly   --fast fast Check only tables that haven't been closed properly   --force force Do a repair operation automatically if myisamchk finds any errors in the table   --force force-recover Overwrite old temporary files. For use with the -r or -o option   ft_max_word_len=# ft_max_word_len Maximum word length for FULLTEXT indexes   ft_min_word_len=# ft_min_word_len Minimum word length for FULLTEXT indexes   ft_stopword_file=value ft_stopword_file Use stopwords from this file instead of built-in list   --HELP   Display help message and exit   --help   Display help message and exit   --information information Print informational statistics about the table that is checked   key_buffer_size=# key_buffer_size The size of the buffer used for index blocks for MyISAM tables   --keys-used=val keys-used A bit-value that indicates which indexes to update   --max-record-length=len max-record-length Skip rows larger than the given length if myisamchk cannot allocate memory to hold them   --medium-check medium-check Do a check that is faster than an --extend-check operation   myisam_block_size=# myisam_block_size Block size to be used for MyISAM index pages   --parallel-recover parallel-recover Uses the same technique as -r and -n, but creates all the keys in parallel, using different threads (beta)   --quick quick Achieve a faster repair by not modifying the data file.   read_buffer_size=# read_buffer_size Each thread that does a sequential scan allocates a buffer of this size for each table it scans   --read-only read-only Don't mark the table as checked   --recover recover Do a repair that can fix almost any problem except unique keys that aren't unique   --safe-recover safe-recover Do a repair using an old recovery method that reads through all rows in order and updates all index trees based on the rows found   --set-auto-increment[=value] set-auto-increment Force AUTO_INCREMENT numbering for new records to start at the given value   --set-collation=name set-collation Specify the collation to use for sorting table indexes   --silent silent Silent mode   sort_buffer_size=# sort_buffer_size The buffer that is allocated when sorting the index when doing a REPAIR or when creating indexes with CREATE INDEX or ALTER TABLE   --sort-index sort-index Sort the index tree blocks in high-low order   sort_key_blocks=# sort_key_blocks sort_key_blocks   --sort-records=# sort-records Sort records according to a particular index   --sort-recover sort-recover Force myisamchk to use sorting to resolve the keys even if the temporary files would be very large   stats_method=value stats_method Specifies how MyISAM index statistics collection code should treat NULLs   --tmpdir=path tmpdir Path of the directory to be used for storing temporary files   --unpack unpack Unpack a table that was packed with myisampack   --update-state update-state Store information in the .MYI file to indicate when the table was checked and whether the table crashed   --verbose   Verbose mode   --version   Display version information and exit   write_buffer_size=# write_buffer_size Write buffer size  

위 표와 같은 옵션들은 크기 일반옵션과 chekc용 옵션, repair용 옵션으로 나눌수 있으며 각각은 다음 posting에서 살펴보고자 한다.




참고.
 복구도 중요하지만 이런 현상이 자주 발생 한다면 그 원인을 찾고 해결하는 것이 우선일 것이다.
아래 링크를 참고하면 도움이 될 것이다.
 - MySQL Internals Porting(http://forge.mysql.com/wiki/MySQL_Internals_Porting)
 - B.1.4.2. What to Do If MySQL Keeps Crashing(http://dev.mysql.com/doc/refman/6.0/en/crashing.html)

댓글목록

등록된 댓글이 없습니다.

Total 243건 1 페이지
MySQL Tip 목록
번호 제목 글쓴이 조회 날짜
243 no_profile 차동박 쪽지보내기 메일보내기 홈페이지 자기소개 아이디로 검색 전체게시물 55005 02-22
242 no_profile 차동박 쪽지보내기 메일보내기 홈페이지 자기소개 아이디로 검색 전체게시물 41768 07-28
열람중 no_profile 차동박 쪽지보내기 메일보내기 홈페이지 자기소개 아이디로 검색 전체게시물 40651 08-16
240 no_profile 차동박 쪽지보내기 메일보내기 홈페이지 자기소개 아이디로 검색 전체게시물 40401 12-30
239 no_profile 차동박 쪽지보내기 메일보내기 홈페이지 자기소개 아이디로 검색 전체게시물 34789 10-29
238 no_profile 차동박 쪽지보내기 메일보내기 홈페이지 자기소개 아이디로 검색 전체게시물 34752 08-09
237 no_profile 차동박 쪽지보내기 메일보내기 홈페이지 자기소개 아이디로 검색 전체게시물 34738 11-01
236 차동박 이름으로 검색 30468 10-11
235 no_profile 차동박 쪽지보내기 메일보내기 홈페이지 자기소개 아이디로 검색 전체게시물 30159 10-31
234 no_profile 차동박 쪽지보내기 메일보내기 홈페이지 자기소개 아이디로 검색 전체게시물 29538 12-23
233 no_profile 차동박 쪽지보내기 메일보내기 홈페이지 자기소개 아이디로 검색 전체게시물 29383 05-04
232 no_profile human 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 28295 09-11
231 차동박 이름으로 검색 27927 08-24
230 no_profile 차동박 쪽지보내기 메일보내기 홈페이지 자기소개 아이디로 검색 전체게시물 26668 04-24
229 no_profile 차동박 쪽지보내기 메일보내기 홈페이지 자기소개 아이디로 검색 전체게시물 25098 07-06
228 no_profile 차동박 쪽지보내기 메일보내기 홈페이지 자기소개 아이디로 검색 전체게시물 24962 12-30
227 no_profile 차동박 쪽지보내기 메일보내기 홈페이지 자기소개 아이디로 검색 전체게시물 24710 10-29
226 no_profile 차동박 쪽지보내기 메일보내기 홈페이지 자기소개 아이디로 검색 전체게시물 23935 08-13
게시물 검색