出現這個錯誤是在創建索引的時候,原因所選字段的長度超出了可以創建索引的最大長度,InnoDB是767字節,但是有時候明明所選字段的長度很小比如 255 明顯比這個小 為什么會報錯呢,原來 所選字段當時UTF8編碼的時候需要乘以3 UTF8mb4的時候需要乘以4,MySQL reserves 3 bytes per UTF8 character 官網地址mysql
Paste_Image.png
讓然也可以修改限制從767 bytes (up to 3072 bytes). 在my.cnf文件中
[mysqld]
innodb_file_format=barracuda
innodb_file_per_table=1
innodb_large_prefix=1
init_connect='SET collation_connection = utf8mb4_unicode_ci'
init_connect='SET NAMES utf8mb4'
character-set-server=utf8mb4collation-server=utf8mb4_unicode_ci
主要是這兩項
innodb_file_format=barracuda
and innodb_file_per_table=true
.
767 up to 3072