6.5.3 mysqlcheck — A Table Maintenance Program (original) (raw)

6.5.3 mysqlcheck — A Table Maintenance Program

The mysqlcheck client performs table maintenance: It checks, repairs, optimizes, or analyzes tables.

Each table is locked and therefore unavailable to other sessions while it is being processed, although for check operations, the table is locked with a READ lock only (seeSection 15.3.6, “LOCK TABLES and UNLOCK TABLES Statements”, for more information aboutREAD and WRITE locks). Table maintenance operations can be time-consuming, particularly for large tables. If you use the--databases or--all-databases option to process all tables in one or more databases, an invocation ofmysqlcheck might take a long time. (This is also true for the MySQL upgrade procedure if it determines that table checking is needed because it processes tables the same way.)

mysqlcheck must be used when themysqld server is running, which means that you do not have to stop the server to perform table maintenance.

mysqlcheck uses the SQL statementsCHECK TABLE,REPAIR TABLE,ANALYZE TABLE, andOPTIMIZE TABLE in a convenient way for the user. It determines which statements to use for the operation you want to perform, and then sends the statements to the server to be executed. For details about which storage engines each statement works with, see the descriptions for those statements inSection 15.7.3, “Table Maintenance Statements”.

All storage engines do not necessarily support all four maintenance operations. In such cases, an error message is displayed. For example, if test.t is anMEMORY table, an attempt to check it produces this result:

$> mysqlcheck test t
test.t
note     : The storage engine for the table doesn't support check

If mysqlcheck is unable to repair a table, see Section 3.14, “Rebuilding or Repairing Tables or Indexes” for manual table repair strategies. This is the case, for example, forInnoDB tables, which can be checked withCHECK TABLE, but not repaired with REPAIR TABLE.

Caution

It is best to make a backup of a table before performing a table repair operation; under some circumstances the operation might cause data loss. Possible causes include but are not limited to file system errors.

There are three general ways to invokemysqlcheck:

mysqlcheck [options] db_name [tbl_name ...]
mysqlcheck [options] --databases db_name ...
mysqlcheck [options] --all-databases

If you do not name any tables following_dbname_ or if you use the--databases or--all-databases option, entire databases are checked.

mysqlcheck has a special feature compared to other client programs. The default behavior of checking tables (--check) can be changed by renaming the binary. If you want to have a tool that repairs tables by default, you should just make a copy ofmysqlcheck namedmysqlrepair, or make a symbolic link tomysqlcheck namedmysqlrepair. If you invokemysqlrepair, it repairs tables.

The names shown in the following table can be used to changemysqlcheck default behavior.

Command Meaning
mysqlrepair The default option is --repair
mysqlanalyze The default option is --analyze
mysqloptimize The default option is --optimize

mysqlcheck supports the following options, which can be specified on the command line or in the[mysqlcheck] and [client] groups of an option file. For information about option files used by MySQL programs, see Section 6.2.2.2, “Using Option Files”.