Home / Alt manpages / myisamchk(1)

  • myisamchk(1)
  • User command
  • linux

Check and Repair MyISAM Tables Safely with myisamchk

You will finish with a read-only check for a MyISAM table, a controlled repair command for the case where one is needed, and a verification pass afterwards. The examples match MariaDB 10.11 as installed here, from package mariadb-server 1:10.11.14-0ubuntu0.24.04.1. Allow about 15 minutes for a check, plus however long the table takes to copy or repair.

You need shell access, enough permission to read and possibly write the table files, and a known path to a MyISAM table. A MyISAM table consists of at least a .MYD data file and a .MYI index file. This tool does not support partitioned tables.

Safety boundary

Never run a check or repair while MariaDB or another program can update the table. Use the database server's normal maintenance procedure to stop it, or arrange an appropriate lock. A repair can lose data, so take a backup first. The commands below do not stop a service for you.

1. Confirm the installed tool and package

Check the binary and package version before relying on option details. These are ordinary read-only commands:

$ command -v myisamchk
/usr/bin/myisamchk
$ dpkg-query -W -f='${Package} ${Version}\n' mariadb-server
mariadb-server 1:10.11.14-0ubuntu0.24.04.1

The manual page identifies the interface as MariaDB 10.11. The installed program accepts --help; use it when you need the complete option list:

$ myisamchk --help | sed -n '1,24p'
  -a, --analyze       Analyze distribution of keys. Will make some joins in
                      MySQL faster. You can check the calculated distribution.
  -c, --check         Check table for errors.
  -d, --description   Prints some information about table.
  -r, --recover       Can fix almost anything except unique keys that aren't
                      unique.

On this particular installation, myisamchk --version aborts instead of printing a version. Do not use that result as a package check; the package query above is the reliable local check. Report the crash to the package maintainer if it affects your operation.

2. Identify the exact table files

Replace the placeholders in the next command with a real database directory and table name. The command only lists files:

$ DB_DIR='/var/lib/mysql/DB_NAME'
$ TABLE='TABLE_NAME'
$ ls -l "$DB_DIR/$TABLE.MYD" "$DB_DIR/$TABLE.MYI"
-rw-r----- 1 mysql mysql  ... /var/lib/mysql/DB_NAME/TABLE_NAME.MYD
-rw-r----- 1 mysql mysql  ... /var/lib/mysql/DB_NAME/TABLE_NAME.MYI

Do not assume that a database directory is under /var/lib/mysql. Confirm the data directory and table name from your MariaDB configuration or operational records. You can also pass several .MYI files, but an unquoted wildcard expands before myisamchk runs, so inspect the expansion first when using one.

Checkpoint: you should have both files, and the table must be out of use before proceeding. If either file is missing, stop and investigate instead of trying a repair.

3. Run a conservative check

With the table quiescent, run the default check explicitly and prevent the tool from marking the table as checked:

$ sudo myisamchk --check --read-only "$DB_DIR/$TABLE.MYI"
Checking MyISAM file: /var/lib/mysql/DB_NAME/TABLE_NAME
Data records:       ...
Deleted blocks:     ...
myisamchk: OK

The exact statistics depend on the table. A successful check normally ends with OK and a zero exit status. The --read-only option is -T; it stops the check from recording checked state in the index file. sudo is only an example for a root-owned data directory. Use the least privilege that can read the files.

If you need a faster first pass over a directory, --fast checks only tables that were not closed properly. It is a triage option, not proof that every table is sound:

$ sudo myisamchk --silent --fast --read-only "$DB_DIR"/*.MYI
$ printf 'check status: %s\n' "$?"
check status: 0

With --silent, normal output is suppressed, so the exit status is the useful result. Remove --silent while investigating a failure. For a slower and more thorough check, use --extend-check, but reserve it for cases where the normal or medium check does not explain the problem.

4. Capture information before changing anything

Get a description and verbose statistics before a repair. This is read-only and gives you a baseline:

$ sudo myisamchk --description --verbose --read-only "$DB_DIR/$TABLE.MYI"
$ sudo myisamchk --information --read-only "$DB_DIR/$TABLE.MYI"

The output includes details such as data records, key information and free space. Save it with your incident notes if the table is damaged. It is also useful for estimating repair resources. Do not treat a large free-space figure as corruption by itself; it can simply reflect deleted or shortened rows.

5. Prepare a repair with a recoverable copy

Warning

--recover changes the table files. Make a filesystem-level backup of both the .MYD and .MYI files while the table is stopped or otherwise safely quiescent. Put the backup on separate storage if possible:

$ BACKUP_DIR='/srv/backup/myisam-YYYYMMDD-HHMM'
$ sudo install -d -m 700 "$BACKUP_DIR"
$ sudo cp --preserve=all "$DB_DIR/$TABLE.MYD" "$DB_DIR/$TABLE.MYI" "$BACKUP_DIR"

Check that both backup files exist and compare their sizes before proceeding:

$ sudo ls -lh "$BACKUP_DIR/$TABLE.MYD" "$BACKUP_DIR/$TABLE.MYI"
$ sudo du -sh "$BACKUP_DIR"
...  # confirm the expected files and available space

The backup is your recovery path. To undo a failed repair, stop MariaDB again, move the damaged pair aside, and copy the corresponding pair from BACKUP_DIR back to the original directory with ownership and permissions preserved. Do not restore files over a running server.

6. Repair only after the checkpoint

Run the normal repair with an explicit temporary directory on a filesystem with space:

$ TMP_DIR='/srv/tmp/myisamchk'
$ sudo install -d -m 700 "$TMP_DIR"
$ sudo myisamchk --recover --backup --tmpdir="$TMP_DIR" "$DB_DIR/$TABLE.MYI"
$ printf 'repair status: %s\n' "$?"
repair status: 0

--backup makes an additional backup of the data file with a time suffix before repair. --recover repairs by sorting keys in the usual case. It can need substantial space: the manual describes a possible second copy of the data file, a replacement index, and sort space in TMP_DIR. Check both the original filesystem and the temporary filesystem before starting.

If disk space is the limiting factor, --quick avoids modifying the data file and recreates only the index, but it is not a general cure for damaged data. --safe-recover uses the older, slower recovery method and can handle some cases where normal recovery cannot. Use --extend-check as a repair option only as a last resort: the manual warns that it can recover garbage rows as well as valid rows.

7. Verify the repaired table

Keep the server stopped or the table locked until verification is complete. Run a fresh check without changing checked state:

$ sudo myisamchk --check --read-only "$DB_DIR/$TABLE.MYI"
$ printf 'verification status: %s\n' "$?"
verification status: 0

Repeat the description command and compare it with the baseline. Only then follow your normal procedure to return MariaDB to service. If the check still fails, do not loop through repair modes blindly. Preserve the files, restore the backup if the repair made matters worse, and investigate the reported error with a copy.

Full-text indexes need one extra check. If the server uses non-default ft_min_word_len, ft_max_word_len or ft_stopword_file, pass the same values to myisamchk when an operation rebuilds indexes. Otherwise full-text queries can stop matching as expected. A server-side REPAIR TABLE or OPTIMIZE TABLE may be safer when the server can perform the operation itself, because it knows its own full-text settings.

Done means

  • You identified matching .MYD and .MYI files and confirmed the installed package version.
  • No process could update the table during checking, backup, repair or verification.
  • A read-only check returned status 0 before and after the operation.
  • You kept a separate backup and know how to restore it with the server stopped.
  • You allowed for index, data and temporary sort space before using --recover.
  • Full-text settings were matched if the repair rebuilt FULLTEXT indexes.