Documentation Home
MySQL 8.0 参考手册  / 第 4 章 MySQL 程序  / 4.6 管理和实用程序  /  4.6.3 myisamchk — MyISAM 表维护实用程序

4.6.3 myisamchk — MyISAM 表维护实用程序

myisamchk实用程序获取有关数据库表的信息或检查、修复或优化它们 。myisamchk使用 MyISAM表(具有 用于存储数据和索引的文件的表) .MYD.MYI

您还可以使用CHECK TABLE andREPAIR TABLE语句来检查和修复MyISAM表。请参阅 第 13.7.2.2 节,“CHECK TABLE 语句”第 13.7.2.5 节,“REPAIR TABLE 语句”

不支持对分区表 使用myisamchk 。

警告

最好在执行表修复操作之前对表进行备份;在某些情况下,该操作可能会导致数据丢失。可能的原因包括但不限于文件系统错误。

像这样调用myisamchk

myisamchk [options] tbl_name ...

options指定您希望 myisamchk执行 的操作。它们在以下部分中描述。您还可以通过调用myisamchk --help获取选项列表。

没有选项,myisamchk只是检查你的表作为默认操作。要获得更多信息或告诉myisamchk采取纠正措施,请按照以下讨论中所述指定选项。

tbl_name是要检查或修复的数据库表。如果 在数据库目录以外的地方 运行myisamchk ,则必须指定数据库目录的路径,因为myisamchk不知道数据库位于何处。事实上,myisamchk实际上并不关心您正在处理的文件是否位于数据库目录中。您可以将对应于数据库表的文件复制到其他某个位置,并在那里对它们执行恢复操作。

如果愿意,您可以在myisamchk命令行 上命名多个表。您也可以通过命名其索引文件(具有.MYI 后缀的文件)来指定表。这使您能够使用模式指定目录中的所有表*.MYI。例如,如果您在数据库目录中,您可以 MyISAM像这样检查该目录中的所有表:

myisamchk *.MYI

如果您不在数据库目录中,则可以通过指定目录路径来检查那里的所有表:

myisamchk /path/to/database_dir/*.MYI

您甚至可以通过指定带有 MySQL 数据目录路径的通配符来检查所有数据库中的所有表:

myisamchk /path/to/datadir/*/*.MYI

快速检查所有 MyISAM表的推荐方法是:

myisamchk --silent --fast /path/to/datadir/*/*.MYI

如果要检查所有MyISAM表并修复任何损坏的表,可以使用以下命令:

myisamchk --silent --force --fast --update-state \
          --key_buffer_size=64M --myisam_sort_buffer_size=64M \
          --read_buffer_size=1M --write_buffer_size=1M \
          /path/to/datadir/*/*.MYI

此命令假定您有超过 64MB 的可用空间。有关使用myisamchk分配内存的更多信息 ,请参阅 第 4.6.3.6 节,“myisamchk 内存使用”

有关使用 myisamchk的其他信息,请参阅 第 7.6 节,“MyISAM 表维护和崩溃恢复”

重要的

You must ensure that no other program is using the tables while you are running myisamchk. The most effective means of doing so is to shut down the MySQL server while running myisamchk, or to lock all tables that myisamchk is being used on.

Otherwise, when you run myisamchk, it may display the following error message:

warning: clients are using or haven't closed the table properly

This means that you are trying to check a table that has been updated by another program (such as the mysqld server) that hasn't yet closed the file or that has died without closing the file properly, which can sometimes lead to the corruption of one or more MyISAM tables.

If mysqld is running, you must force it to flush any table modifications that are still buffered in memory by using FLUSH TABLES. You should then ensure that no one is using the tables while you are running myisamchk

However, the easiest way to avoid this problem is to use CHECK TABLE instead of myisamchk to check tables. See Section 13.7.2.2, “CHECK TABLE Statement”.

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

Table 4.20 myisamchk Options

Option Name Description
--analyze Analyze the distribution of key values
--backup Make a backup of the .MYD file as file_name-time.BAK
--block-search Find the record that a block at the given offset belongs to
--check Check the table for errors
--check-only-changed Check only tables that have changed since the last check
--correct-checksum Correct the checksum information for the table
--data-file-length Maximum length of the data file (when re-creating data file when it is full)
--debug Write debugging log
--decode_bits Decode_bits
--defaults-extra-file Read named option file in addition to usual option files
--defaults-file Read only named option file
--defaults-group-suffix Option group suffix value
--description Print some descriptive information about the table
--extend-check Do very thorough table check or repair that tries to recover every possible row from the data file
--fast Check only tables that haven't been closed properly
--force Do a repair operation automatically if myisamchk finds any errors in the table
--force Overwrite old temporary files. For use with the -r or -o option
--ft_max_word_len Maximum word length for FULLTEXT indexes
--ft_min_word_len Minimum word length for FULLTEXT indexes
--ft_stopword_file Use stopwords from this file instead of built-in list
--HELP Display help message and exit
--help Display help message and exit
--information Print informational statistics about the table that is checked
--key_buffer_size Size of buffer used for index blocks for MyISAM tables
--keys-used A bit-value that indicates which indexes to update
--max-record-length Skip rows larger than the given length if myisamchk cannot allocate memory to hold them
--medium-check Do a check that is faster than an --extend-check operation
--myisam_block_size Block size to be used for MyISAM index pages
--myisam_sort_buffer_size The buffer that is allocated when sorting the index when doing a REPAIR or when creating indexes with CREATE INDEX or ALTER TABLE
--no-defaults Read no option files
--parallel-recover Uses the same technique as -r and -n, but creates all the keys in parallel, using different threads (beta)
--print-defaults Print default options
--quick Achieve a faster repair by not modifying the data file
--read_buffer_size Each thread that does a sequential scan allocates a buffer of this size for each table it scans
--read-only Do not mark the table as checked
--recover Do a repair that can fix almost any problem except unique keys that aren't unique
--safe-recover Do a repair using an old recovery method that reads through all rows in order and updates all index trees based on the rows found
--set-auto-increment Force AUTO_INCREMENT numbering for new records to start at the given value
--set-collation Specify the collation to use for sorting table indexes
--silent Silent mode
--sort_buffer_size 在执行 REPAIR 或使用 CREATE INDEX 或 ALTER TABLE 创建索引时对索引进行排序时分配的缓冲区
--排序索引 按高低顺序对索引树块进行排序
--sort_key_blocks 排序键块
--排序记录 根据特定索引对记录进行排序
--排序恢复 强制 myisamchk 使用排序来解析键,即使临时文件会非常大
--stats_method 指定 MyISAM 索引统计信息收集代码应如何处理 NULL
--tmpdir 用于存放临时文件的目录
--解包 解压一个用 myisampack 打包的表
--更新状态 在 .MYI 文件中存储信息以指示表何时被检查以及表是否崩溃
--冗长 详细模式
- 版本 显示版本信息并退出
--write_buffer_size 写入缓冲区大小