mysql: Packet for query is too large 和Data too long for column
今天在用mysql开发项目的时候,用BLOB字段存储文件二进制流。
一、先是报异常
com.mysql.jdbc.MysqlDataTruncation:
Data truncation: Data too long for column 'title' at row 1
发现上传的文件大于65535字符(mysql在UTF-8编码下汉字也是一个字符,跟oracle一个汉字3个字符不一样)
用show full fields from table_name; 查询的collation字段可以看到当前表的字符集。
一个BLOB或TEXT列,最大长度为65535(2^16-1)个字符。(0.06 M兆)
MEDIUMBLOB
MEDIUMTEXT
一个BLOB或TEXT列,最大长度为16777215(2^24-1)个字符。 (16M)
LONGBLOB
LONGTEXT
一个BLOB或TEXT列,最大长度为4294967295(2^32-1)个字符。(4G)
最后修改为LONGBLOB 解决第一个问题。
二、max_allowed_packet设置及问题
报的异常:com.mysql.jdbc.PacketTooBigException: Packet for query is too large (1521830 > 1048576). You can change this value on the server by setting the max_allowed_packet' variable
解决方法,在my.ini里的[mysqld]增加如下部分
[mysqld]
max_allowed_packet=64M
注意,在[client]和[mysql]部分增加无效,那个是客户端读取的参数。
然后一定记住重启MYSQL服务(不是终端),使用net stop mysql + net start mysql 或者 cmd->services.msc重启MYSQL服务,这里仅限于Windows
推荐阅读
-
com.mysql.jdbc.PacketTooBigException: Packet for query is too large 异常解决办法
-
MySQL Packet for query is too large 问题及解决方法
-
ERROR 1406 : Data too long for column 解决办法_MySQL
-
mysql: Packet for query is too large 和Data too long for column
-
mysql: Packet for query is too large 和Data too long for column
-
mysql: Packet for query is too large 和Data too long for column
-
mysql: Packet for query is too large 和Data too long for column
-
Packet for query is too large (1986748 > 1048576). Yo_MySQL
-
mysql- Packet for query is too large (60 > -1).
-
com.mysql.cj.jdbc.exceptions.PacketTooBigException: Packet for query is too large