我在 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 largestBLOB
you want to use. The protocol limit formax_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/