开发者

How can you repair all tables in all databases from the MySQL command prompt when MYI file is corrupted or missing?

开发者 https://www.devze.com 2023-03-28 06:48 出处:网络
When dealing with MySQL database corruption, if the MYI index file is missing or if its header is corrupted you can\'t use a myisamchk command:

When dealing with MySQL database corruption, if the MYI index file is missing or if its header is corrupted you can't use a myisamchk command:

myisamchk --safe-recover --force --sort_buffer_size=2G --key_buffer_size=2G /var/lib/mysql/*/*.MYI

You have to do the repair from the MySQL command prompt with the use_frm option:

repair tbl_name use_frm;

Per 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.

With myisamchk, you can easily drop into each database folder and repair every table by using asterisks at the end of command:

/var/lib/mysql/*/*.MYI

You can't do anything similar from the MySQL command prompt.

There's a StackOverflow question with an answer that explains 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 SE开发者_如何学GoLECT `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

Is it possible to modify a procedure like this to loop through all your MySQL databases and repair every table within those databases?

I think this could be useful for anyone who has a large number of databases and runs into serious corruption.


mysqlcheck is a more convenient command-line interface to the MySQL CHECK, REPAIR, ANALYZE and OPTIMIZE statements.

mysqlcheck --repair --use-frm --all-databases


Here's my solution for when I had to fix all of the MyISAM files in my DB:

find ./ -name "*.MYI" -exec myisamchk -r {} \;

It traverses all of the databases.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号