I got the following error from a MySQL query.
#126 - Incorrect key file for table
I have not even declared a key for this table, but I do have i
Go to /etc/mysql/my.cnf
and comment out tmpfs
#tmpdir=/var/tmpfs
This fixes the problem.
I ran the command suggested in another answer and while the directory is small, it was empty, so space was not the issue.
/var/tmp$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vzfs 60G 51G 9.5G 85% /
none 1.5G 4.0K 1.5G 1% /dev
tmpfs 200M 0 200M 0% /var/tmpfs
/var/tmpfs$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vzfs 60G 51G 9.5G 85% /
none 1.5G 4.0K 1.5G 1% /dev
tmpfs 200M 0 200M 0% /var/tmpfs
I got this message when writing to a table after reducing the ft_min_word_len (full text min word length). To solve it, re-create the index by repairing the table.
First of all, you should know that keys and indices are synonyms in MySQL. If you look at the documentation about the CREATE TABLE Syntax, you can read:
KEY
is normally a synonym forINDEX
. The key attributePRIMARY KEY
can also be specified as justKEY
when given in a column definition. This was implemented for compatibility with other database systems.
Now, the kind of error you are getting can be due to two things:
In the first case, you will see that adding a limit to your query might solve the problem temporarily. If that does it for you, you probably have a tmp
folder that is too small for the size of the queries you are trying to do. You can then decide or to make tmp
bigger, or to make your queries smaller! ;)
Sometimes, tmp
is big enough but still gets full, you'll need to do some manual cleanup in these situations.
In the second case, there are actual issues with MySQL's data. If you can re-insert the data easily, I would advice to just drop/re-create the table, and re-insert the data. If you can't you can try repairing the table in place with REPAIR table. It is a generally lengthy process which might very well fail.
Look at the complete error message you get:
Incorrect key file for table 'FILEPATH.MYI'; try to repair it
It mentions in the message that you can try to repair it. Also, if you look at the actual FILEPATH you get, you can find out more:
if it is something like /tmp/#sql_ab34_23f
it means that MySQL needs to create a temporary table because of the query size. It stores it in /tmp, and that there is not enough space in your /tmp for that temporary table.
if it contains the name of an actual table instead, it means that this table is very likely corrupted and you should repair it.
If you identify that your issue is with the size of /tmp, just read this answer to a similar question for the fix: MySQL, Error 126: Incorrect key file for table.
Came here searching for - "#1034 - Incorrect key file for table 'test'; try to repair it"
Seeing this caused by added a charset to an indexed Enum (might be the same with other fields) with Mysql 8.0.21.
CREATE TABLE `test` (
`enumVal` ENUM( 'val1' ) NOT NULL
) ENGINE = MYISAM;
ALTER TABLE `test` ADD INDEX ( `enumVal` );
ALTER TABLE `test` CHANGE `enumVal` `enumVal` ENUM( 'val1') CHARACTER SET utf8 COLLATE utf8_bin NOT NULL;
Solution using is to drop the index before the alter.
ALTER TABLE `test` ADD INDEX ( `enumVal` );
Now of the other answers solved it for me. Turns out that renaming a column and an index in the same query caused the error.
Not working:
-- rename column and rename index
ALTER TABLE `client_types`
CHANGE `template_path` `path` VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
DROP INDEX client_types_template_path_unique,
ADD UNIQUE INDEX `client_types_path_unique` (`path` ASC);
Works (2 statements):
-- rename column
ALTER TABLE `client_types`
CHANGE `template_path` `path` VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL;
-- rename index
ALTER TABLE `client_types`
DROP INDEX client_types_template_path_unique,
ADD UNIQUE INDEX `client_types_path_unique` (`path` ASC);
This was on MariaDB 10.0.20. There were no errors with the same query on MySQL 5.5.48.
Error #126 usually occurs when you got a corrupt table. The best way to solve this is to perform repair. This article might help:
http://dev.mysql.com/doc/refman/5.0/en/repair-table.html