isamchkfor table maintenance and crash recovery
To check/repair ISAM tables (
.ISD) you should use the
isamchk utility. To check/repair MyISAM tables (
you should use the
See section 10.18 MySQL table types..
In the following text we will talk about
isamchk but everything
also applies to
You can use the
isamchk utility to get information about your database
tables, check and repair them or optimize them. The following sections
describe how to invoke
isamchk (including a description of its
options), how to set up a table maintenance schedule, and how to use
isamchk to perform its various functions.
If you run
--skip-locking (which is the default on
some systems, like Linux), you can't reliably use
check a table when
mysqld is using the same table. If you
can be sure that no one is accessing the tables through
while you run
isamchk, you only have to do
flush-tables before you start checking the tables. If you can't
guarantee the above, then you must take down
mysqld while you
check the tables. If you run
mysqld is updating
the tables, you may get a warning that a table is corrupt even if it
If you are not using
--skip-locking, you can use
to check tables at any time. While you do this, all clients that try
to update the table will wait until
isamchk is ready before
If you use
isamchk 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
If you don't take down
mysqld you should at least do a
mysqladmin flush-tables before you run
You can in most cases also use the command
OPTIMIZE TABLES to
optimize and repair tables, but this is not as fast or reliable (in case
of real fatal errors) as
isamchk. On the other hand,
OPTIMIZE TABLE is easier to use and you don't have to worry about
See section 7.8
OPTIMIZE TABLE syntax.
isamchk is invoked like this:
shell> isamchk [options] tbl_name
options specify what you want
isamchk to do. They are
described below. (You can also get a list of options by invoking
isamchk --help.) With no options,
isamchk simply checks your
table. To get more information or to tell
isamchk to take corrective
action, specify options as described below and in the following sections.
tbl_name is the database table you want to check. If you run
isamchk somewhere other than in the database directory, you must
specify the path to the file, since
isamchk has no idea where your
database is located. Actually,
isamchk doesn't care whether or not
the files you are working on are located in a database directory; you can
copy the files that correspond to a database table into another location and
perform recovery operations on them there.
You can name several tables on the
isamchk command line if you
wish. You can also specify a name as an index file
name (with the `.ISM' suffix), which allows you to specify all
tables in a directory by using the pattern `*.ISM'.
For example, if you are in a database directory, you can check all the
tables in the directory like this:
shell> isamchk *.ISM
If you are not in the database directory, you can check all the tables there by specifying the path to the directory:
shell> isamchk /path/to/database_dir/*.ISM
You can even check all tables in all databases by specifying a wildcard with the path to the MySQL data directory:
shell> isamchk /path/to/datadir/*/*.ISM
isamchk supports the following options:
debug_optionsstring often is
isamchkshould find all errors even without this option.
-fwhen checking tables (running
isamchkwill automatically restart with
-ron any table for which an error occurs during checking.
-k #, --keys-used=#
-r. Tell the NISAM table handler to update only the first
#indexes. Higher-numbered indexes are deactivated. This can be used to get faster inserts! Deactivated indexes can be reactivated by using
isamchkrepairs the table a symlink points at.
-rto get a faster repair. Normally, the original data file isn't touched; you can specify a second
-qto force the original data file to be used.
-r, but can handle a couple of cases that
-O var=option, --set-variable var=option
-ss) to make
-R index_num, --sort-records=index_num
ORDER BYoperations on this index. (It may be VERY slow to do a sort the first time!) To find out a table's index numbers, use
SHOW INDEX, which shows a table's indexes in the same order that
isamchksees them. Indexes are numbered beginning with 1.
-vmultiple times (
-vvv) for more verbosity!
isamchkversion and exit.
Possible variables for the
-O) option are:
key_buffer_size current value: 16776192 read_buffer_size current value: 262136 write_buffer_size current value: 262136 sort_buffer_size current value: 2097144 sort_key_blocks current value: 16 decode_bits current value: 9
Memory allocation is important when you run
uses no more memory than you specify with the
-O options. If you are
going to use
isamchk on very large files, you should first decide how
much memory you want it to use. The default is to use only about 3M to fix
things. By using larger values, you can get
isamchk to operate
faster. For example, if you have more than 32M RAM, you could use options
such as these (in addition to any other options you might specify):
shell> isamchk -O sort=16M -O key=16M -O read=1M -O write=1M ...
-O sort=16M should
probably be enough for most cases.
Be aware that
isamchk uses temporary files in
TMPDIR points to a memory file system, you may easily get out of
memory errors. If this happens, set
TMPDIR to point at some directory
with more space and restart
It is a good idea to perform table checks on a regular basis rather than
waiting for problems to occur. For maintenance purposes, you can use
isamchk -s to check tables. The
-s option causes
isamchk to run in silent mode, printing messages only when errors
It's a good idea to check tables when the server starts up.
For example, whenever the machine has done a reboot in the middle of an
update, you usually need to check all the tables that could have been
affected. (This is an ``expected crashed table''.) You could add a test to
safe_mysqld that runs
isamchk to check all tables that have
been modified during the last 24 hours if there is an old `.pid'
(process ID) file left after a reboot. (The `.pid' file is created by
mysqld when it starts up and removed when it terminates normally. The
presence of a `.pid' file at system startup time indicates that
mysqld terminated abnormally.)
An even better test would be to check any table whose last-modified time is more recent than that of the `.pid' file.
You should also check your tables regularly during normal system operation.
At TcX, we run a
cron job to check all our important tables once a week,
using a line like this in a `crontab' file:
35 0 * * 0 /path/to/isamchk -s /path/to/datadir/*/*.ISM
This prints out information about crashed tables so we can examine and repair them when needed.
As we haven't had any unexpectedly crashed tables (tables that become corrupted for reasons other than hardware trouble) for a couple of years now (this is really true), once a week is more than enough for us.
We recommend that to start with, you execute
isamchk -s each
night on all tables that have been updated during the last 24 hours,
until you come to trust MySQL as much as we do.
To get a description of a table or statistics about it, use the commands shown below. We explain some of the information in more detail later.
isamchk -d tbl_name
isamchkin ``describe mode'' to produce a description of your table. If you start the MySQL server using the
isamchkmay report an error for a table that is updated while it runs. However, since
isamchkdoesn't change the table in describe mode, there isn't any risk of destroying data.
isamchk -d -v tbl_name
isamchkis doing, add
-vto tell it to run in verbose mode.
isamchk -eis tbl_name
isamchk -eiv tbl_name
-eis, but tells you what is being done.
isamchk -d output:
ISAM file: company.ISM Data records: 1403698 Deleted blocks: 0 Recordlength: 226 Record format: Fixed length table description: Key Start Len Index Type 1 2 8 unique double 2 15 10 multip. text packed stripped 3 219 8 multip. double 4 63 10 multip. text packed stripped 5 167 2 multip. unsigned short 6 177 4 multip. unsigned long 7 155 4 multip. text 8 138 4 multip. unsigned long 9 177 4 multip. unsigned long 193 1 text
isamchk -d -v output:
ISAM file: company.ISM Isam-version: 2 Creation time: 1996-08-28 11:44:22 Recover time: 1997-01-12 18:35:29 Data records: 1403698 Deleted blocks: 0 Datafile: Parts: 1403698 Deleted data: 0 Datafilepointer (bytes): 3 Keyfile pointer (bytes): 3 Max datafile length: 3791650815 Max keyfile length: 4294967294 Recordlength: 226 Record format: Fixed length table description: Key Start Len Index Type Root Blocksize Rec/key 1 2 8 unique double 15845376 1024 1 2 15 10 multip. text packed stripped 25062400 1024 2 3 219 8 multip. double 40907776 1024 73 4 63 10 multip. text packed stripped 48097280 1024 5 5 167 2 multip. unsigned short 55200768 1024 4840 6 177 4 multip. unsigned long 65145856 1024 1346 7 155 4 multip. text 75090944 1024 4995 8 138 4 multip. unsigned long 85036032 1024 87 9 177 4 multip. unsigned long 96481280 1024 178 193 1 text
isamchk -eis output:
Checking ISAM file: company.ISM Key: 1: Keyblocks used: 97% Packed: 0% Max levels: 4 Key: 2: Keyblocks used: 98% Packed: 50% Max levels: 4 Key: 3: Keyblocks used: 97% Packed: 0% Max levels: 4 Key: 4: Keyblocks used: 99% Packed: 60% Max levels: 3 Key: 5: Keyblocks used: 99% Packed: 0% Max levels: 3 Key: 6: Keyblocks used: 99% Packed: 0% Max levels: 3 Key: 7: Keyblocks used: 99% Packed: 0% Max levels: 3 Key: 8: Keyblocks used: 99% Packed: 0% Max levels: 3 Key: 9: Keyblocks used: 98% Packed: 0% Max levels: 4 Total: Keyblocks used: 98% Packed: 17% Records: 1403698 M.recordlength: 226 Packed: 0% Recordspace used: 100% Empty space: 0% Blocks/Record: 1.00 Recordblocks: 1403698 Deleteblocks: 0 Recorddata: 317235748 Deleted data: 0 Lost space: 0 Linkdata: 0 User time 1626.51, System time 232.36 Maximum resident set size 0, Integral resident set size 0 Non physical pagefaults 0, Physical pagefaults 627, Swaps 0 Blocks in 0 out 0, Messages in 0 out 0, Signals 0 Voluntary context switches 639, Involuntary context switches 28966
isamchk -eiv output:
Checking ISAM file: company.ISM Data records: 1403698 Deleted blocks: 0 - check file-size - check delete-chain index 1: index 2: index 3: index 4: index 5: index 6: index 7: index 8: index 9: No recordlinks - check index reference - check data record references index: 1 Key: 1: Keyblocks used: 97% Packed: 0% Max levels: 4 - check data record references index: 2 Key: 2: Keyblocks used: 98% Packed: 50% Max levels: 4 - check data record references index: 3 Key: 3: Keyblocks used: 97% Packed: 0% Max levels: 4 - check data record references index: 4 Key: 4: Keyblocks used: 99% Packed: 60% Max levels: 3 - check data record references index: 5 Key: 5: Keyblocks used: 99% Packed: 0% Max levels: 3 - check data record references index: 6 Key: 6: Keyblocks used: 99% Packed: 0% Max levels: 3 - check data record references index: 7 Key: 7: Keyblocks used: 99% Packed: 0% Max levels: 3 - check data record references index: 8 Key: 8: Keyblocks used: 99% Packed: 0% Max levels: 3 - check data record references index: 9 Key: 9: Keyblocks used: 98% Packed: 0% Max levels: 4 Total: Keyblocks used: 9% Packed: 17% - check records and index references [LOTS OF ROW NUMBERS DELETED] Records: 1403698 M.recordlength: 226 Packed: 0% Recordspace used: 100% Empty space: 0% Blocks/Record: 1.00 Recordblocks: 1403698 Deleteblocks: 0 Recorddata: 317235748 Deleted data: 0 Lost space: 0 Linkdata: 0 User time 1639.63, System time 251.61 Maximum resident set size 0, Integral resident set size 0 Non physical pagefaults 0, Physical pagefaults 10580, Swaps 0 Blocks in 4 out 0, Messages in 0 out 0, Signals 0 Voluntary context switches 10604, Involuntary context switches 122798
Here are the sizes of the data and index files for the table used in the preceding examples:
-rw-rw-r-- 1 monty tcx 317235748 Jan 12 17:30 company.ISD -rw-rw-r-- 1 davida tcx 96482304 Jan 12 18:35 company.ISM
Explanations for the types of information
isamchk produces are given
below. The ``keyfile'' is the index file. ``Record'' and ``row'' are
Max datafile length
.ISDfile) can become, in bytes.
Max keyfile length
.ISMfile) can become, in bytes.
Fixed length. Other possible values are
multip.(multiple). Indicates whether or not one value can exist multiple times in this index.
isamchk -a. If this is not updated at all, a default value of 30 is given.
isamchk, the values are very high (very near the theoretical maximum).
DECIMALkeys. For long strings like names, this can significantly reduce the space used. In the third example above, the 4th key is 10 characters long and a 60% reduction in space is achieved.
Packedvalue indicates the percentage savings achieved by doing this.
isamchk. See section 13.4.3 Table optimization.
Linkdatais the sum of the amount of storage used by all such pointers.
If a table has been compressed with
prints additional information about each table column. See section 12.5 The MySQL compressed read-only table generator, for an example of this information and a description of
what it means.
isamchkfor crash recovery
The file format that MySQL uses to store data has been extensively tested, but there are always external circumstances that may cause database tables to become corrupted:
mysqldprocess being killed in the middle of a write
This chapter describes how to check for and deal with data corruption in MySQL databases. If your tables get corrupted a lot you should try to find the reason for this! See section G.1 Debugging a MySQL server.
When performing crash recovery, it is important to understand that each table
tbl_name in a database corresponds to three files in the database
|`tbl_name.frm'||Table definition (form) file|
Each of these three file types is subject to corruption in various ways, but problems occur most often in data files and index files.
isamchk works by creating a copy of the `.ISD' (data) file row by
row. It ends the repair stage by removing the old `.ISD' file and
renaming the new file to the original file name. If you use
isamchk does not create a temporary `.ISD' file, but instead
assumes that the `.ISD' file is correct and only generates a new index
file without touching the `.ISD' file. This is safe, because
isamchk automatically detects if the `.ISD' file is corrupt and
aborts the repair in this case. You can also give two
isamchk. In this case,
isamchk does not abort on some
errors (like duplicate key) but instead tries to resolve them by
modifying the `.ISD' file. Normally the use of two
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
To check a table, use the following commands:
isamchkwithout options or with either the
isamchk -e tbl_name
-emeans ``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 on a big table with many keys.
isamchkwill normally stop after the first error it finds. If you want to obtain more information, you can add the
-v) option. This causes
isamchkto keep going, up through a maximum of 20 errors. In normal usage, a simple
isamchk(with no arguments other than the table name) is sufficient.
isamchk -e -i tbl_name
isamchkto print some informational statistics, too.
The symptoms of a corrupted table are usually that queries abort unexpectedly and that you observe errors such as these:
In these cases, you must repair your tables.
can usually detect and fix most things that go wrong.
The repair process involves up to four stages, described below. Before you
begin, you should
cd to the database directory and check the
permissions of the table files. Make sure they are readable by the Unix user
mysqld runs as (and to you, since 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.
Stage 1: Checking your tables
isamchk *.ISM or (
isamchk -e *.ISM if you have more time).
-s (silent) option to suppress unnecessary information.
You have to repair only those tables for which
isamchk announces an
error. For such tables, proceed to Stage 2.
If you get weird errors when checking (such as
memory errors), or if
isamchk crashes, go to Stage 3.
Stage 2: Easy safe repair
isamchk -r -q tbl_name (
-r -q means ``quick recovery
mode''). This will attempt 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
isamchk -r tbl_name(
-rmeans ``recovery mode''). This will remove incorrect records and deleted records from the data file and reconstruct the index file.
isamchk --safe-recover tbl_name. Safe recovery mode uses an old recovery method that handles a few cases that regular recovery mode doesn't (but is slower).
If you get weird errors when repairing (such as
memory errors), or if
isamchk crashes, go to Stage 3.
Stage 3: Difficult repair
You should only reach this stage if the first 16K block in the index file is destroyed or contains incorrect information, or if the index file is missing. In this case, it's necessary to create a new index file. Do so as follows:
shell> mysql db_name mysql> DELETE FROM tbl_name; mysql> quit
Go back to Stage 2.
isamchk -r -q should work now. (This shouldn't
be an endless loop).
Stage 4: Very difficult repair
You should reach this stage only if the description file has also crashed. That should never happen, because the description file isn't changed after the table is created.
To coalesce fragmented records and eliminate wasted space resulting from
deleting or updating records, run
isamchk in recovery mode:
shell> isamchk -r tbl_name
You can optimize a table in the same way using the SQL
OPTIMIZE TABLE is easier, but
isamchk is faster.
There is also no possibility of unwanted interaction between a utility
and the server, because the server does all the work when you use
isamchk also has a number of other options you can use to improve
the performance of a table:
-R index_num, --sort-records=index_num
For a full description of the option see section 13.1.1
isamchk invocation syntax.
When using MySQL with log files, you will from time to time want to remove/backup old log files and tell MySQL to start logging on new files. See section 9.2 The update log.
One a Linux (
Redhat) installation, you can use the
mysql-log-rotate script for this. If you installed MySQL
from an RPM distribution, the script should have been installed
On other systems you must install a short script yourself that you
cron to handle log files.
You can force MySQL to start using new log files by using
mysqladmin flush-logs or by using the SQL command
If you are using MySQL 3.21 you must use
The above command does the following:
--log) is used, closes and reopens the log file. (`mysql.log' as default).
--log-update) is used, closes the update log and opens a new log file with a higher sequence number.
If you are using only an update log, you only have to flush the logs and then move away the old update log files to a backup. If you are using the normal logging, you can do something like:
shell> cd mysql-data-directory shell> mv mysql.log mysql.old shell> mysqladmin flush-tables
and then take a backup and remove `mysql.old'.
Go to the first, previous, next, last section, table of contents.