mysql error: Specified key was too long; max key length is 767 bytes

烂漫一生 提交于 2019-12-13 02:09:54

问题


I had to run this command to alter a column in one of my table.

ALTER table XYZ modify value VARCHAR(1024);

When I run it, I get this error.

ERROR 1071 (42000) at line 1: Specified key was too long; max key length is 767 bytes

The original column size was 250. Here is the strange part. I have a 2nd MySQL server with identical configurations and it is doing 2 way replication with the 1st server. When I run this "alter table" command on my 2nd MySQL server, I didn't get this error. In fact, after the alter command finished running, this command was replicated to my primary mysql server without any error.

My two mysql servers are setup as active-active, but only one of them is being actively use. So to clarify, I got this "max key length" error on the actively used server. But it ran fine on the 2nd server that was not being use.

Does anyone have any idea why the command ran find on my 2nd server?

Thanks../Ken


回答1:


You might need to adjust the index on this table.

DROP INDEX index_name ON tbl_name;
CREATE INDEX index_name ON tbl_name (col1,col2(100));

Where col2 is the value column from your example and 100 is the max characters to include in the index.

If you are unsure of the indexes on the table, try the following (replacing tbl_name with your table name).

SHOW INDEXES IN tbl_name;



回答2:


I have also faced same issue when the encoding of the database is set to UTF8.

If you use latin1 then the largest column you can index is varchar(767), but if you use utf8 then the limit is varchar(255).

Try changing the value length to 255.

It might work.

Following link may guide you to the same.

http://mechanics.flite.com/blog/2014/07/29/using-innodb-large-prefix-to-avoid-error-1071/



来源:https://stackoverflow.com/questions/24436301/mysql-error-specified-key-was-too-long-max-key-length-is-767-bytes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!