Go to the first, previous, next, last section, table of contents.


9 Crash recovery

The file format that MySQL uses to store data has been extensively tested, but there are always circumstances that may cause database tables to become corrupted:

This chapter describes how to check for and deal with data corruption in MySQL databases.

When performing crash recovery, it is important to understand that each table tbl_name in a database corresponds to three files in the database directory:

File Purpose
`tbl_name.frm' Table definition (form) file
`tbl_name.ISD' Data file
`tbl_name.ISM' Index 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.

To detect and correct problems, use the isamchk utility, which is invoked like this:

shell> isamchk [options] tbl_name

The options specify what you want isamchk to do. With no options, it simply checks your table. To get more information or to tell isamchk to take corrective action, specify options as described in the sections below.

tbl_name is the database table you want to check. If you do not run isamchk 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 directory, you can check all the tables by specifying the path to the database 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

9.1 How to check tables for errors

To check a table, use the following commands:

isamchk tbl_name
This finds 99.99% of all errors. What it can't find is corruption that involves ONLY the data file (which is very unusual). If you want to check a table, you should normally run isamchk without options or with either the -s or --silent option.
isamchk -e tbl_name
This does a complete and thorough check of all data (-e = "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. isamchk will normally stop after the first error it finds. If you want to obtain more information, you can add the --verbose (-v) option. This causes isamchk to 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
Like the previous command, but the -i option tells isamchk to prints some informational statistics, too.

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. The -s option causes isamchk to run in silent mode, printing messages only when errors occur.

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.

Of course, 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".)

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.

Naturally, 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' 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.

9.2 How to repair tables

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. The isamchk external utility can usually detect and fix most things that go wrong.

If you are going to use isamchk on very large files, you should first decide how much memory you want it to use (isamchk runs faster with more memory). 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 sortbuffer=16M -O keybuffer=16M \
           -O readbuffer=1M -O writebuffer=1M ...

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 that 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

Run isamchk *.ISM or (isamchk -e *.ISM if you have more time). Use the -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 out of memory errors), or if isamchk crashes, go to Stage 3.

Stage 2: Easy safe repair

First, try 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 following procedure:

  1. Make a backup of the data file before continuing.
  2. Use isamchk -r tbl_name (-r means "recovery mode"). This will remove incorrect records and deleted records from the data file and reconstruct the index file.
  3. If the preceding step fails, use 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 out of 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:

  1. Move the data file to some safe place.
  2. Use the table description file to create new (empty) data and index files:
    shell> mysql db_name
    mysql> DELETE FROM tbl_name;
    mysql> quit
    
  3. Copy (don't move) the old data file back onto the newly created data file.

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.

  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 isamchk -r.
  2. If you don't 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 description and index files from the other database to your crashed database. This gives you new description and index files, but leaves the data file alone. Go back to Stage 2 and attempt to reconstruct the index file.


Go to the first, previous, next, last section, table of contents.