Problem
디비 서버를 운영하다보면 아래와 같은 에러와 함께 db서버가 죽어버리는 경우가 있다.

091030 13:40:29 [ERROR] /usr/libexec/mysqld: Table ‘.some_table’ is marked as crashed and should be repaired

Solution
mysql> analyze table some_table;
mysql> repaire table some_table;
//위에서 실패하면 아래 실행
]# myisamchk -r some_table;

위와 같이 repair를 하면 쉽게 해결되기는 하지만, 그 원인은 뭘까?

mysql Reference에서 How to Repair MyISAM Tables 페이지에서 찾을 수 있었다.
위의 문제는 다음과 같은 충돌들로 일어날 수 있다.

  • tbl_name.frm이 변경에 대해서 락이 걸려있는 경우
  • tbl_name.MYI(Errcod:nnn)을 찾을 수 없는 경우
  • 파일이 unexpected end일 경우
  • 저장 파일이 충돌했을 경우
  • 테이블 핸들러로부터 nnn에러를 받았을 경우

위에서 말하는 nnn 에러는 system error로 다음 커맨드로 그 의미를 알아볼 수 있다.
]# perror 126 127 132 134 135 136 141 144 145
MySQL error code 126 = Index file is crashed
MySQL error code 127 = Record-file is crashed
MySQL error code 132 = Old database file
MySQL error code 134 = Record was already deleted (or record file crashed)
MySQL error code 135 = No more room in record file
MySQL error code 136 = No more room in index file
MySQL error code 141 = Duplicate unique key or constraint on write or update
MySQL error code 144 = Table is crashed and last repair failed
MySQL error code 145 = Table was marked as crashed and should be repaired

위의 145 번 에러 코드가 이번 경우에 해당 되었다.
참고로 135번과 136번에러는 테이블의 MAX_ROWS와 AVG_ROW_LENGTH를 수정해서 고칠 수 있다.
mysql>ALTER TABLE tbl_name MAX_ROWS=xxx AVG_ROW_LENGTH=yyy;
현재 테이블 옵션이 어떻게 되어 있는지 보려면 아래 명령어로 보자.
mysql>SHOW CREATE TABLE some_table;

다른 에러들에 대한 대처는 myisamchk르 대부분이 찾아내서 고칠 수 있다.
이 작업을 하기 전에 디비 테이블 파일들에 대한 권한을 확인한다.(없으면 부여)
먼저 mysqld서버를 중지한다.(유의: 중지 후 바로 디비 서버가 바로 죽지 않는다. 모든 index 변경사항이 디스크에 플러시 될때까지가 중지되는 시점이다)

Stage 1: 테이블 검사

]# myisamchk *.MYI
이 작업으로 error라고 판별된 테이블에 대해서만 Stage 2에서 고쳐질 수 있으며, 이 커맨드 과정에서 에러(e.g. out of memory)가 나면 Stage 3로 너어가면 된다.

Stage 2: Easy safe repair

]# myisamchk -r -q tbl_name (-r -q 은 “빠른 복구 모드”)
이는 data file을 건들지 않고 index file을 고치는 커맨드이다.

  1. Make a backup of the data file before continuing.
  2. Use myisamchk -r tbl_name (-r means “recovery mode”). This removes incorrect rows and deleted rows from the data file and reconstructs the index file.
  3. If the preceding step fails, use myisamchk –safe-recover tbl_name. Safe recovery mode uses an old recovery method that handles a few cases that regular recovery mode does not (but is slower).

나는 여기까지로 대부분이 해결이 되었다.
stage 3에 대한 방법, 그 외의 방법은 How to Repair MyISAM Tables를 참고하면 되겠다.

Posted by 달팽이맛나
,