com.mysql.jdbc.PacketTooBigException
Asked Answered
S

5

20

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;
Stricklin answered 3/7, 2012 at 22:42 Comment(1)
That error relates to the buffer size when data is being received by MySQL. A proper size for that parameter is about 15M. P.S. Storing images in DB should be discouragedBodwell
G
24

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.

Graptolite answered 3/7, 2012 at 22:44 Comment(5)
I do understand, but my client want to store in mysql strictly :(Stricklin
@FahimParkar: Then increase max_allowed_packet?Graptolite
just curious how to set max_allowed_packet?Stricklin
@FahimParkar: SET GLOBAL max_allowed_packet = ...... You should probably set it in your MySQL configuration file if you want it to persist across server restarts.Graptolite
Thanks for help... now problem is solved... I set SET GLOBAL max_allowed_packet = 1024 * 1024 * 14 i.e. 14 MBStricklin
B
2

For Windows users:

mysql_home points to your mysql/mariadb installation folder.

open cmd, cd to %mysql_home%\bin and run mysqladmin > temp.txt This will spit out a lot of information about the usage of the tool. Somewhere among all that output you will find this information:

Default options are read from the following files in the given order: C:\windows\my.ini C:\windows\my.cnf C:\my.ini C:\my.cnf c:\mariadb-5.5.29-w inx64\my.ini c:\mariadb-5.5.29-winx64\my.cnf

This shows that you could have, if you don't have it already, a file called my.ini or my.conf in the %mysql_home% directory.

create my.ini and add the lines:

[mysqld]
#allow larger BLOBs to be stored
max_allowed_packet = 10M

make sure to include the settings group which is [mysqld] otherwise it will fail to start (and for me it ended up hanging in limbo).

You will now need to restart the MySQL daemon, this is done either by killing and starting the currently running mysqld process or by restarting the MySQL service (run services.msc, locate MySQL, press the restart button; or from cmd, net stop MySQL followed by net start MySQL).

Baziotes answered 12/4, 2016 at 8:57 Comment(0)
A
1

Following worked for me

edit my.cnf file ( mine was in /etc/mysql )

Then modify the max_allowed_packet value I set it to max_allowed_packet=200M

Make sure you restart MySQL for change to take effect

Aubreir answered 24/4, 2015 at 2:44 Comment(0)
T
0

If working with AWS RDS, max_allowed_packet can be modified using DB Parameter Groups

Tenney answered 31/7, 2018 at 7:45 Comment(0)
U
0

The max_allowed_packet variable has a default value set in the configuration file (my.ini in my case). If your application tries to execute a query whose packet size exceeds this value, this exception is thrown.

My setup is on a Windows 10 machine with MySQL Server 8.0.

I copied the my.ini file with my desired value (64M) for max_allowed_packet to the various possible locations (C:\my.ini, C:\Windows\my.ini). Then restarted the mysql server. It didn't work. When I queried the database for the max_allowed_packet variable, the value remained unchanged.

The following steps worked:

  1. I discovered that there is a my.ini file at a different location.

  2. Open the my.ini file at the following location C:\ProgramData\MySQL\MySQL Server 8.0. This has lots of entries, besides max_allowed_packet.

  3. Locate the entry for max_allowed_packet, and set it to the desired value (for e.g. 64M).

  4. Save and close the my.ini file.

  5. Restart the MySQL80 service.

  6. Log into to the mysql server prompt and run the query

     show variables like 'max_allowed_packet';
    
  7. You should see the value set to your desired value.

Uni answered 23/12, 2020 at 22:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.