本节描述了如何重建或修复表或索引,这可能由于以下原因而需要:This section describes how to rebuild or repair tables or indexes, which may be necessitated by:
MySQL 对数据类型或字符集的处理方式发生变化。例如,排序规则中的错误可能已经被更正,因此需要重建表以更新使用该排序规则的字符列的索引。Changes to how MySQL handles data types or character sets. For example, an error in a collation might have been corrected, necessitating a table rebuild to update the indexes for character columns that use the collation.
由
CHECK TABLE或 mysqlcheck 报告的所需表修复或升级。Required table repairs or upgrades reported byCHECK TABLEor mysqlcheck.
重建表的方法包括:Methods for rebuilding a table include:
如果由于二进制(就地)升级或降级后不同版本的 MySQL 无法处理表而需要重建表,则必须使用转储和重新加载方法。在升级或降级之前使用原始版本的 MySQL 转储表before然后在升级或降级之后重新加载表after用于连接到服务器的 MySQL 帐户多因素认证因子 1 的密码。密码值是可选的。如果未提供,客户端程序会提示输入密码。如果提供,密码和其前的If you are rebuilding tables because a different version of MySQL cannot handle them after a binary (in-place) upgrade or downgrade, you must use the dump-and-reload method. Dump the tables before upgrading or downgrading using your original version of MySQL. Then reload the tables after upgrading or downgrading.
如果您仅为了重建索引而使用转储重载方法重建表,您可以在升级或降级之前或之后执行转储。但重载仍必须在之后进行。If you use the dump-and-reload method of rebuilding tables only for the purpose of rebuilding indexes, you can perform the dump either before or after upgrading or downgrading. Reloading still must be done afterward.
如果因为 CHECK TABLE 操作显示表需要升级而需要重建 InnoDB 表,请使用 mysqldump 创建转储文件,并使用 mysql 重新加载该文件。如果 CHECK TABLE 操作显示存在损坏或导致 InnoDB 失败,请参阅 第 17.20.3 节,“强制 InnoDB 恢复”,了解如何使用 innodb_force_recovery 选项重新启动 InnoDB。要了解 CHECK TABLE 可能遇到的问题类型,请参阅 第 15.7.3.2 节,“CHECK TABLE 语句” 中的 InnoDB 注释。If you need to rebuild an InnoDB table
because a CHECK TABLE operation
indicates that a table upgrade is required, use
mysqldump to create a dump file and
mysql to reload the file. If the
CHECK TABLE operation indicates
that there is a corruption or causes InnoDB
to fail, refer to Section 17.20.3, “Forcing InnoDB Recovery” for
information about using the
innodb_force_recovery option to
restart InnoDB. To understand the type of
problem that CHECK TABLE may be
encountering, refer to the InnoDB notes in
Section 15.7.3.2, “CHECK TABLE Statement”.
要通过导出和重新加载来重建表,使用 mysqldump 创建一个转储文件,并使用 mysql 重新加载该文件:To rebuild a table by dumping and reloading it, use mysqldump to create a dump file and mysql to reload the file:
mysqldumpdb_namet1 > dump.sql mysqldb_name< dump.sql
要重建单个数据库中的所有表,请指定数据库名称而不附加任何表名:To rebuild all the tables in a single database, specify the database name without any following table name:
mysqldumpdb_name> dump.sql mysqldb_name< dump.sql
要重建所有数据库中的所有表,请使用--all-databases选项:To rebuild all tables in all databases, use the
--all-databases option:
mysqldump --all-databases > dump.sql mysql < dump.sql
要使用ALTER TABLE重建表,请使用“null”修改;也就是说,使用一个ALTER
TABLE语句来“更改”表以使用它已经拥有的存储引擎。例如,如果t1是一个InnoDB表,请使用以下语句:To rebuild a table with ALTER
TABLE, use a “null” alteration; that is,
an ALTER TABLE statement that
“changes” the table to use the storage engine that
it already has. For example, if t1 is an
InnoDB table, use this statement:
ALTER TABLE t1 ENGINE = InnoDB;
如果您不确定在 ALTER TABLE 语句中应指定哪种存储引擎,请使用 SHOW CREATE TABLE 来显示表定义。If you are not sure which storage engine to specify in the
ALTER TABLE statement, use
SHOW CREATE TABLE to display the
table definition.
REPAIR TABLE 方法仅适用于 MyISAM、ARCHIVE 和 CSV 表。The REPAIR TABLE method is only
applicable to MyISAM,
ARCHIVE, and CSV tables.
如果表检查操作显示表存在损坏或需要升级,可以使用 REPAIR TABLE。例如,要修复一个 MyISAM 表,可以使用以下语句:You can use REPAIR TABLE if the
table checking operation indicates that there is a corruption or
that an upgrade is required. For example, to repair a
MyISAM table, use this statement:
REPAIR TABLE t1;
mysqlcheck --repair 提供对 REPAIR TABLE 语句的命令行访问。这可以是一种更方便的修复表的方法,因为你可以使用 --databases 或 --all-databases 选项分别修复特定数据库或所有数据库中的所有表:mysqlcheck --repair provides command-line
access to the REPAIR TABLE
statement. This can be a more convenient means of repairing
tables because you can use the
--databases or
--all-databases option to
repair all tables in specific databases or all databases,
respectively:
mysqlcheck --repair --databases db_name ...
mysqlcheck --repair --all-databases