在处理 MySQL 数据库损坏时,如果 MYI 索引文件丢失或者其 header 损坏,您将无法使用 myisamchk 命令:
myisamchk --safe-recover --force --sort_buffer_size=2G --key_buffer_size=2G /var/lib/mysql/*/*.MYI
您必须使用 use_frm 选项从 MySQL 命令提示符进行修复:
repair tbl_name use_frm;
根据 MySQL documentation's on repairing tables
The USE_FRM option is available for use if the .MYI index file is missing or if its header is corrupted. This option tells MySQL not to trust the information in the .MYI file header and to re-create it using information from the .frm file. This kind of repair cannot be done with myisamchk.
使用 myisamchk,您可以轻松地进入每个数据库文件夹并通过在命令末尾使用星号来修复每个表:
/var/lib/mysql/*/*.MYI
您不能在 MySQL 命令提示符下执行任何类似的操作。
有一个 StackOverflow 问题的答案解释了 how to repair all tables within one specific database from the MySQL command prompt with a procedure :
CREATE DEFINER = 'root'@'localhost'
PROCEDURE MYDATABASE.repair_all()
BEGIN
DECLARE endloop INT DEFAULT 0;
DECLARE tableName char(100);
DECLARE rCursor CURSOR FOR SELECT `TABLE_NAME` FROM `information_schema`.`TABLES` WHERE `TABLE_SCHEMA`=DATABASE();
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET endloop=1;
OPEN rCursor;
FETCH rCursor INTO tableName;
WHILE endloop = 0 DO
SET @sql = CONCAT("REPAIR TABLE `", tableName, "`");
PREPARE statement FROM @sql;
EXECUTE statement;
FETCH rCursor INTO tableName;
END WHILE;
CLOSE rCursor;
END
是否可以修改这样的过程以遍历所有 MySQL 数据库并修复这些数据库中的每个表?
我认为这对于拥有大量数据库并遇到严重损坏的任何人来说都是有用的。
最佳答案
mysqlcheck
是 MySQL CHECK
、REPAIR
、ANALYZE
和 OPTIMIZE
语句的更方便的命令行界面。
mysqlcheck --repair --use-frm --all-databases
关于mysql - 当 MYI 文件损坏或丢失时,如何从 MySQL 命令提示符修复所有数据库中的所有表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7082043/