mysql - com.mysql.jdbc.PacketTooBigException

标签 mysql blob packet

我在 MYSQL 中存储图像。

我有 table

CREATE TABLE myTable (id INT, myImage BLOB);

当我尝试插入 4.7MB 文件时,我收到异常

com.mysql.jdbc.PacketTooBigException: Packet for query is too large (4996552 > 1048576). You can change this value on the server by setting the max_allowed_packet' variable.

我相信这仅与图像大小有关。还有其他我可以使用的变量类型吗?


更新 1

根据较早的 SO 问题,我也尝试使用 MEDIUMBLOB 但仍然遇到相同的错误。

Adding Image to a database in Java


更新 2

在项目开始时,我执行以下查询,现在一切正常

SET GLOBAL max_allowed_packet = 1024*1024*14;

最佳答案

如错误所述,它与变量类型无关,而是与 max_allowed_packet 有关变量:

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.

但是,一般来说,不要将文件存储在数据库中 - 将它们存储在文件系统中并在数据库中记录文件的路径。

关于mysql - com.mysql.jdbc.PacketTooBigException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11320236/

相关文章:

python - 如何在我的 App Engine 应用程序中请求本地 URL?

udp - 路由器将记录保留在NAT中多长时间,并且可以重用来自其他主机的转发请求吗?

mysql - 如何只显示同一个id的一条数据

MySQL - 使用不在列表中的列按语句排序

sql-server - 从 SQL Server 导出 Blob 并将其另存为文件

javascript - blob 到 base64 转换 javascript

sockets - HTTP 丢包

c++ - 示例 RTP 数据包流

php - 如何通过 Laravel 5 中的 SQL 查询在模型中分配动态属性

sql - mysql_num_rows 错误