草庐IT

mysql - 错误 : Specified key was too long; max key length is 1000 bytes

coder 2023-06-10 原文

错误:

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

CREATE TABLE `phppos_modules_actions` (
  `action_id` VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL ,
  `module_id` VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL ,
  `action_name_key` VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL ,
  `sort` INT NOT NULL ,
  PRIMARY KEY ( `action_id` , `module_id` )
) ENGINE = INNODB CHARACTER SET utf8 COLLATE utf8_unicode_ci;

我知道错误发生是因为 255x2x3(每个字符 3 个字节)

这不会发生在所有安装上。我可以更改什么设置?

最佳答案

NO_ENGINE_SUBSTITUTION 被禁用且 INNODB 未激活,一个糟糕的组合

  • 在 MySql 5.5 之前,默认的 sqlmode 是一个空字符串,这意味着默认情况下没有设置 sqlmode NO_ENGINE_SUBSTITUTION

根据 MySql 文档(参见 https://dev.mysql.com/doc/refman/5.6/en/sql-mode.html#sqlmode_no_engine_substitution),这是 sqlmode NO_ENGINE_SUBSTITUTION 的含义:

Control automatic substitution of the default storage engine when a statement such as CREATE TABLE or ALTER TABLE specifies a storage engine that is disabled or not compiled in.

Because storage engines can be pluggable at runtime, unavailable engines are treated the same way:

With NO_ENGINE_SUBSTITUTION disabled, for CREATE TABLE the default engine is used and a warning occurs if the desired engine is unavailable. For ALTER TABLE, a warning occurs and the table is not altered.

With NO_ENGINE_SUBSTITUTION enabled, an error occurs and the table is not created or altered if the desired engine is unavailable.

  • 因此:如果 NO_ENGINE_SUBSTITUTION 被禁用并且 INNODB 被关闭,如果您在 CREATE TABLE 语句中指定 INNODB,MySql 也会切换到 MYISAM。
  • 如果您正在创建的表对于 MYISAM 是正确的,您只会收到一条警告并创建该表。那不是你的情况,你的创建语句包含一个索引,超出了 MYISAM 的 1000 字节限制,然后创建失败,错误 1071 报告 MYISAM 的错误。那是因为工作引擎是 MYISAM,而不是 INNODB。

证明

MySql 版本 5.1.56 社区

案例一:

Options in my.cnf  
sql-mode=""  
default-storage-engine=MYISAM  
skip-innodb uncommented (without#) 

Return on execution of your create statement:
Error Code: 1071. Specified key was too long; max key length is 1000 bytes

Explanation: INNODB is not active, the engine is automatically switched to MYISAM  
that returns this error as they key is longer than MYISAM 1000 bytes limit.  
The key length is: 
2 fields x 255 char x 3 bytes utf8 encoding + 2 x 1 length byte = 1532 bytes   

案例二:

Options in my.cnf
sql-mode="NO_ENGINE_SUBSTITUTION"
default-storage-engine=MYISAM
skip-innodb uncommented (without#)

Return on execution of your create statement:
Error Code: 1286. Unknown table engine 'INNODB'

Explanation: INNODB is not active but the engine substitution is not permitted
by sql mode therefore the DB returns an error about the attempt of using a disabled engine. 

案例三:

Options in my.cnf
sql-mode="NO_ENGINE_SUBSTITUTION"
default-storage-engine=MYISAM
skip-innodb commented (with#)

Return on execution of your create statement:
Table creation OK!

Explanation: INNODB is active (skip-innodb commented) and it is used also if      
the default engine is MYISAM.

要重现测试,请在每次更改 my.cnf 后重新启动 MySql。

由于MySql 5.6版本sqlmode默认不再为空,包含NO_ENGINE_SUBSTITUTION,而且INNODB是默认引擎,所以很难遇到错误。

其他测试

没有其他方法可以重现错误:

Error Code: 1071. Specified key was too long; max key length is 1000 bytes 

在尝试创建 INNODB 表时已找到。

在 INNODB 中你有两种 ERROR 1071:

Error Code: 1071. Specified key was too long; max key length is 767 bytes

这与 innodb_large_prefix ON 或 OFF 无关,仅与用作索引的单个 VARCHAR 列的大小有关。

Mysql 将 varchar utf8 存储为 3 个字节加上 1 个字节,长度最多为 255 个字符,之后为 2 个(请参阅:http://dev.mysql.com/doc/refman/5.7/en/storage-requirements.html),因此如果您尝试使用 VARCHAR(256) utf8 设置键,您将得到:

256 x 3 + 2 = 770 bytes

你会得到之前的错误,因为对于 InnoDB 表,单个列的最大键长度是 767 字节。 VARCHAR(255) 没问题,因为:

255 x 3 + 1 = 766 bytes

我在 Mysql 的四个安装版本 5.1.56、5.5.33、5.6 和 5.7 上对其进行了测试,并得到证实。使用 VARCHAR(255) 的查询没有问题,使用 VARCHAR(256) 的问题:

Error Code: 1071. Specified key was too long; max key length is 767 bytes

如您所见,消息不同,因为它是 INNODB 消息而不是 MYISAM 消息!

INNODB 表的另一种错误 1071 是:

Error Code: 1071. Specified key was too long; max key length is 3072 bytes

这与具有多列的键有关。要启用这些键,您需要将 innodb_large_prefix 设置为 on。 不管怎样,如果你尝试运行这样的东西:

CREATE TABLE `phppos_modules_actions` (
  `action_id` VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL ,
  `module_id` VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL ,
  `action_name_key` VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL ,
  `action_name_key1` VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL ,
  `action_name_key2` VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL ,
  `sort` INT NOT NULL ,
  PRIMARY KEY ( `action_id` , `module_id`, `action_name_key`, `action_name_key1`, `action_name_key2` )
) ENGINE = INNODB CHARACTER SET utf8 COLLATE utf8_unicode_ci;

使用 5 VARCHAR(255) utf8 列的键,即 3830 字节,您将遇到:

Error Code: 1071. Specified key was too long; max key length is 3072 bytes

奇异假设

在寻找原因的过程中,我制定并测试了不同且非常奇怪的假设:

行格式

已测试 REDUNDANT、COMPACT、COMPRESS、DYNAMIC:对使用您的语句创建表没有影响。

文件格式

经过测试的 Antelope 和 Barracuda:您的语句对建表没有影响。

MySql 构建

测试了 32 位和 64 位 MySql:对您的语句创建表没有影响。

其他类似故障

在这里你可以找到相同情况下的相同错误:

https://www.drupal.org/node/2466287

我在 PROOF 中列出的 3 种测试情况下测试了该语句,它再现了与您完全相同的行为,因此我可以说问题是相同的。在那种情况下,他们切换到其他数据库,但问题是设置的混合,而不是数据库版本。

引用资料

这里给出了一篇非常好的使用 INNODB 建立索引的文章:

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

警告:在创建索引超过 1000 的 INNODB 表后通过取消注释 my.cnf 中的 skip-innodb 来禁用 INNODB 将不允许启动 MySql 服务

问候

关于mysql - 错误 : Specified key was too long; max key length is 1000 bytes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11847815/

有关mysql - 错误 : Specified key was too long; max key length is 1000 bytes的更多相关文章

  1. ruby-on-rails - Rails 常用字符串(用于通知和错误信息等) - 2

    大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje

  2. ruby-on-rails - 迷你测试错误 : "NameError: uninitialized constant" - 2

    我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test

  3. ruby-on-rails - 如何在 Rails View 上显示错误消息? - 2

    我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c

  4. 使用 ACL 调用 upload_file 时出现 Ruby S3 "Access Denied"错误 - 2

    我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file

  5. ruby-on-rails - 错误 : Error installing pg: ERROR: Failed to build gem native extension - 2

    我克隆了一个rails仓库,我现在正尝试捆绑安装背景:OSXElCapitanruby2.2.3p173(2015-08-18修订版51636)[x86_64-darwin15]rails-v在您的Gemfile中列出的或native可用的任何gem源中找不到gem'pg(>=0)ruby​​'。运行bundleinstall以安装缺少的gem。bundleinstallFetchinggemmetadatafromhttps://rubygems.org/............Fetchingversionmetadatafromhttps://rubygems.org/...Fe

  6. ruby - #之间? Cooper 的 *Beginning Ruby* 中的错误或异常 - 2

    在Cooper的书BeginningRuby中,第166页有一个我无法重现的示例。classSongincludeComparableattr_accessor:lengthdef(other)@lengthother.lengthenddefinitialize(song_name,length)@song_name=song_name@length=lengthendenda=Song.new('Rockaroundtheclock',143)b=Song.new('BohemianRhapsody',544)c=Song.new('MinuteWaltz',60)a.betwee

  7. ruby-on-rails - 每次我尝试部署时,我都会得到 - (gcloud.preview.app.deploy) 错误响应 : [4] DEADLINE_EXCEEDED - 2

    我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie

  8. ruby-on-rails - Rails 5 Active Record 记录无效错误 - 2

    我有两个Rails模型,即Invoice和Invoice_details。一个Invoice_details属于Invoice,一个Invoice有多个Invoice_details。我无法使用accepts_nested_attributes_forinInvoice通过Invoice模型保存Invoice_details。我收到以下错误:(0.2ms)BEGIN(0.2ms)ROLLBACKCompleted422UnprocessableEntityin25ms(ActiveRecord:4.0ms)ActiveRecord::RecordInvalid(Validationfa

  9. arrays - 这是 Ruby 中 Array.fill 方法的错误吗? - 2

    这个问题在这里已经有了答案:Arraysmisbehaving(1个回答)关闭6年前。是否应该这样,即我误解了,还是错误?a=Array.new(3,Array.new(3))a[1].fill('g')=>[["g","g","g"],["g","g","g"],["g","g","g"]]它不应该导致:=>[[nil,nil,nil],["g","g","g"],[nil,nil,nil]]

  10. ruby-on-rails - Ruby on Rails 计数器缓存错误 - 2

    尝试在我的RoR应用程序中实现计数器缓存列时出现错误Unknownkey(s):counter_cache。我在这个问题中实现了模型关联:Modelassociationquestion这是我的迁移:classAddVideoVotesCountToVideos0Video.reset_column_informationVideo.find(:all).eachdo|p|p.update_attributes:videos_votes_count,p.video_votes.lengthendenddefself.downremove_column:videos,:video_vot

随机推荐