com.mysql.jdbc.PacketTooBigException

Fahim Parkar picture Fahim Parkar · Jul 4, 2012 · Viewed 39.7k times · Source

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;

Answer

eggyal picture eggyal · Jul 4, 2012

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.