mysqlcheck is a maintenance tool that allows you to check, repair, analyze and optimize multiple tables from the command line.

It is essentially a commandline interface to the CHECK TABLE, REPAIR TABLE, ANALYZE TABLE and OPTIMIZE TABLE commands, and so, unlike myisamchk and aria_chk, requires the server to be running.

This tool does not work with partitioned tables.


./client/mysqlcheck [OPTIONS] database [tables]


./client/mysqlcheck [OPTIONS] --databases DB1 [DB2 DB3...]


./client/mysqlcheck [OPTIONS] --all-databases

mysqlcheck can be used to CHECK (-c, -m, -C), REPAIR (-r), ANALYZE (-a), or OPTIMIZE (-o) tables. Some of the options (like -e or -q) can be used at the same time. Not all options are supported by all storage engines.

The -c, -r, -a and -o options are exclusive to each other.

The option --check will be used by default, if no other options were specified. You can change the default behavior by making a symbolic link to the binary, or copying it somewhere with another name, the alternatives are:

mysqlrepairThe default option will be -r (--repair)
mysqlanalyzeThe default option will be -a (--analyze)
mysqloptimizeThe default option will be -o (--optimize)

The following groups are read from the my.cnf files: [mysqlcheck] and [client].

The following options to handle option files may be given as the first argument:

--print-defaultsPrint the program argument list and exit.
--no-defaultsDon't read default options from any option file.
--defaults-file=nameOnly read default options from the given file name.
--defaults-extra-file=nameRead the file name after the global files are read.

Main arguments are:

-A, --all-databasesCheck all the databases. This is the same as --databases with all databases selected.
-1, --all-in-1Instead of issuing one query for each table, use one query per database, naming all tables in the database in a comma-separated list.
-a, --analyzeAnalyze given tables.
--auto-repairIf a checked table is corrupted, automatically fix it. Repairing will be done after all tables have been checked.
--character-sets-dir=nameDirectory where character set files are installed.
-c, --checkCheck table for errors.
-C, --check-only-changedCheck only tables that have changed since last check or haven't been closed properly.
-g, --check-upgradeCheck tables for version-dependent changes. May be used with --auto-repair to correct tables requiring version-dependent updates. Automatically enables the --fix-db-names and --fix-table-names options. Used when upgrading
--compressCompress all information sent between the client and server if both support compression.
-B, --databasesCheck several databases. Note that normally mysqlcheck treats the first argument as a database name, and following arguments as table names. With this option, no tables are given, and all name arguments are regarded as database names.
-# , --debug[=name]Output debug log. Often this is 'd:t:o,filename'.
--debug-checkCheck memory and open file usage at exit.
--debug-infoPrint some debug info at exit.
--default-auth=pluginDefault authentication client-side plugin to use.
--default-character-set=nameSet the default character set.
-e, --extendedIf you are using this option with --check, it will ensure that the table is 100 percent consistent, but will take a long time. If you are using this option with --repair, it will force using the old, slow, repair with keycache method, instead of the much faster repair by sorting.
-F, --fastCheck only tables that haven't been closed properly.
--fix-db-namesConvert database names to the format used since MySQL 5.1. Only database names that contain special characters are affected. Used when upgrading from an old MySQL version.
--fix-table-namesConvert table names (including views) to the format used since MySQL 5.1. Only table names that contain special characters are affected. Used when upgrading from an old MySQL version.
--flushFlush each table after check. This is useful if you don't want to have the checked tables take up space in the caches after the check.
-f, --forceContinue even if we get an SQL error.
-?, --helpDisplay this help message and exit.
-h name, --host=nameConnect to the given host.
-m, --medium-checkFaster than extended-check, but only finds 99.99 percent of all errors. Should be good enough for most cases.
-o, --optimizeOptimize tables.
-p, --password[=name]Password to use when connecting to the server. If you use the short option form (-p), you cannot have a space between the option and the password. If you omit the password value following the --password or -p option on the command line, mysqlcheck prompts for one. Specifying a password on the command line should be considered insecure. You can use an option file to avoid giving the password on the command line.
-Z, --persistentWhen using ANALYZE TABLE (--analyze), uses the PERSISTENT FOR ALL option, which forces Engine-independent Statistics for this table to be updated. Added in MariaDB 10.1.10
-W, --pipeOn Windows, connect to the server via a named pipe. This option applies only if the server supports named-pipe connections.
--plugin-dirDirectory for client-side plugins.
-P num, --port=numPort number to use for connection or 0 for default to, in order of preference, my.cnf, $MYSQL_TCP_PORT, /etc/services, built-in default (3306).
--process-tablesPerform the requested operation (check, repair, analyze, optimize) on tables. Enabled by default. Use --skip-process-tables to disable. Added in MariaDB 10.0.18 and MariaDB 5.5.43.
--process-views[=val]Perform the requested operation (only CHECK VIEW or REPAIR VIEW). Possible values are NO, YES (correct the checksum, if necessary, add the mariadb-version field), UPGRADE_FROM_MYSQL (same as YES and toggle the algorithm MERGE<->TEMPTABLE. Added in MariaDB 10.0.18 and MariaDB 5.5.43.
--protocol=nameThe connection protocol (tcp, socket, pipe, memory) to use for connecting to the server. Useful when other connection parameters would cause a protocol to be used other than the one you want.
-q, --quickIf you are using this option with CHECK TABLE, it prevents the check from scanning the rows to check for wrong links. This is the fastest check. If you are using this option with REPAIR TABLE, it will try to repair only the index tree. This is the fastest repair method for a table.
-r, --repairCan fix almost anything except unique keys that aren't unique.
--shared-memory-base-nameShared-memory name to use for Windows connections using shared memory to a local server (started with the --shared-memory option). Case-sensitive.
-s, --silentPrint only error messages.
--skip-databaseDon't process the database (case-sensitive) specified as argument.
-S name, --socket=nameFor connections to localhost, the Unix socket file to use, or, on Windows, the name of the named pipe to use.
--sslEnable TLS for secure connection (automatically enabled with other flags). Disable with '--skip-ssl'.
--ssl-ca=nameCA file in PEM format (check OpenSSL docs, implies --ssl).
--ssl-capath=nameCA directory (check OpenSSL docs, implies --ssl).
--ssl-cert=nameX509 cert in PEM format (implies --ssl).
--ssl-cipher=nameSSL cipher to use (implies --ssl).
--ssl-key=nameX509 key in PEM format (implies --ssl).
--ssl-crl=nameCertificate revocation list (implies --ssl).
--ssl-crlpath=nameCertificate revocation list path (implies --ssl).
--ssl-verify-server-certVerify server's "Common Name" in its cert against hostname used when connecting. This option is disabled by default.
--tablesOverrides the --databases or -B option such that all name arguments following the option are regarded as table names.
--use-frmFor repair operations on MyISAM tables, get table structure from .frm file, so the table can be repaired even if the .MYI header is corrupted.
-u, --user=nameUser for login if not current user.
-v, --verbosePrint info about the various stages. You can give this option several times to get even more information. See mysqlcheck and verbose, below.
-V, --versionOutput version information and exit.
--write-binlogWrite ANALYZE, OPTIMIZE and REPAIR TABLE commands to the binary log. Enabled by default; use --skip-write-binlog when commands should not be sent to replication slaves.


Default values

To see the default values for the options and also to see the arguments you get from configuration files you can do:

./client/mysqlcheck --print-defaults
./client/mysqlcheck --help

mysqlcheck and auto-repair

When running mysqlcheck with --auto-repair (as done by mysql_upgrade), mysqlcheck will first check all tables and then in a separate phase repair those that failed the check.

mysqlcheck and all-databases

mysqlcheck --all-databases will ignore the internal log tables general_log and slow_log as these can't be checked, repaired or optimized.

mysqlcheck and verbose

Using one --verbose option will give you more information about what mysqlcheck is doing.

Using two --verbose options will also give you connection information.

MariaDB starting with 10.0.14

If you use three --verbose options you will also get, on stdout, all ALTER, RENAME, and CHECK commands that mysqlcheck executes.


Comments loading...