当您的网站因数据库错误而损坏或无法访问时,您可能需要检查或修复 MySQL 中的数据库或表。在这种情况下,您可以使用 mysqlcheck 工具检查并修复损坏的表或数据库。mysqlcheck 是一个维护工具,允许您通过命令行界面检查、修复、分析和优化 MySQL 表。使用 mysqlcheck 的最佳功能之一是您可以在实时网站上执行数据库维护,而无需停止 MySQL 服务。
在这篇文章中,我们将解释如何检查/修复 MySQL 数据库和表。
运行 Linux 的服务器在您的服务器上配置的 root 密码
mysqlcheck 命令行工具的基本语法如下所示:
mysqlcheck [OPTION] DATABASENAME TABLENAME -u root -p
下面显示了可以与 mysqlcheck 一起使用的每个选项的简要说明:
-c - 检查表是否有错误-C - 检查上周之后更改的表。-a - 分析表。-A - 检查所有数据库。-g - 检查表以获取与版本相关的更改。-B , –databases – 指定多个数据库。-F - 检查未正确关闭的表。–fix-db-names – 修复数据库名称。–fix-table-names – 修复表名。-e - 执行扩展检查。-r - 修复损坏的表。
有时,您可能需要检查特定数据库中的特定表。在这种情况下,您可以使用以下语法:
mysqlcheck -c db-name table-name -u root -p
例如,要检查 class 数据库中的 students 表,请运行以下命令:
mysqlcheck -c class students -u root -p
您将获得以下输出:
class.students OK
如果要检查特定数据库中的所有表,请使用以下语法:
mysqlcheck -c db-name -u root -p
例如,要检查 class 数据库中的所有表,请运行以下命令:
mysqlcheck -c class -u root -p
您应该得到以下输出:
Enter password: class.teacher OK class.students OK class.peon OK
您可以使用以下命令检查所有表和所有数据库:
mysqlcheck -c -u root -p --all-databases
输出:
Enter password: class.teacher OK class.students OK class.peon OK guest.MyGuests OK movies.hotstar OK mysql.columns_priv OK mysql.component OK mysql.db OK mysql.default_roles OK mysql.engine_cost OK mysql.func OK mysql.general_log OK mysql.global_grants OK mysql.gtid_executed OK mysql.help_category OK mysql.help_keyword OK mysql.help_relation OK mysql.help_topic OK mysql.innodb_index_stats OK mysql.innodb_table_stats OK mysql.password_history OK mysql.plugin OK mysql.procs_priv OK mysql.proxies_priv OK mysql.role_edges OK mysql.server_cost OK mysql.servers OK mysql.slave_master_info OK mysql.slave_relay_log_info OK mysql.slave_worker_info OK
您还可以使用以下命令优化所有表和所有数据库:
mysqlcheck -o root -p --all-databases
输出:
Enter password: class.teacher note : Table does not support optimize, doing recreate analyze instead status : OK class.students note : Table does not support optimize, doing recreate analyze instead status : OK class.peon note : Table does not support optimize, doing recreate analyze instead status : OK guest.MyGuests note : Table does not support optimize, doing recreate analyze instead status : OK movies.hotstar note : Table does not support optimize, doing recreate analyze instead status : OK mysql.columns_priv
在上面的输出中,您应该看到“ Table does not support optimize ”,这意味着 InnoDB 表不支持此选项。
修复 MySQL 数据库
要修复 class 数据库中的 teacher 表,请运行以下命令:
mysqlcheck -r class teacher -u root -p
输出:
mysqlcheck class teacher root Enter password: class.teacher OK
要修复 class 和 movies 数据库中的所有表,请运行以下命令:
mysqlcheck -r --databases class movies -u root -p
输出:
Enter password: class.teacher OK class.students OK class.peon OK movies.hotstar OK
如果要检查和修复所有数据库中的所有表,请运行以下命令:
mysqlcheck --auto-repair --all-databases -u root -p
输出:
Enter password: class.teacher OK class.students OK class.peon OK guest.MyGuests OK movies.hotstar OK mysql.columns_priv OK mysql.component OK mysql.db OK mysql.default_roles OK mysql.engine_cost OK mysql.func OK mysql.general_log OK mysql.global_grants OK mysql.gtid_executed OK mysql.help_category OK mysql.help_keyword OK mysql.help_relation OK mysql.help_topic OK mysql.innodb_index_stats OK mysql.innodb_table_stats OK mysql.password_history OK mysql.plugin OK mysql.procs_priv OK mysql.proxies_priv OK mysql.role_edges OK mysql.server_cost OK mysql.servers OK mysql.slave_master_info OK mysql.slave_relay_log_info OK mysql.slave_worker_info OK mysql.slow_log OK mysql.tables_priv OK mysql.time_zone OK mysql.time_zone_leap_second OK mysql.time_zone_name OK mysql.time_zone_transition OK mysql.time_zone_transition_type OK
注意:默认情况下,InnoDB 存储引擎不支持修复。在这种情况下,您需要将 MySQL 存储引擎从 InnoDB 更改为 MyISAM。
在这篇文章中,我们解释了如何使用 mysqlcheck 命令行工具检查和修复 MySQL 中的表。