MySQL VARCHAR(255) UTF8 is too long for key, but max length is 1000 bytes

If you’re using utf8mb4, and you have unique indexes on varchar columns that are greater than 191 characters in length, you’ll need to turn on innodb_large_prefix to allow for larger columns in indexes, because utf8mb4 requires more storage space than utf8 or latin1. Add the following to your my.cnf file.

[mysqld]
innodb_file_format=barracuda
innodb_file_per_table=1
innodb_large_prefix=1
init_connect="SET collation_connection = utf8mb4_unicode_ci; SET NAMES utf8mb4"
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci

More info about the why and future from MySQL 5.7 documentation:

If innodb_large_prefix is enabled (the default in MySQL 5.7.7), the
index key prefix limit is 3072 bytes for InnoDB tables that use
DYNAMIC or COMPRESSED row format. If innodb_large_prefix is disabled,
the index key prefix limit is 767 bytes for tables of any row format.

innodb_large_prefix is deprecated in MySQL 5.7.7 and will be removed
in a future release. innodb_large_prefix was introduced in MySQL 5.5
to disable large index key prefixes for compatibility with earlier
versions of InnoDB that do not support large index key prefixes.

To sum up, the limit is only there for compatibility and will be increased in future versions.

Leave a Comment

tech