Friday, March 14, 2014

MySQL MyISAM table repair.

MySQL MyISAM tables might break down from time to time.
You might get following errors in database error log:
--
[ERROR] mysqld: Incorrect key file
 for table '<TABLE_INDEX_FILE_NAME_AND_PATH>.MYI'; try to repair it


[ERROR] mysqld: Table '<TABLE_NAME_AND_PATH>' is marked as crashed and should be repaired--

If you get only error number from application then you can use perror utility from command prompt to get more info about error number (error numbers related to MyISAM tables usually are:
126 127 132 134 135 136 141 144 145) :

mysql/bin/perror 144

MySQL error code 144: Table is crashed and last repair failed



Check and fix MyISAM tables:

With myisamchk utility you can try to check and fix these tables/index files. Before run myisamchk  commands you need to stop MySQL server and go into database directory (or specify directory in the command). I prefer myisamchk because it garentees that no one is not trying to use tables when doing the repair (MySQL server need to be stopped when running it).


To Check table errors:
 
To check all tables (to check only one table specify that table name):
myisamchk *.MYI

To do slower and more extend-check check run:
myisamchk -e *.MYI

You can also check tables from SQL (when server is running):
CHECK TABLE <TABLE_NAME> <OPTIONS>;
CHECK TABLE <TABLE_NAME> QUICK;


To Fixing Tables or Index files:

Repairing tables might sometimes lead to lost of data so it is good practice to take backup of files or database before running these if it is possible.
 
Fix only those tables that are broken ( -q makes quick repair that repairs only index files):
mysql/bin/myisamchk -r -q <TABLE_NAME>


With big tables (and index files) and to get more performance for fixing use following options (you need to tune these values suitable for your environment) with myisamchk (to see all myisamchk options you can use myisamchk --help ):
mysql/bin/myisamchk --sort_buffer_size=2G \
           --key_buffer_size=2G \
           --read_buffer_size=512M \
           --write_buffer_size=512M \
      --tmpdir=/data/myisam_repair \
      --recover --quick \
     <TABLE_NAME>


You can also try to fix tables from SQL (when server is running) :
REPAIR TABLE <TABLE_NAME> <OPTIONS> ;
REPAIR TABLE <TABLE_NAME> QUICK;


NOTE: If some reason myisamchk did not fix your table/index files or you want to get more detailed guide and info about MyISAM table repair check these links:
http://dev.mysql.com/doc/refman/5.7/en/myisam-repair.html
http://dev.mysql.com/doc/refman/5.7/en/check-table.html
http://dev.mysql.com/doc/refman/5.1/en/repair-table.html


1 comment:

  1. Tables of the SQL database got crash due to no of reasons so in such situation you must go with a tool. Read more http://www.sqlrepairtool.org

    ReplyDelete