MySQL "Incorrect key file for table" error
Posted June 24th, 2009 in MySql (Updated June 1st, 2012)
When saving a record to a MySQL table the other day I got the error message "Incorrect key file for table 'mytable'; try to repair it". I am uncertain why the error occured and how to ensure it doesn't happen again in the future but a quick fix for the time being is simple.
How to repair a MySQL table
All you need to do is to repair the table by running the following SQL command, where "mytable" is the name of the table that gave the error:
REPAIR TABLE `mytable`;
You can run this from e.g. the MySQL CLI or phpMyAdmin. From phpMyAdmin select the table, then "Operations" from the navigation tabs in the right frame above the table info; then "Repair Table" from the "Table maintenance" options at the bottom of the page.
When the table is /tmp/#sql_xxx_x.MYI
If the error looks like "Incorrect key file for table '/tmp/#sql_xxx_x.MYI'; try to repair it" where it refers to a temporary location on the filesystem, it's likely you've run out of diskspace. Read my follow up post for more information.
- MySQL "Incorrect key file for table /tmp/#sql_xxx_x.MYI" error (Friday, June 1st 2012)
- Cannot load from mysql.proc. The table is probably corrupted (Wednesday, February 22nd 2012)
- MySQL table is marked as crashed and should be repaired (Tuesday, August 2nd 2011)
- Optimize a table in MySQL from phpMyAdmin (Saturday, September 6th 2008)
- Running queries from the MySQL Command Line (Wednesday, April 16th 2008)