com.mysql.jdbc.PacketTooBigException

I am storing images in MYSQL.

Tengo mesa como

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?


actualización 1

As per older SO question, I also tried with MEDIUMBLOB pero sigo recibiendo el mismo error.

Agregar imagen a una base de datos en Java


actualización 2

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

SET GLOBAL max_allowed_packet = 1024*1024*14;

preguntado el 03 de julio de 12 a las 23:07

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 discouraged -

5 Respuestas

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.

Respondido 03 Jul 12, 23:07

I do understand, but my client want to store in mysql strictly :( - Fahim Parkar

@FahimParkar: Then increase max_allowed_packet? - huevo

just curious how to set max_allowed_packet? - Fahim Parkar

@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. - huevo

Thanks for help... now problem is solved... I set SET GLOBAL max_allowed_packet = 1024 * 1024 * 14 i.e. 14 MB - Fahim Parkar

Para usuarios de Windows:

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 podría 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 Grupo cual es [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 seguido por net start MySQL).

Respondido 25 ago 16, 20:08

Seguir funcionó para mí

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

Respondido 24 Abr '15, 03:04

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

Respondido 31 Jul 18, 08:07

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.

Los siguientes pasos funcionaron:

  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.

Respondido el 23 de diciembre de 20 a las 22:12

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.