Tuesday 17 December 2019

Optimizing and repairing MySQL databases with mysqlcheck

1. Check one table in the MySQL database

The following command will check the table posts in the database blog:
$ mysqlcheck -c blog posts
blog.posts                         OK
If your database is protected by a password add -u root -p at the end of the command:
$ mysqlcheck -c blog posts -u root -p
Enter password:
blog.posts                         OK

2. Analyze all tables in a MySQL database

The following command will check the table posts in the database blog:
$ mysqlcheck -a blog posts
blog.posts                         OK
If the MySQL/MariaDB server is running on a remote host, add -h at the end of the command:
$ mysqlcheck -a blog posts -h remotehost.com
blog.posts                         OK

3. Optimize all tables in all MySQL database

$ mysqlcheck -o --all-databases
blog.users
note     : Table does not support optimize, doing recreate + analyze instead
status   : OK
mysql.time_zone_transition_type                    Table is already up to date
Table does not support optimize, doing recreate + analyze instead means that we’re doing OPTIMIZE on an InnoDB table that doesn’t support this option. When doing OPTIMIZE on an InnoDB table it creates an empty table, copies all rows from the existing table into to the new one, deletes the old one and renames the new table, and then runs ANALYZE on the table.
Table is already up to date means that the table is already up to date and there is no need to check it.

4. Repair multiple MySQL databases

The following command will repair all tables in both blog and blog2 databases:
$ mysqlcheck -r --databases blog blog2
If you see note : The storage engine for the table doesn't support repair it means that you are doing REPAIR on an InnoDB.

5. Optimize and repair all tables in all MySQL databases

The following command will check all tables in all databases and if some table is corrupted it will automatically fix it that table:
$ mysqlcheck --auto-repair -o --all-databases

6. Most used mysqlcheck arguments

-c, --checkCheck table for errors.
-a, --analyzeAnalyze given tables.
-o --optimizeOptimize the tables.
-r, --repairPerform a repair that can fix almost anything except unique keys that are not unique.
--auto-repairIf a checked table is corrupted, automatically fix it. Repairing will be done after all tables have been checked.
-A, --all-databasesCheck all the databases. This is the same as –databases with all databases selected.
-B, --databasesProcess all tables in the named databases. With this option, all name arguments are regarded as database names, not as table names.
--tablesOverrides the –databases or -B option such that all name arguments following the option are regarded as table names.
-g, --check-upgradeCheck tables for version-dependent changes. May be used with –auto-repair to correct tables requiring version-dependent updates.

0 comments:

Post a Comment