mysql max_allowed_packet 设置

  mysql5数据,插入或更新字段有大数据时(大于1M),会出现如下错误:
SEVERE: Servlet.service() for servlet [webs] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.TransientDataAccessResourceException: Hibernate flushing: Could not execute JDBC batch update; SQL [update t_article set (省略部分...)author_id=? where id=?]; Packet for query is too large (1117260 > 1048576). You can change this value on the server by setting the max_allowed_packet' variable.; nested exception is java.sql.BatchUpdateException: Packet for query is too large (1117260 > 1048576). You can change this value on the server by setting the max_allowed_packet' variable.] with root cause java.sql.BatchUpdateException: Packet for query is too large (1117260 > 1048576). You can change this value on the server by setting the max_allowed_packet' variable.

开始以为是相应字段的类型设置不正确,超出了字段的最大范围,网上查询后发现是mysql默认加载的数据文件不超过1M,可以通过更改mysql的配置文件my.cnf(Linux,或windows的my.ini)来更改这一默认值,从而达到插入大数据的目的。


解决:
MySQL的一个系统参数:max_allowed_packet,其默认值为1048576(1M),
可以先查询一下:show VARIABLES like '%max_allowed_packet%';
修改此变量的值:MySQL安装目录下的my.ini文件中的[mysqld]段中的"max_allowed_packet = 1M",如更改为4M(如果没有这行内容,增加一行),保存,重新启动MySQL服务。现在可以load大于1M的文件了。

有时候命令“ SET GLOBAL max_allowed_packet=16*1024*1024”无效,需要使用如下命令: set @@max_allowed_packet=5*1024*1024

猜你喜欢

转载自blog.csdn.net/acoolper/article/details/48347227
今日推荐