Mysql PacketTooBigException Packet for query is too large

来源:互联网 发布:黑暗之魂2原罪学者优化 编辑:程序博客网 时间:2024/06/05 03:38

一、问题描述

今天编辑富文本内容提交更新到数据库时,遇到以下错误:

Error updating database.  Cause: com.mysql.jdbc.PacketTooBigException: Packet for query is too large (23663004 > 4194304). You can change this value on the server by setting the max_allowed_packet' variable.

编辑器用的是 vue-quill-editor,这个编辑器对图片的处理是把图片进行 Base64 编码到文本中。

以上的错误提示里,是说查询的数据包太大??

二、解决方案

以上错误提示也给出了方案,设置 Mysql 的 max_allowed_packet 就可以了。
my.cnf(Linux) 或 my.init(Windows) 文件的 [mysqld] 下方,增加一行设置:

[mysqld]max_allowed_packet=128M

这个值默认是 4M,最大可设置为 1G,设置时必须是 1024 的倍数,否则会被 rounded down 到最近的一个 1024 的倍数值。

然后重启 Mysql 即可:

# service mysql restart

Mysql 官方文档 说明:

- - - System Variable Name max_allowed_packet System Variable Variable Scope Global, Session System Variable Dynamic Variable Yes - - - Permitted Values Type integer Permitted Values Default 4194304 Permitted Values Min Value 1024 Permitted Values Max Value 1073741824

The maximum size of one packet or any generated/intermediate string, or any parameter sent by the mysql_stmt_send_long_data() C API function. The default is 4MB.

The packet message buffer is initialized to net_buffer_length bytes, but can grow up to max_allowed_packet bytes when needed. This value by default is small, to catch large (possibly incorrect) packets.

You must increase this value if you are using large BLOB columns or long strings. It should be as big as the largest BLOB you want to use. The protocol limit for max_allowed_packet is 1GB. The value should be a multiple of 1024; nonmultiples are rounded down to the nearest multiple.

When you change the message buffer size by changing the value of the max_allowed_packet variable, you should also change the buffer size on the client side if your client program permits it. The default max_allowed_packet value built in to the client library is 1GB, but individual client programs might override this. For example, mysql and mysqldump have defaults of 16MB and 24MB, respectively. They also enable you to change the client-side value by setting max_allowed_packet on the command line or in an option file.

The session value of this variable is read only. The client can receive up to as many bytes as the session value. However, the server will not send to the client more bytes than the current global max_allowed_packet value. (The global value could be less than the session value if the global value is changed after the client connects.)

三、参考

  • 《com.mysql.jdbc.PacketTooBigException》
  • 《max_allowed_packet》
0 0