8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

正文为中文,英文原文默认隐藏。点击任意中文段落即可展开 / 收起对应英文原文。

打开官方英文原文 ↗

译文由机器翻译生成,仅供理解参考;有疑问请以官方英文原文为准。

MySQL 8.4 参考手册 / 9 备份与恢复 / 9.6 MyISAM 表维护和崩溃恢复 / 9.6.1 使用 myisamchk 进行崩溃恢复
中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 87%

9.6.1 使用 myisamchk 进行崩溃恢复9.6.1 Using myisamchk for Crash Recovery

本节介绍了如何检查和处理 MySQL 数据库中的数据损坏问题。如果您的表格经常损坏,您应尝试找出原因。请参见 B.3.3.3 节,“如果 MySQL 持续崩溃该怎么办”。This section describes how to check for and deal with data corruption in MySQL databases. If your tables become corrupted frequently, you should try to find the reason why. See Section B.3.3.3, “What to Do If MySQL Keeps Crashing”.

有关 MyISAM 表如何可能损坏的解释,请参见 18.2.4 节,“MyISAM 表问题”。For an explanation of how MyISAM tables can become corrupted, see Section 18.2.4, “MyISAM Table Problems”.

如果您在禁用外部锁(默认设置)的情况下运行 mysqld,则无法可靠地使用 myisamchk 在 mysqld 正在使用相同表时检查表。如果您能够确保在运行 myisamchk 时,没有人通过 mysqld 访问这些表,那么您只需在开始检查表之前执行 mysqladmin flush-tables。如果您无法保证这一点,则必须在检查表时停止 mysqld。如果您在 myisamchk 检查 mysqld 正在同时更新的表时,您可能会收到表已损坏的警告,即使表实际上并未损坏。If you run mysqld with external locking disabled (which is the default), you cannot reliably use myisamchk to check a table when mysqld is using the same table. If you can be certain that no one can access the tables using mysqld while you run myisamchk, you only have to execute mysqladmin flush-tables before you start checking the tables. If you cannot guarantee this, you must stop mysqld while you check the tables. If you run myisamchk to check tables that mysqld is updating at the same time, you may get a warning that a table is corrupt even when it is not.

如果服务器以启用外部锁定的方式运行,您可以随时使用 myisamchk 检查表。在这种情况下,如果服务器尝试更新 myisamchk 正在使用的表,服务器会等待 myisamchk 完成后才继续。If the server is run with external locking enabled, you can use myisamchk to check tables at any time. In this case, if the server tries to update a table that myisamchk is using, the server waits for myisamchk to finish before it continues.

如果你使用 myisamchk 来修复或优化表,你 必须 始终确保 mysqld 服务器没有使用该表(如果禁用了外部锁定,这条也适用)。如果你不停止 mysqld,你至少应该在运行 myisamchk 之前执行一次 mysqladmin flush-tables。如果服务器和 myisamchk 同时访问表,你的表 可能会损坏。If you use myisamchk to repair or optimize tables, you must always ensure that the mysqld server is not using the table (this also applies if external locking is disabled). If you do not stop mysqld, you should at least do a mysqladmin flush-tables before you run myisamchk. Your tables may become corrupted if the server and myisamchk access the tables simultaneously.

在执行崩溃恢复时,重要的是要理解数据库中的每个 MyISAM 表 tbl_name 对应于数据库目录中如下表所示的三个文件。When performing crash recovery, it is important to understand that each MyISAM table tbl_name in a database corresponds to the three files in the database directory shown in the following table.

文件File 目的Purpose
tbl_name.MYD 数据文件Data file
tbl_name.MYI 索引文件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.

myisamchk 的工作原理是通过逐行创建 .MYD 数据文件的副本来进行的。它通过删除旧的 .MYD 文件并将新文件重命名为原文件名来结束修复阶段。如果使用 --quick,myisamchk 不会创建临时的 .MYD 文件,而是假定 .MYD 文件是正确的,只生成新的索引文件而不更改 .MYD 文件。这是安全的,因为 myisamchk 会自动检测 .MYD 文件是否损坏,如果损坏则中止修复。你也可以为 myisamchk 指定两次 --quick 选项。在这种情况下,myisamchk 不会在某些错误(例如重复键错误)上中止,而是尝试通过修改 .MYD 文件来解决它们。通常,仅在磁盘空闲空间不足以执行正常修复时,使用两个 --quick 选项才有用。在这种情况下,在运行 myisamchk 之前,你至少应先备份该表。myisamchk works by creating a copy of the .MYD data file row by row. It ends the repair stage by removing the old .MYD file and renaming the new file to the original file name. If you use --quick, myisamchk does not create a temporary .MYD file, but instead assumes that the .MYD file is correct and generates only a new index file without touching the .MYD file. This is safe, because myisamchk automatically detects whether the .MYD file is corrupt and aborts the repair if it is. You can also specify the --quick option twice to myisamchk. In this case, myisamchk does not abort on some errors (such as duplicate-key errors) but instead tries to resolve them by modifying the .MYD file. Normally the use of two --quick options is useful only if you have too little free disk space to perform a normal repair. In this case, you should at least make a backup of the table before running myisamchk.