Mysql Repair Commands For DevOps

These are the Best Commands to repair any mysql DBs.

4.5.3 mysqlcheck — A Table Maintenance Program

The mysqlcheck client performs table maintenance: It checks, repairs, optimizes, or analyzes tables.

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). 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. (This is also true for the MySQL upgrade procedure if it determines that table checking is needed because it processes tables the same way.)

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 uses the SQL statements CHECK TABLEREPAIR TABLEANALYZE TABLE, and OPTIMIZE TABLE in a convenient way for the user. It 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”.

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:

$ mysqlcheck test ttest.tnote     : 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. This is the case, for example, for InnoDB tables, which can be checked with CHECK TABLE, but not repaired with 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 [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.

mysqlcheck has a special feature compared to other client programs. The default behavior of checking tables (--check) can be changed by renaming the binary. 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.

The names shown in the following table can be used to change mysqlcheck default behavior.

CommandMeaning
mysqlrepairThe default option is --repair
mysqlanalyzeThe default option is --analyze
mysqloptimizeThe default option is --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. For information about option files used by MySQL programs, see Section 4.2.2.2, “Using Option Files”.

Table 4.13 mysqlcheck Options

Option NameDescriptionIntroducedDeprecated
--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  
--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  
--compressCompress all information sent between client and server 8.0.18
--compression-algorithmsPermitted compression algorithms for connections to server8.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  
--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  
--get-server-public-keyRequest RSA public key from server  
--helpDisplay help message and exit  
--hostHost on which MySQL server is located  
--login-pathRead login path options from .mylogin.cnf  
--medium-checkDo a check that is faster than an --extended operation  
--no-defaultsRead no option files  
--optimizeOptimize the tables  
--passwordPassword to use when connecting to server  
--password1First multifactor authentication password to use when connecting to server8.0.27 
--password2Second multifactor authentication password to use when connecting to server8.0.27 
--password3Third multifactor authentication password to use when connecting to server8.0.27 
--pipeConnect to server using named pipe (Windows only)  
--plugin-dirDirectory where plugins are installed  
--portTCP/IP port number for connection  
--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  
--shared-memory-base-nameShared-memory name for shared-memory connections (Windows only)  
--silentSilent mode  
--skip-databaseOmit this database from performed operations  
--socketUnix socket file or Windows named pipe to use  
--ssl-caFile that contains list of trusted SSL Certificate Authorities  
--ssl-capathDirectory that contains trusted SSL Certificate Authority certificate files  
--ssl-certFile that contains X.509 certificate  
--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  
--ssl-keyFile that contains X.509 key  
--ssl-modeDesired security state of connection to server  
--ssl-session-dataFile that contains SSL session data8.0.29 
--ssl-session-data-continue-on-failed-reuseWhether to establish connections if session reuse fails8.0.29 
--tablesOverrides the --databases or -B option  
--tls-ciphersuitesPermissible TLSv1.3 ciphersuites for encrypted connections8.0.16 
--tls-versionPermissible TLS protocols for encrypted connections  
--use-frmFor repair operations on MyISAM tables  
--userMySQL user name to use when connecting to server  
--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  
--zstd-compression-levelCompression level for connections to server that use zstd compression8.0.18 
 

 


Aamir Rehman

5 Blog posts

Comments