【Database】排错:Mysql5.6报错Specified key was too long; max key length is 767 bytes

本文涉及的产品
云数据库 RDS MySQL,集群版 2核4GB 100GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: 在某个实验系统部署的过程中,出现mysql报错,是特定版本的处理错误,在查阅官网文档时得到解决方案

报错如下:

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

 

image.png

 

错误是指超出索引字节的限制,并不是指字段长度限制。

MySQL 5.6官方文档内容如下

   By default, the index key prefix length limit is 767 bytes. See Section 13.1.13, “CREATE INDEX Syntax”. For example, you might hit this limit with a column prefix index of more than 255 characters on a TEXT or VARCHAR column, assuming a utf8mb3 character set and the maximum of 3 bytes for each character. When the innodb_large_prefix configuration option is enabled, the index key prefix length limit is raised to 3072 bytes for InnoDB tables that use DYNAMIC or COMPRESSED row format.

   Attempting to use an index key prefix length that exceeds the limit returns an error. To avoid such errors in replication configurations, avoid enablinginnodb_large_prefix on the master if it cannot also be enabled on slaves.

     The limits that apply to index key prefixes also apply to full-column index keys.

译文:

   默认情况下,索引键前缀长度限制为767字节。参见13.1.13创建索引语法。例如,如果在TEXTVARCHAR列上的列前缀索引超过255个字符,就可能会达到这个限制,假设使用utf8mb3字符集,每个字符的最大字节数为3字节。当innodb_large_prefix配置选项被启用时,对于使用动态或压缩行格式的InnoDB表,索引键前缀长度限制提高到3072字节。

   试图使用超过限制的索引键前缀长度将返回错误。为了避免在复制配置中出现这种错误,如果不能在从服务器上启用,请避免在主服务器上启用innodb_large_prefix

   适用于索引键前缀的限制也适用于全列索引键。

 

官网文档<https://dev.mysql.com/doc/refman/5.6/en/innodb-limits.html>

 

 

如果启用了系统变量innodb_large_prefix(默认启用,注意实验版本为MySQL  5.6.41,默认是关闭的,MySQL 5.7默认开启),则对于使用DYNAMICCOMPRESSED行格式的InnoDB表,索引键前缀限制为3072字节。如果禁用innodb_large_prefix,则对于任何行格式的表,索引键前缀限制为767字节。

 

 

解决:

1: 系统变量innodb_large_prefix为ON

2: 系统变量innodb_file_format为Barracuda

 

mysql> show variables like '%innodb_large_prefix%';
+---------------------+-------+
| Variable_name       | Value |
+---------------------+-------+
| innodb_large_prefix | OFF   |
+---------------------+-------+
1 row in set (0.00 sec)
mysql> set global innodb_large_prefix=on;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like '%innodb_file_format%';
+--------------------------+----------+
| Variable_name            | Value    |
+--------------------------+----------+
| innodb_file_format       | Antelope |
| innodb_file_format_check | ON       |
| innodb_file_format_max   | Antelope |
+--------------------------+----------+
3 rows in set (0.00 sec)
mysql> set global innodb_file_format=Barracuda;
Query OK, 0 rows affected (0.00 sec)

image.png

 

 

 

相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
2月前
|
关系型数据库 MySQL
MySQL创建表出现 Specified key was too long; max key length is 767 bytes
MySQL创建表出现 Specified key was too long; max key length is 767 bytes
29 2
|
2月前
|
关系型数据库 MySQL 数据库
Specified key was too long; max key length is 767 bytes导入mysql数据库表报错
Specified key was too long; max key length is 767 bytes导入mysql数据库表报错
25 0
|
2月前
|
存储 关系型数据库 MySQL
1071 - Specified key was too long; max key length is 767 bytes
【2月更文挑战第5天】1071 - Specified key was too long; max key length is 767 bytes 问题处理
|
1天前
|
SQL 存储 关系型数据库
MySQL数据库—初识数据库 | DDL语句 | DML语句
MySQL数据库—初识数据库 | DDL语句 | DML语句
|
1天前
|
Java 关系型数据库 MySQL
使用MySQL JDBC连接数据库
使用MySQL JDBC连接数据库
|
1天前
|
存储 关系型数据库 MySQL
|
3天前
|
关系型数据库 MySQL 测试技术
《阿里云产品四月刊》—瑶池数据库微课堂|RDS MySQL 经济版 vs 自建 MySQL 性能压测与性价比分析
阿里云瑶池数据库云原生化和一体化产品能力升级,多款产品更新迭代
|
4天前
|
存储 关系型数据库 MySQL
|
4天前
|
存储 SQL 关系型数据库
|
5天前
|
关系型数据库 MySQL 数据库
深入OceanBase分布式数据库:MySQL 模式下的 SQL 基本操作
深入OceanBase分布式数据库:MySQL 模式下的 SQL 基本操作