MYISAMCHK(1) MySQL Database System MYISAMCHK(1)
myisamchk - MyISAM table-maintenance utility isamchk - ISAM
myisamchk [options] tbl_name ...
isamchk [options] tbl_name ...
The myisamchk utility gets information about your database tables or
checks, repairs, or optimizes them. myisamchk works with MyISAM tables
(tables with .MYI and .MYD files). A related utility, isamchk, works
with ISAM tables (tables with .ISM and .ISD files).
Invoke myisamchk like this:
shell> myisamchk [options] tbl_name ...
The options specify what you want myisamchk to do. They are described
in the following sections. You can also get a list of options by
invoking myisamchk --help.
With no options, myisamchk simply checks your table as the default
operation. To get more information or to tell myisamchk to take
corrective action, specify options as described in the following
tbl_name is the database table you want to check or repair. If you run
myisamchk somewhere other than in the database directory, you must
specify the path to the database directory, because myisamchk has no
idea where the database is located. In fact, myisamchk does not
actually care whether the files you are working on are located in a
database directory. You can copy the files that correspond to a
database table into some other location and perform recovery operations
on them there.
You can name several tables on the myisamchk command line if you wish.
You can also specify a table by naming its index file (the file with
the .MYI suffix). This allows you to specify all tables in a directory
by using the pattern *.MYI. For example, if you are in a database
directory, you can check all the MyISAM tables in that directory like
shell> myisamchk *.MYI
If you are not in the database directory, you can check all the tables
there by specifying the path to the directory:
shell> myisamchk /path/to/database_dir/*.MYI
You can even check all tables in all databases by specifying a wildcard
with the path to the MySQL data directory:
shell> myisamchk /path/to/datadir/*/*.MYI
The recommended way to quickly check all MyISAM and ISAM tables is:
shell> myisamchk --silent --fast /path/to/datadir/*/*.MYI
shell> isamchk --silent /path/to/datadir/*/*.ISM
If you want to check all MyISAM and ISAM tables and repair any that are
corrupted, you can use the following commands:
shell> myisamchk --silent --force --fast --update-state \
-O key_buffer=64M -O sort_buffer=64M \
-O read_buffer=1M -O write_buffer=1M \
shell> isamchk --silent --force -O key_buffer=64M \
-O sort_buffer=64M -O read_buffer=1M -O write_buffer=1M \
These commands assume that you have more than 64MB free. For more
information about memory allocation with myisamchk, see the section
called “\FBMYISAMCHK\FR MEMORY USAGE”.
You must ensure that no other program is using the tables while you are
running myisamchk. Otherwise, when you run myisamchk, it may display
the following error message:
warning: clients are using or haven’t closed the table properly
This means that you are trying to check a table that has been updated
by another program (such as the mysqld server) that hasn’t yet closed
the file or that has died without closing the file properly.
If mysqld is running, you must force it to flush any table
modifications that are still buffered in memory by using FLUSH TABLES.
You should then ensure that no one is using the tables while you are
running myisamchk. The easiest way to avoid this problem is to use
CHECK TABLE instead of myisamchk to check tables.
GENERAL OPTIONS FOR MYISAMCHK
The options described in this section can be used for any type of table
maintenance operation performed by myisamchk. The sections following
this one describe options that pertain only to specific operations,
such as table checking or repairing.
· --help, -?
Display a help message and exit.
· --debug=debug_options, -# debug_options
Write a debugging log. The debug_options string often is
· --silent, -s
Silent mode. Write output only when errors occur. You can use -s
twice (-ss) to make myisamchk very silent.
· --verbose, -v
Verbose mode. Print more information. This can be used with -d and
-e. Use -v multiple times (-vv, -vvv) for even more output.
· --version, -V
Display version information and exit.
· --wait, -w
Instead of terminating with an error if the table is locked, wait
until the table is unlocked before continuing. Note that if you are
running mysqld with the --skip-external-locking option, the table
can be locked only by another myisamchk command.
You can also set the following variables by using --var_name=value
It is also possible to set variables by using
--set-variable=var_name=value or -O var_name=value syntax. However,
this syntax is deprecated as of MySQL 4.0.
The possible myisamchk variables and their default values can be
examined with myisamchk --help:
sort_buffer_size is used when the keys are repaired by sorting keys,
which is the normal case when you use --recover.
key_buffer_size is used when you are checking the table with
--extend-check or when the keys are repaired by inserting keys row by
row into the table (like when doing normal inserts). Repairing through
the key buffer is used in the following cases:
· You use --safe-recover.
· The temporary files needed to sort the keys would be more than twice
as big as when creating the key file directly. This is often the
case when you have large key values for CHAR, VARCHAR, or TEXT
columns, because the sort operation needs to store the complete key
values as it proceeds. If you have lots of temporary space and you
can force myisamchk to repair by sorting, you can use the
Repairing through the key buffer takes much less disk space than using
sorting, but is also much slower.
If you want a faster repair, set the key_buffer_size and
sort_buffer_size variables to about 25% of your available memory. You
can set both variables to large values, because only one of them is
used at a time.
myisam_block_size is the size used for index blocks. It is available as
of MySQL 4.0.0.
stats_method influences how NULL values are treated for index
statistics collection when the --analyze option is given. It acts like
the myisam_stats_method system variable. For more information, see the
description of myisam_stats_method in the section called “SERVER SYSTEM
VARIABLES” and Section 4.7, “MyISAM Index Statistics Collection”.
stats_method was added in MySQL 4.1.15/5.0.14. For older versions, the
statistics collection method is equivalent to nulls_equal.
The ft_min_word_len and ft_max_word_len variables are available as of
MySQL 4.0.0. ft_stopword_file is available as of MySQL 4.0.19.
ft_min_word_len and ft_max_word_len indicate the minimum and maximum
word length for FULLTEXT indexes. ft_stopword_file names the stopword
file. These need to be set under the following circumstances.
If you use myisamchk to perform an operation that modifies table
indexes (such as repair or analyze), the FULLTEXT indexes are rebuilt
using the default full-text parameter values for minimum and maximum
word length and the stopword file unless you specify otherwise. This
can result in queries failing.
The problem occurs because these parameters are known only by the
server. They are not stored in MyISAM index files. To avoid the problem
if you have modified the minimum or maximum word length or the stopword
file in the server, specify the same ft_min_word_len, ft_max_word_len,
and ft_stopword_file values to myisamchk that you use for mysqld. For
example, if you have set the minimum word length to 3, you can repair a
table with myisamchk like this:
shell> myisamchk --recover --ft_min_word_len=3 tbl_name.MYI
To ensure that myisamchk and the server use the same values for
full-text parameters, you can place each one in both the [mysqld] and
[myisamchk] sections of an option file:
An alternative to using myisamchk is to use the REPAIR TABLE, ANALYZE
TABLE, OPTIMIZE TABLE, or ALTER TABLE. These statements are performed
by the server, which knows the proper full-text parameter values to
CHECK OPTIONS FOR MYISAMCHK
myisamchk supports the following options for table checking operations:
· --check, -c
Check the table for errors. This is the default operation if you
specify no option that selects an operation type explicitly.
· --check-only-changed, -C
Check only tables that have changed since the last check.
· --extend-check, -e
Check the table very thoroughly. This is quite slow if the table has
many indexes. This option should only be used in extreme cases.
Normally, myisamchk or myisamchk --medium-check should be able to
determine whether there are any errors in the table.
If you are using --extend-check and have plenty of memory, setting
the key_buffer_size variable to a large value helps the repair
operation run faster.
· --fast, -F
Check only tables that haven’t been closed properly.
· --force, -f
Do a repair operation automatically if myisamchk finds any errors in
the table. The repair type is the same as that specified with the
--repair or -r option.
· --information, -i
Print informational statistics about the table that is checked.
· --medium-check, -m
Do a check that is faster than an --extend-check operation. This
finds only 99.99% of all errors, which should be good enough in most
· --read-only, -T
do not mark the table as checked. This is useful if you use
myisamchk to check a table that is in use by some other application
that does not use locking, such as mysqld when run with the
· --update-state, -U
Store information in the .MYI file to indicate when the table was
checked and whether the table crashed. This should be used to get
full benefit of the --check-only-changed option, but you should not
use this option if the mysqld server is using the table and you are
running it with the --skip-external-locking option.
REPAIR OPTIONS FOR MYISAMCHK
myisamchk supports the following options for table repair operations:
· --backup, -B
Make a backup of the .MYD file as file_name-time.BAK
The directory where character sets are installed. See Section 7.1,
“The Character Set Used for Data and Sorting”.
Correct the checksum information for the table.
· --data-file-length=len, -D len
Maximum length of the data file (when re-creating data file when it
· --extend-check, -e
Do a repair that tries to recover every possible row from the data
file. Normally this also finds a lot of garbage rows. do not use
this option unless you are totally desperate.
· --force, -f
Overwrite old intermediate files (files with names like
tbl_name.TMD) instead of aborting.
· --keys-used=val, -k val
For myisamchk, the option value is a bit-value indicates which
indexes to update. Each binary bit of the option value corresponds
to a table index, where the first index is bit 0. For isamchk, the
option value indicates that only the first val of the table indexes
should be updated. In either case, an option value of 0 disables
updates to all indexes, which can be used to get faster inserts.
Deactivated indexes can be reactivated by using myisamchk -r or
· --no-symlinks, -l
Do not follow symbolic links. Normally myisamchk repairs the table
that a symlink points to. This option does not exist as of MySQL
4.0, because versions from 4.0 on do not remove symlinks during
· --parallel-recover, -p
Uses the same technique as -r and -n, but creates all the keys in
parallel, using different threads. This option was added in MySQL
4.0.2. This is alpha code; use at your own risk.
· --quick, -q
Achieve a faster repair by not modifying the data file. You can
specify this option twice to force myisamchk to modify the original
data file in case of duplicate keys.
· --recover, -r
Do a repair that can fix almost any problem except unique keys that
are not unique (which is an extremely unlikely error with
ISAM/MyISAM tables). If you want to recover a table, this is the
option to try first. You should try -o only if myisamchk reports
that the table cannot be recovered by -r. (In the unlikely case that
-r fails, the data file remains intact.)
If you have lots of memory, you should increase the value of
· --safe-recover, -o
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. This
is an order of magnitude slower than -r, but can handle a couple of
very unlikely cases that -r cannot. This recovery method also uses
much less disk space than -r. Normally, you should repair first
using -r, and then with -o only if -r fails.
If you have lots of memory, you should increase the value of
Change the character set used by the table indexes. This option was
replaced by --set-collation in MySQL 4.1.1.
Change the collation used to sort table indexes. The character set
name is implied by the first part of the collation name. This option
was added in MySQL 4.1.11.
· --sort-recover, -n
Force myisamchk to use sorting to resolve the keys even if the
temporary files should be very big.
· --tmpdir=path, -t path
Path of the directory to be used for storing temporary files. If
this is not set, myisamchk uses the value of the TMPDIR environment
variable. Starting from MySQL 4.1, tmpdir can be set to a list of
directory paths that are used successively in round-robin fashion
for creating temporary files. The separator character between
directory names should be colon (‘:’) on Unix and semicolon (‘;’) on
Windows, NetWare, and OS/2.
· --unpack, -u
Unpack a table that was packed with myisampack.
OTHER OPTIONS FOR MYISAMCHK
myisamchk supports the following options for actions other than table
checks and repairs:
· --analyze, -a
Analyze the distribution of keys. This improves join performance by
enabling the join optimizer to better choose the order in which to
join the tables and which keys it should use. To obtain information
about the distribution, use a myisamchk --description --verbose
tbl_name command or the SHOW KEYS FROM tbl_name statement.
· --description, -d
Print some descriptive information about the table.
· --set-auto-increment[=value], -A[value]
Force AUTO_INCREMENT numbering for new records to start at the given
value (or higher, if there are existing records with AUTO_INCREMENT
values this large). If value is not specified, AUTO_INCREMENT number
for new records begins with the largest value currently in the
table, plus one.
· --sort-index, -S
Sort the index tree blocks in high-low order. This optimizes seeks
and makes table scanning by key faster.
· --sort-records=N, -R N
Sort records according to a particular index. This makes your data
much more localized and may speed up range-based SELECT and ORDER BY
operations that use this index. (The first time you use this option
to sort a table, it may be very slow.) To determine a table’s index
numbers, use SHOW KEYS, which displays a table’s indexes in the same
order that myisamchk sees them. Indexes are numbered beginning with
If keys are not packed (PACK_KEYS=0)), they have the same length, so
when myisamchk sorts and moves records, it just overwrites record
offsets in the index. If keys are packed (PACK_KEYS=1), myisamchk
must unpack key blocks first, then recreate indexes and pack the key
blocks again. (In this case, recreating indexes is faster than
updating offsets for each index.)
MYISAMCHK MEMORY USAGE
Memory allocation is important when you run myisamchk. myisamchk uses
no more memory than you specify with the -O options. If you are going
to use myisamchk on very large tables, you should first decide how much
memory you want it to use. The default is to use only about 3MB to
perform repairs. By using larger values, you can get myisamchk to
operate faster. For example, if you have more than 32MB RAM, you could
use options such as these (in addition to any other options you might
shell> myisamchk -O sort=16M -O key=16M -O read=1M -O write=1M ...
Using -O sort=16M should probably be enough for most cases.
Be aware that myisamchk uses temporary files in TMPDIR. If TMPDIR
points to a memory filesystem, you may easily get out of memory errors.
If this happens, set TMPDIR to point at some directory located on a
filesystem with more space and run myisamchk again.
When repairing, myisamchk also needs a lot of disk space:
· Double the size of the data file (the original one and a copy). This
space is not needed if you do a repair with --quick; in this case,
only the index file is re-created. This space must be available on
the same filesystem as the original data file, as the copy is
created in the same directory as the original.
· Space for the new index file that replaces the old one. The old
index file is truncated at the start of the repair operation, so you
usually ignore this space. This space must be available on the same
filesystem as the original data file.
· When using --recover or --sort-recover (but not when using
--safe-recover), you need space for a sort buffer. The amount of
space required is:
(largest_key + row_pointer_length) * number_of_rows * 2
You can check the length of the keys and the row_pointer_length with
myisamchk -dv tbl_name. This space is allocated in the temporary
directory (specified by TMPDIR or --tmpdir=path).
If you have a problem with disk space during repair, you can try to use
--safe-recover instead of --recover.
USING MYISAMCHK FOR CRASH RECOVERY
If you run mysqld with --skip-external-locking (which is the default on
some systems, such as Linux), you cannot reliably use myisamchk to
check a table when mysqld is using the same table. If you can be sure
that no one is accessing the tables through mysqld while you run
myisamchk, you only have to do mysqladmin flush-tables before you start
checking the tables. If you cannot guarantee this, then you must stop
mysqld while you check the tables. If you run myisamchk while mysqld is
updating the tables, you may get a warning that a table is corrupt even
when it is not.
If you are not using --skip-external-locking, you can use myisamchk to
check tables at any time. While you do this, all clients that try to
update the table wait until myisamchk is ready before continuing.
If you use myisamchk to repair or optimize tables, you must always
ensure that the mysqld server is not using the table (this also applies
if you are using --skip-external-locking). If you do not take down
mysqld, you should at least do a mysqladmin flush-tables before you run
myisamchk. Your tables may become corrupted if the server and myisamchk
access the tables simultaneously.
This section describes how to check for and deal with data corruption
in MySQL databases. If your tables are corrupted frequently you should
try to find the reason why. See Section 4.2, “What to Do If MySQL Keeps
The MyISAM table section discusses some reasons why a table could be
corrupted. See Section 1.4, “MyISAM Table Problems”.
When performing crash recovery, it is important to understand that each
MyISAM table tbl_name in a database corresponds to three files in the
database directory: FilePurposetbl_name.frmDefinition (format)
filetbl_name.MYDData filetbl_name.MYIIndex file.PP Each of these three
file types is subject to corruption in various ways, but problems occur
most often in data files and index files.
myisamchk works by creating a copy of the .MYD data file row by row. It
ends the repair stage by removing the old .MYD file and renaming the
new file to the original file name. If you use --quick, myisamchk does
not create a temporary .MYD file, but instead assumes that the .MYD
file is correct and only generates a new index file without touching
the .MYD file. This is safe, because myisamchk automatically detects
whether the .MYD file is corrupt and aborts the repair if it is. You
can also specify the --quick option twice to myisamchk. In this case,
myisamchk does not abort on some errors (such as duplicate-key errors)
but instead tries to resolve them by modifying the .MYD file. Normally
the use of two --quick options is useful only if you have too little
free disk space to perform a normal repair. In this case, you should at
least make a backup before running myisamchk.
HOW TO CHECK MYISAM TABLES FOR ERRORS
To check a MyISAM table, use the following commands:
· myisamchk tbl_name
This finds 99.99% of all errors. What it cannot find is corruption
that involves only the data file (which is very unusual). If you
want to check a table, you should normally run myisamchk without
options or using the -s or --silent option.
· myisamchk -m tbl_name
This finds 99.999% of all errors. It first checks all index entries
for errors and then reads through all rows. It calculates a checksum
for all keys in the rows and verifies that the checksum matches the
checksum for the keys in the index tree.
· myisamchk -e tbl_name
This does a complete and thorough check of all data (-e means
“extended check”). It does a check-read of every key for each row to
verify that they indeed point to the correct row. This may take a
long time for a large table that has many indexes. Normally,
myisamchk stops after the first error it finds. If you want to
obtain more information, you can add the --verbose (-v) option. This
causes myisamchk to continue, up to a maximum of 20 errors.
· myisamchk -e -i tbl_name
Like the previous command, but the -i option tells myisamchk to
print additional statistical information.
In most cases, a simple myisamchk with no arguments other than the
table name is sufficient to check a table.
HOW TO REPAIR TABLES
The discussion in this section describes how to use myisamchk on MyISAM
tables (extensions .MYI and .MYD). If you are using ISAM tables
(extensions .ISM and .ISD), you should use isamchk instead; the
concepts are similar.
If you are using MySQL 3.23.16 and above, you can (and should) use the
CHECK TABLE and REPAIR TABLE statements to check and repair MyISAM
tables. See Section 5.2.3, “CHECK TABLE Syntax” and Section 5.2.6,
“REPAIR TABLE Syntax”.
The symptoms of a corrupted table include queries that abort
unexpectedly and observable errors such as these:
· tbl_name.frm is locked against change
· cannot find file tbl_name.MYI (Errcode: nnn)
· Unexpected end of file
· Record file is crashed
· Got error nnn from table handler
To get more information about the error you can run perrornnn, where
nnn is the error number. The following example shows how to use perror
to find the meanings for the most common error numbers that indicate a
problem with a table:
shell> perror 126 127 132 134 135 136 141 144 145
126 = Index file is crashed / Wrong file format
127 = Record-file is crashed
132 = Old database file
134 = Record was already deleted (or record file crashed)
135 = No more room in record file
136 = No more room in index file
141 = Duplicate unique key or constraint on write or update
144 = Table is crashed and last repair failed
145 = Table was marked as crashed and should be repaired
Note that error 135 (no more room in record file) and error 136 (no
more room in index file) are not errors that can be fixed by a simple
repair. In this case, you have to use ALTER TABLE to increase the
MAX_ROWS and AVG_ROW_LENGTH table option values:
ALTER TABLE tbl_name MAX_ROWS=xxx AVG_ROW_LENGTH=yyy;
If you do not know the current table option values, use SHOW CREATE
For the other errors, you must repair your tables. myisamchk can
usually detect and fix most problems that occur.
The repair process involves up to four stages, described here. Before
you begin, you should change location to the database directory and
check the permissions of the table files. On Unix, make sure that they
are readable by the user that mysqld runs as (and to you, because you
need to access the files you are checking). If it turns out you need to
modify files, they must also be writable by you.
The options that you can use for table maintenance with myisamchk and
isamchk are described in several of the earlier subsections of
The following section is for the cases where the above command fails or
if you want to use the extended features that myisamchk and isamchk
If you are going to repair a table from the command line, you must
first stop the mysqld server. Note that when you do mysqladmin shutdown
on a remote server, the mysqld server is still alive for a while after
mysqladmin returns, until all queries are stopped and all keys have
been flushed to disk.
Stage 1: Checking your tables
Run myisamchk *.MYI or myisamchk -e *.MYI if you have more time. Use
the -s (silent) option to suppress unnecessary information.
If the mysqld server is down, you should use the --update-state option
to tell myisamchk to mark the table as ’checked’.
You have to repair only those tables for which myisamchk announces an
error. For such tables, proceed to Stage 2.
If you get strange errors when checking (such as out of memory), or if
myisamchk crashes, go to Stage 3.
Stage 2: Easy safe repair
Note: If you want a repair operation to go much faster, you should set
the values of the sort_buffer_size and key_buffer_size variables each
to about 25% of your available memory when running myisamchk or
First, try myisamchk -r -q tbl_name (-r -q means “quick recovery
mode”). This attempts to repair the index file without touching the
data file. If the data file contains everything that it should and the
delete links point at the correct locations within the data file, this
should work, and the table is fixed. Start repairing the next table.
Otherwise, use the following procedure:
1. Make a backup of the data file before continuing.
2. Use myisamchk -r tbl_name (-r means “recovery mode”). This removes
incorrect and deleted records 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).
If you get strange errors when checking (such as out of memory), or if
myisamchk crashes, go to Stage 3.
Stage 3: Difficult repair
You should reach this stage only if the first 16KB block in the index
file is destroyed or contains incorrect information, or if the index
file is missing. In this case, it is necessary to create a new index
file. Do so as follows:
1. Move the data file to a safe place.
2. Use the table description file to create new (empty) data and index
shell> mysql db_name
mysql> SET AUTOCOMMIT=1;
mysql> TRUNCATE TABLE tbl_name;
If your version of MySQL does not have TRUNCATE TABLE, use DELETE
FROM tbl_name instead.
3. Copy the old data file back onto the newly created data file. (do
not just move the old file back onto the new file; you want to
retain a copy in case something goes wrong.)
Go back to Stage 2. myisamchk -r -q should work. (This should not be
an endless loop.)
As of MySQL 4.0.2, you can also use REPAIR TABLE tbl_name USE_FRM,
which performs the whole procedure automatically.
Stage 4: Very difficult repair
You should reach this stage only if the .frm description file has also
crashed. That should never happen, because the description file is not
changed after the table is created:
1. Restore the description file from a backup and go back to Stage 3.
You can also restore the index file and go back to Stage 2. In the
latter case, you should start with myisamchk -r.
2. If you do not have a backup but know exactly how the table was
created, create a copy of the table in another database. Remove the
new data file, then move the .frm description and .MYI index files
from the other database to your crashed database. This gives you new
description and index files, but leaves the .MYD data file alone. Go
back to Stage 2 and attempt to reconstruct the index file.
To coalesce fragmented records and eliminate wasted space resulting
from deleting or updating records, run myisamchk in recovery mode:
shell> myisamchk -r tbl_name
You can optimize a table in the same way by using the SQL OPTIMIZE
TABLE statement. OPTIMIZE TABLE does a repair of the table and a key
analysis, and also sorts the index tree to give faster key lookups.
There is also no possibility of unwanted interaction between a utility
and the server, because the server does all the work when you use
OPTIMIZE TABLE. See Section 5.2.5, “OPTIMIZE TABLE Syntax”.
myisamchk also has a number of other options you can use to improve the
performance of a table:
· -S, --sort-index
· -R index_num, --sort-records=index_num
· -a, --analyze
For a full description of all available options, see myisamchk(1).
isamchk(1), isamlog(1), msql2mysql(1), myisamlog(1), myisampack(1),
mysql(1), mysql.server(1), mysql_config(1),
mysql_fix_privilege_tables(1), mysql_zap(1), mysqlaccess(1),
mysqladmin(1), mysqlbinlog(1), mysqlcheck(1), mysqld(1),
mysqld_multi(1), mysqld_safe(1), mysqldump(1), mysqlhotcopy(1),
mysqlimport(1), mysqlshow(1), pack_isam(1), perror(1), replace(1),
For more information, please refer to the MySQL Reference Manual, which
may already be installed locally and which is also available online at
MySQL AB (http://www.mysql.com/). This software comes with no
MySQL 4.1 11/30/2005 MYISAMCHK(1)
Man(1) output converted with