如何檢查和修復(fù) MySQL 數(shù)據(jù)庫和表
當(dāng)您的網(wǎng)站因數(shù)據(jù)庫錯(cuò)誤而損壞或無法訪問時(shí),您可能需要檢查或修復(fù) MySQL 中的數(shù)據(jù)庫或表。在這種情況下,您可以使用 mysqlcheck 工具檢查并修復(fù)損壞的表或數(shù)據(jù)庫。mysqlcheck 是一個(gè)維護(hù)工具,允許您通過命令行界面檢查、修復(fù)、分析和優(yōu)化 MySQL 表。使用 mysqlcheck 的最佳功能之一是您可以在實(shí)時(shí)網(wǎng)站上執(zhí)行數(shù)據(jù)庫維護(hù),而無需停止 MySQL 服務(wù)。
在這篇文章中,我們將解釋如何檢查/修復(fù) MySQL 數(shù)據(jù)庫和表。
先決條件
- 運(yùn)行 Linux 的服務(wù)器
- 在您的服務(wù)器上配置的 root 密碼
mysqlcheck的基本語法
mysqlcheck 命令行工具的基本語法如下所示:
mysqlcheck [OPTION] DATABASENAME TABLENAME -u root -p
下面顯示了可以與 mysqlcheck 一起使用的每個(gè)選項(xiàng)的簡要說明:
- -c - 檢查表是否有錯(cuò)誤
- -C - 檢查上周之后更改的表。
- -a - 分析表。
- -A - 檢查所有數(shù)據(jù)庫。
- -g - 檢查表以獲取與版本相關(guān)的更改。
- -B , –databases – 指定多個(gè)數(shù)據(jù)庫。
- -F - 檢查未正確關(guān)閉的表。
- –fix-db-names – 修復(fù)數(shù)據(jù)庫名稱。
- –fix-table-names – 修復(fù)表名。
- -e - 執(zhí)行擴(kuò)展檢查。
- -r - 修復(fù)損壞的表。
在 MySQL 中檢查表
有時(shí),您可能需要檢查特定數(shù)據(jù)庫中的特定表。在這種情況下,您可以使用以下語法:
mysqlcheck -c db-name table-name -u root -p
例如,要檢查 class 數(shù)據(jù)庫中的 students 表,請(qǐng)運(yùn)行以下命令:
mysqlcheck -c class students -u root -p
您將獲得以下輸出:
class.students OK
檢查 MySQL 中的所有表
如果要檢查特定數(shù)據(jù)庫中的所有表,請(qǐng)使用以下語法:
mysqlcheck -c db-name -u root -p
例如,要檢查 class 數(shù)據(jù)庫中的所有表,請(qǐng)運(yùn)行以下命令:
mysqlcheck -c class -u root -p
您應(yīng)該得到以下輸出:
Enter password:
class.teacher OK
class.students OK
class.peon OK
檢查和優(yōu)化所有表和所有 MySQL 數(shù)據(jù)庫
您可以使用以下命令檢查所有表和所有數(shù)據(jù)庫:
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
您還可以使用以下命令優(yōu)化所有表和所有數(shù)據(jù)庫:
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
在上面的輸出中,您應(yīng)該看到“ Table does not support optimize ”,這意味著 InnoDB 表不支持此選項(xiàng)。
修復(fù) MySQL 數(shù)據(jù)庫
要修復(fù) class 數(shù)據(jù)庫中的 teacher 表,請(qǐng)運(yùn)行以下命令:
mysqlcheck -r class teacher -u root -p
輸出:
mysqlcheck -r class teacher -u root -p
Enter password:
class.teacher OK
要修復(fù) class 和 movies 數(shù)據(jù)庫中的所有表,請(qǐng)運(yùn)行以下命令:
mysqlcheck -r --databases class movies -u root -p
輸出:
Enter password:
class.teacher OK
class.students OK
class.peon OK
movies.hotstar OK
如果要檢查和修復(fù)所有數(shù)據(jù)庫中的所有表,請(qǐng)運(yùn)行以下命令:
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
注意:默認(rèn)情況下,InnoDB 存儲(chǔ)引擎不支持修復(fù)。在這種情況下,您需要將 MySQL 存儲(chǔ)引擎從 InnoDB 更改為 MyISAM。
結(jié)論
在這篇文章中,我們解釋了如何使用 mysqlcheck 命令行工具檢查和修復(fù) MySQL 中的表。