数据库mysql Specified key was too long;max key length is 767 bytes
The error "Specified key was too long; max key length is 767 bytes" typically occurs in MySQL when you try to create an index on a column or a combination of columns that exceeds the maximum key length limit.
In MySQL, the maximum key length for InnoDB tables is 767 bytes. This limit is due to the default character set used by InnoDB, which is utf8mb3. In this character set, each character can take up to 3 bytes, so the maximum key length is 767/3 = 255 characters.
To fix this error, you can try one of the following solutions:
-
Reduce the length of the column(s) you are trying to create an index on. For example, if you have a VARCHAR column with a length of 255, you can reduce it to 191 to ensure it fits within the maximum key length limit.
-
Change the character set of your table to utf8mb4, which allows each character to take up to 4 bytes. To do this, you can modify your table definition and specify the character set as follows:
ALTER TABLE your_table_name
CONVERT TO CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;
Note that changing the character set may have implications on the storage requirements and performance of your database, so make sure to test and evaluate the impact before making the change.
- If you are using a MySQL version older than 5.7.7 or 5.6.3, you can enable the
innodb_large_prefixoption. This option allows index key prefixes longer than 767 bytes, up to a maximum of 3072 bytes. However, enabling this option requires rebuilding all your InnoDB tables, so it is not recommended unless absolutely necessary.
To enable the innodb_large_prefix option, you can add the following line to your MySQL configuration file (my.cnf or my.ini):
innodb_large_prefix=1
After making the necessary changes, you should be able to create the index without encountering the "Specified key was too long; max key length is 767 bytes" error
原文地址: https://www.cveoy.top/t/topic/iDTA 著作权归作者所有。请勿转载和采集!