Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

blob - com.mysql.jdbc.PacketTooBigException

I am storing images in MYSQL.

I have table as

CREATE TABLE myTable (id INT, myImage BLOB);

When I am trying to insert 4.7MB file, I am getting exception as

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.

I believe this is related to image size only. Is there any other variable type that I can use?


Update 1

As per older SO question, I also tried with MEDIUMBLOB but still I am getting same error.

Adding Image to a database in Java


Update 2

At the start of the project, I execute below query and everything is working now

SET GLOBAL max_allowed_packet = 1024*1024*14;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As the error says, it has nothing to do with variable type but rather the max_allowed_packet variable:

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.

But, generally speaking, don't store files in your database - store them in your filesystem and record the path to the file in the database.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...