MySQL: #126 - Incorrect key file for table

前端 未结 17 1762
臣服心动
臣服心动 2020-11-29 02:18

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

相关标签:
17条回答
  • 2020-11-29 02:31

    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
    
    0 讨论(0)
  • 2020-11-29 02:32

    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.

    0 讨论(0)
  • 2020-11-29 02:33

    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 for INDEX. The key attribute PRIMARY KEY can also be specified as just KEY 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:

    • Disk issues on the MySQL server
    • Corrupted keys/tables

    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.

    0 讨论(0)
  • 2020-11-29 02:34

    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` );
    
    0 讨论(0)
  • 2020-11-29 02:36

    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.

    0 讨论(0)
  • 2020-11-29 02:37

    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

    0 讨论(0)
提交回复
热议问题