4.5.3 mysqlcheck — A Table Maintenance Program表格维护程序

The mysqlcheck client performs table maintenance: It checks, repairs, optimizes, or analyzes tables.mysqlcheck客户端执行表维护:它检查、修复、优化或分析表。

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). 每个表都被锁定,因此在处理过程中无法用于其他会话,尽管对于检查操作,该表仅用READ锁锁定(有关READWRITE锁的更多信息,请参阅第13.3.6节,“LOCK TABLES和UNLOCK TABLES语句”)。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. 如果使用--databases--all-databases选项来处理一个或多个数据库中的所有表,则调用mysqlcheck可能需要很长时间。(This is also true for the MySQL upgrade procedure if it determines that table checking is needed because it processes tables the same way.)(MySQL升级过程也是如此,如果它确定需要表检查,因为它以相同的方式处理表。)

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必须在mysqld服务器运行时使用,这意味着您不必停止服务器来执行表维护。

mysqlcheck uses the SQL statements CHECK TABLE, REPAIR TABLE, ANALYZE TABLE, and OPTIMIZE TABLE in a convenient way for the user. mysqlcheck以方便用户的方式使用SQL语句CHECK TABLEREPAIR TABLEANALYZE TABLEOPTIMIZE TABLEIt 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”.有关每个语句使用的存储引擎的详细信息,请参阅第13.7.3节,“表维护语句”中对这些语句的描述。

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:在这种情况下,会显示错误消息。例如,如果test.t是一个MEMORY表,尝试检查它会产生以下结果:

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. 如果mysqlcheck无法修复表,请参阅第2.11.13节,“重建或修复表或索引”,了解手动表修复策略。This is the case, for example, for InnoDB tables, which can be checked with CHECK TABLE, but not repaired with REPAIR TABLE.例如,InnoDB表就是这种情况,可以用CHECK TABLE检查,但不能用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 invoke mysqlcheck:调用mysqlcheck的一般方法有三种:

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

If you do not name any tables following db_name or if you use the --databases or --all-databases option, entire databases are checked.如果不在db_name后面命名任何表,或者使用--databases--all-databases选项,则会检查整个数据库。

mysqlcheck has a special feature compared to other client programs. 与其他客户端程序相比,mysqlcheck有一个特殊的功能。The default behavior of checking tables (--check) can be changed by renaming the binary. 通过重命名二进制文件,可以更改检查表的默认行为(--check)。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.如果你想有一个默认情况下修复表的工具,你应该只复制一个名为mysqlrepairmysqlcheck,或者创建一个指向mysqlcheck的符号链接,命名为mysqlrepaire。如果你调用mysqlrepair,它会修复表。

The names shown in the following table can be used to change mysqlcheck default behavior.下表中显示的名称可用于更改mysqlcheck的默认行为。

Command命令Meaning意义
mysqlrepairThe default option is --repair默认选项是--repair
mysqlanalyzeThe default option is --analyze默认选项是--analyze
mysqloptimizeThe default option is --optimize默认选项是--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. mysqlcheck支持以下选项,可以在命令行或选项文件的[mysqlcheck][client]组中指定。For information about option files used by MySQL programs, see Section 4.2.2.2, “Using Option Files”.有关MySQL程序使用的选项文件的信息,请参阅第4.2.2.2节,“使用选项文件”

Table 4.13 mysqlcheck Options选项

Option Name选项名称Description描述Introduced引入版本Deprecated弃用版本
--all-databasesCheck all tables in all databases检查所有数据库中的所有表
--all-in-1Execute a single statement for each database that names all the tables from that database为每个数据库执行一条语句,命名该数据库中的所有表
--analyzeAnalyze the tables分析表格
--auto-repairIf a checked table is corrupted, automatically fix it如果已检查的表已损坏,请自动修复它
--bind-addressUse specified network interface to connect to MySQL Server使用指定的网络接口连接MySQL服务器
--character-sets-dirDirectory where character sets are installed安装字符集的目录
--checkCheck the tables for errors检查表格是否有错误
--check-only-changedCheck only tables that have changed since the last check仅检查自上次检查以来已更改的表
--check-upgradeInvoke CHECK TABLE with the FOR UPGRADE option使用FOR UPGRADE选项调用CHECK TABLE
--compressCompress all information sent between client and server压缩客户端和服务器之间发送的所有信息8.0.18
--compression-algorithmsPermitted compression algorithms for connections to server服务器连接的允许压缩算法8.0.18
--databasesInterpret all arguments as database names将所有参数解释为数据库名称
--debugWrite debugging log写入调试日志
--debug-checkPrint debugging information when program exits程序退出时打印调试信息
--debug-infoPrint debugging information, memory, and CPU statistics when program exits程序退出时打印调试信息、内存和CPU统计信息
--default-authAuthentication plugin to use要使用的身份验证插件
--default-character-setSpecify default character set指定默认字符集
--defaults-extra-fileRead named option file in addition to usual option files除了常规选项文件外,还读取命名选项文件
--defaults-fileRead only named option file只读命名选项文件
--defaults-group-suffixOption group suffix value选项组后缀值
--enable-cleartext-pluginEnable cleartext authentication plugin启用明文身份验证插件
--extendedCheck and repair tables检查和维修表
--fastCheck only tables that have not been closed properly仅检查未正确关闭的表
--forceContinue even if an SQL error occurs即使发生SQL错误,也要继续
--get-server-public-keyRequest RSA public key from server从服务器请求RSA公钥
--helpDisplay help message and exit显示帮助消息并退出
--hostHost on which MySQL server is locatedMySQL服务器所在的主机
--login-pathRead login path options from .mylogin.cnf从.mylogin.cnf读取登录路径选项
--medium-checkDo a check that is faster than an --extended operation执行比--extended操作更快的检查
--no-defaultsRead no option files不读取选项文件
--optimizeOptimize the tables优化表格
--passwordPassword to use when connecting to server连接到服务器时使用的密码
--pipeConnect to server using named pipe (Windows only)使用命名管道连接到服务器(仅限Windows)
--plugin-dirDirectory where plugins are installed安装插件的目录
--portTCP/IP port number for connection用于连接的TCP/IP端口号
--print-defaultsPrint default options打印默认选项
--protocolTransport protocol to use要使用的传输协议
--quickThe fastest method of checking最快的检查方法
--repairPerform a repair that can fix almost anything except unique keys that are not unique执行修复,几乎可以修复任何东西,除了不是唯一的唯一密钥
--server-public-key-pathPath name to file containing RSA public key包含RSA公钥的文件的路径名
--shared-memory-base-nameShared-memory name for shared-memory connections (Windows only)共享内存连接的共享内存名称(仅限Windows)
--silentSilent mode静音模式
--skip-databaseOmit this database from performed operations从执行的操作中省略此数据库
--socketUnix socket file or Windows named pipe to use要使用的Unix套接字文件或Windows命名管道
--ssl-caFile that contains list of trusted SSL Certificate Authorities包含受信任SSL证书颁发机构列表的文件
--ssl-capathDirectory that contains trusted SSL Certificate Authority certificate files包含受信任的SSL证书颁发机构证书文件的目录
--ssl-certFile that contains X.509 certificate包含X.509证书的文件
--ssl-cipherPermissible ciphers for connection encryption连接加密的允许密码
--ssl-crlFile that contains certificate revocation lists包含证书吊销列表的文件
--ssl-crlpathDirectory that contains certificate revocation-list files包含证书吊销列表文件的目录
--ssl-fips-modeWhether to enable FIPS mode on client side是否在客户端启用FIPS模式
--ssl-keyFile that contains X.509 key包含X.509密钥的文件
--ssl-modeDesired security state of connection to server与服务器连接的所需安全状态
--tablesOverrides the --databases or -B option覆盖--databases或-B选项
--tls-ciphersuitesPermissible TLSv1.3 ciphersuites for encrypted connections加密连接允许的TLSv1.3密码套件8.0.16
--tls-versionPermissible TLS protocols for encrypted connections加密连接的允许TLS协议
--use-frmFor repair operations on MyISAM tables用于MyISAM表的修复操作
--userMySQL user name to use when connecting to server连接到服务器时使用的MySQL用户名
--verboseVerbose mode详细模式
--versionDisplay version information and exit显示版本信息并退出
--write-binlogLog ANALYZE, OPTIMIZE, REPAIR statements to binary log. --skip-write-binlog adds NO_WRITE_TO_BINLOG to these statements将日志分析、优化、修复语句转换为二进制日志。--skip-write-binlog将NO_WRITE_TO_BINLOG添加到这些语句中
--zstd-compression-levelCompression level for connections to server that use zstd compression使用zstd压缩的服务器连接的压缩级别8.0.18