MySQL The maximum column size is 767 bytes解决方案

MySQL在导入sql文件的时候,碰到报错输出:

Index column size too large. The maximum column size is 767 bytes

该问题出现的原因是使用utf8mb4字符集造成的。

转载解释如下:

由于 MySQL 的 Innodb 引擎表索引字段长度的限制为 767 字节,因此对于多字节字符集的大字段或者多字段组合,创建索引时会出现此错误。

如果是 utf8mb4 字符集,由于 utf8mb4 是 4 字节字符集,则默认支持的索引字段最大长度是 191 字符(767 字节 / 4 字节每字符≈191 字符),因此在 varchar (255) 或 char (255) 类型字段上创建索引会失败。

如果是 utf8 字符集,由于 utf8 是 3 字节字符集,则默认支持的索引字段最大长度是 255 字符(767 字节 / 3 字节每字符≈255 字符),则 varchar (255) 或 char (255) 不会失败。

解决方案:

修改my.cnf或my.ini,添加innodb_large_prefix = 1到mysqld。

然后重启MySQL服务。

创建表的时候指定表的 row format 格式为 Dynamic 或者 Compressed,示例如下:

    create table idx_length_test_xxxx
    (
      id int auto_increment primary key,
      stringname varchar(255)
    ) 
    ROW_FORMAT=DYNAMIC default charset utf8mb4;

对已经创建的表,修改表的 row_format 格式命令如下:

    alter table <表名> row_format=dynamic;
    alter table <表名> row_format=compressed;

 

未经允许不得转载:阿藏博客 » MySQL The maximum column size is 767 bytes解决方案

赞 (0) 打赏