原址如下:
http://www.thegeekstuff.com/2008/09/how-to-repair-corrupted-mysql-tables-using-myisamchk/
MyISAM is the default storage engine for MySQL database. MyISAM table gets corrupted very easily. In this article, I’ll explain how to use myisamchk to identify and fix table corruption in MyISAM. When a table is created under MySQL, it creates three different files: *.frm file to store table format, *.MYD (MyData) file to store the data, and *.MYI (MyIndex) to store the index. I prefer to use InnoDB as the storage engine for bigger database, as it resembles Oracle and provides commit, rollback options.
I got the following error from a production bugzilla application that is using MySQL database. From the error message, it is clear that attach_data table is corrupted and needs to be reparied. The corrupted table can be repaired using myisamchk as explained below.
undef error - DBD::mysql::db selectrow_array failed: Table 'attach_data' is
marked as crashed and should be repaired [for Statement "SELECT LENGTH(thedata)
FROM attach_data WHERE id = ?"] at Bugzilla/Attachment.pm line 344
Bugzilla::Attachment::datasize('Bugzilla::Attachment=HASH(0x9df119c)') called
# myisamchk /var/lib/mysql/bugs/*.MYI >> /tmp/myisamchk_log.txt
myisamchk: error: Wrong bytesec: 0-0-0 at linkstart: 18361936
MyISAM-table 'attach_data.MYI' is corrupted
Fix it using switch "-r" or "-o"
myisamchk: warning: 1 client is using or hasn't closed the table properly
MyISAM-table 'groups.MYI' is usable but should be fixed
myisamchk: warning: 1 client is using or hasn't closed the table properly
MyISAM-table 'profiles.MYI' is usable but should be fixed
When you redirect the output of myisamchk to a temporary file, it will display only the corrupted table names on the screen. The /tmp/myisamchk_log.txt file will contain information about all the tables including the good ones, as shown below:
Checking MyISAM file: user_group_map.MYI Data records: 182 Deleted blocks: 0 - check file-size - check record delete-chain - check key delete-chain - check index reference - check data record references index: 1
Execute the myisamchk as shown below, with -r option to repair the corrupted tables identified in the above step.
# myisamchk -r profiles.MYI
- recovering (with sort) MyISAM-table 'profiles.MYI'
Data records: 80
- Fixing index 1
- Fixing index 2
You may get error message: clients are using or haven’t closed the table properly, if the tables are still getting used by your application and other tables. To avoid this error message, shutdown mysqld before performing the repair, if you can afford to shutdown the DB for a while. If not, use FLUSH TABLES to force mysqld to flush any table modification that are still in memory.
# myisamchk --silent --force --fast --update-state /var/lib/mysql/bugs/*.MYI
myisamchk: MyISAM file /var/lib/mysql/bugs/groups.MYI
myisamchk: warning: 1 client is using or hasn't closed the table properly
myisamchk: MyISAM file /var/lib/mysql/bugs/profiles.MYI
myisamchk: warning: 1 client is using or hasn't closed the table properly
For large database, it may take several hours to recover the tables. Depending on RAM available on your system, increase the memory parameters as shown below while executing the myisamchk:
# 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/bugs/*.MYI
You can also use myisamchk to get detailed information about a table, as shown below.
# myisamchk -dvv profiles.MYI
MyISAM file: profiles.MYI
Record format: Packed
Character set: latin1_swedish_ci (8)
File-version: 1
Creation time: 2007-08-16 18:46:59
Status: open,changed,analyzed,optimized keys,sorted index pages
Auto increment key: 1 Last value: 88
Data records: 88 Deleted blocks: 0
Datafile parts: 118 Deleted data: 0
Datafile pointer (bytes): 4 Keyfile pointer (bytes): 4
Datafile length: 6292 Keyfile length: 6144
Max datafile length: 4294967294 Max keyfile length: 4398046510079
Recordlength: 2124
table description:
Key Start Len Index Type Rec/key Root Blocksize
1 2 3 unique int24 1 1024 1024
2 5 765 unique char packed stripped 1 2048 4096
Field Start Length Nullpos Nullbit Type
1 1 1
2 2 3 no zeros
3 5 765 no endspace
Execute the following command to understand all the available option for myisamchk.
# myisamchk --help
Following are some of the key options that you can use along with myisamchk.
Global options:
Check options (check is the default action for myisamchk):
Repair options (When using ‘-r’ or ‘-o’):
Other actions: