content_image/760637.jpg content_image/760638.jpg
## 4.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 (see Section 13.3.6, “LOCK TABLES and UNLOCK TABLES Statements” , for more information about READ  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 of mysqlcheck  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 the mysqld  server is running, which means that you do not have to stop the server to perform table maintenance. 

mysqlcheck  uses the SQL statements CHECK TABLE , REPAIR TABLE , ANALYZE TABLE , and OPTIMIZE 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 in Section 13.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 an MEMORY  table, an attempt to check it produces this result: 

shell> 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 2.11.13, “Rebuilding or Repairing Tables or Indexes”  for manual table repair strategies. This is the case, for example, for InnoDB  tables, which can be checked with CHECK TABLE , but not repaired with REPAIR TABLE . 

<img src='content_image/760637.jpg'>

There are three general ways to invoke mysqlcheck : 

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

## 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. 

If you do not name any tables following db_name  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 of mysqlcheck  named mysqlrepair , or make a symbolic link to mysqlcheck  named mysqlrepair . If you invoke mysqlrepair , it repairs tables. 

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

<img src='content_image/760638.jpg'>

