执行以下命令时:

ALTER TABLE `mytable` ADD UNIQUE (
`column1` ,
`column2`
);

我得到了这个错误信息:

#1071 - Specified key was too long; max key length is 767 bytes

columnn1和column2的信息:

column1 varchar(20) utf8_general_ci
column2  varchar(500) utf8_general_ci

我认为varchar(20)只需要21个字节,而varchar(500)只需要501个字节。所以总字节数是522,小于767。为什么我得到了错误消息?

#1071 - Specified key was too long; max key length is 767 bytes

当前回答

我在这个话题上做了一些搜索,最后得到了一些自定义更改

MySQL工作台6.3.7版本有图形界面

启动Workbench并选择连接。 转到管理或实例并选择选项文件。 如果Workbench要求您允许读取配置文件,然后按OK两次。 在中心位置的管理员选项文件窗口出现。 进入InnoDB选项卡,如果在General部分没有检查innodb_large_prefix。 设置innodb_default_row_format选项值为DYNAMIC。

对于6.3.7以下的版本,直接选项不可用,因此需要使用命令提示符

Start CMD as administrator. Go To director where mysql server is install Most of cases its at "C:\Program Files\MySQL\MySQL Server 5.7\bin" so command is "cd \" "cd Program Files\MySQL\MySQL Server 5.7\bin". Now Run command mysql -u userName -p databasescheema Now it asked for password of respective user. Provide password and enter into mysql prompt. We have to set some global settings enter the below commands one by one set global innodb_large_prefix=on; set global innodb_file_format=barracuda; set global innodb_file_per_table=true; Now at the last we have to alter the ROW_FORMAT of required table by default its COMPACT we have to set it to DYNAMIC. use following command alter table table_name ROW_FORMAT=DYNAMIC; Done

其他回答

Specified key was too long; max key length is 767 bytes

You got that message because 1 byte equals 1 character only if you use the latin-1 character set. If you use utf8, each character will be considered 3 bytes when defining your key column. If you use utf8mb4, each character will be considered to be 4 bytes when defining your key column. Thus, you need to multiply your key field's character limit by, 1, 3, or 4 (in my example) to determine the number of bytes the key field is trying to allow. If you are using uft8mb4, you can only define 191 characters for a native, InnoDB, primary key field. Just don't breach 767 bytes.

我在这个话题上做了一些搜索,最后得到了一些自定义更改

MySQL工作台6.3.7版本有图形界面

启动Workbench并选择连接。 转到管理或实例并选择选项文件。 如果Workbench要求您允许读取配置文件,然后按OK两次。 在中心位置的管理员选项文件窗口出现。 进入InnoDB选项卡,如果在General部分没有检查innodb_large_prefix。 设置innodb_default_row_format选项值为DYNAMIC。

对于6.3.7以下的版本,直接选项不可用,因此需要使用命令提示符

Start CMD as administrator. Go To director where mysql server is install Most of cases its at "C:\Program Files\MySQL\MySQL Server 5.7\bin" so command is "cd \" "cd Program Files\MySQL\MySQL Server 5.7\bin". Now Run command mysql -u userName -p databasescheema Now it asked for password of respective user. Provide password and enter into mysql prompt. We have to set some global settings enter the below commands one by one set global innodb_large_prefix=on; set global innodb_file_format=barracuda; set global innodb_file_per_table=true; Now at the last we have to alter the ROW_FORMAT of required table by default its COMPACT we have to set it to DYNAMIC. use following command alter table table_name ROW_FORMAT=DYNAMIC; Done

好吧,在我的情况下,我必须将数据库文件从mySQL 5.7恢复到mySQL 5.6,我遇到了这个问题。

根本原因是版本不兼容,并且索引的某些列长于191(默认值为255)。

所以解决方案很简单:让所有“索引列的长度”都小于191(例如180)

我对这个相同问题的修复是添加一个选项作为第三个参数:charset

queryInterface.createTable(
  tableName,
  { /*... columns*/ },
  { charset: 'utf8' } 
)

否则sequelize将创建utf8mb4表。

索引长度& MySQL / MariaDB


Laravel默认使用utf8mb4字符集,其中包括在数据库中存储“表情符号”的支持。如果您运行的MySQL版本比5.7.7版本更老,或者MariaDB版本比10.2.2版本更老,您可能需要手动配置由迁移生成的默认字符串长度,以便MySQL为它们创建索引。你可以在AppServiceProvider中调用Schema::defaultStringLength方法来配置:

use Illuminate\Support\Facades\Schema; /** * Bootstrap any application services. * * @return void */ public function boot() { Schema::defaultStringLength(191); } Alternatively, you may enable the innodb_large_prefix option for your database. Refer to your database's documentation for instructions on how to properly enable this option. Reference from blog : https://www.scratchcode.io/specified-key-too-long-error-in-laravel/ Reference from Official laravel documentation : https://laravel.com/docs/5.7/migrations