45

We are using Docker for mysql, We are facing the below error while running

Packet for query is too large (12884616 > 4194304). You can change this value on the server by setting the max_allowed_packet' variable.; nested exception is com.mysql.jdbc.PacketTooBigException: Packet for query is too large (12884616 > 4194304). You can change this value on the server by setting the max_allowed_packet' variable.

now we need to increase max_allowed_packet size in mysql configuration, Can anyone help me on docker command to increase max_allowed_packet.

3 Answers 3

97

As an argument to the container command:

docker run -it -e MYSQL_ROOT_PASSWORD=my-secret-pw mysql:5.7 --max-allowed-packet=67108864

See "Configuration without a cnf file" at https://hub.docker.com/_/mysql/, copied here for posterity:

Configuration without a cnf file Many configuration options can be passed as flags to mysqld. This will give you the flexibility to customize the container without needing a cnf file. For example, if you want to change the default encoding and collation for all tables to use UTF-8 (utf8mb4) just run the following:

$ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci

If you would like to see a complete list of available options, just run:

$ docker run -it --rm mysql:tag --verbose --help

When using docker-compose (as asked in the comments), add a command key with the arguments:

version: "3"
services:
  data:
    image: "mysql:5.7.20"
    command: --max_allowed_packet=32505856      # Set max_allowed_packet to 256M (or any other value)
    environment:
      - MYSQL_ROOT_PASSWORD=password
      - MYSQL_DATABASE=db
      - MYSQL_USER=user
      - MYSQL_PASSWORD=user_password
4
  • 2
    Any help in docker compose file ?
    – user6830821
    Jan 9, 2018 at 17:42
  • 1
    Do I have to rebuild with the docker-compose file? or What should I do next? run docker-compose down and then up again?
    – Tan Dat
    Feb 4, 2018 at 2:28
  • 1
    @TanDat yes, you will need to reconfigure the service using docker-compose down and docker-compose up like you stated.
    – Mike Hill
    Aug 2, 2018 at 17:37
  • 2
    I find it essential with sql imports to also ensure proper connection and database encoding. For that it's best to change command to this: command: [ "--character-set-server=utf8mb4", "--collation-server=utf8mb4_unicode_ci", "--init-connect='SET NAMES utf8mb4'", "--max-allowed-packet=67108864" ] and add another environment parameter: - LANG=C.UTF-8
    – Sharak
    Apr 8, 2021 at 11:37
25

When using docker-compose (as asked in the comments), add a command key with the arguments, for example:

version: "3"
services:
  data:
    image: "mysql:5.7.20"
    # Set max_allowed_packet to 256M (or any other value)
    command: --max_allowed_packet=32505856
    environment:
      - MYSQL_ROOT_PASSWORD=password
      - MYSQL_DATABASE=db
      - MYSQL_USER=user
      - MYSQL_PASSWORD=user_password

Whilst this works for the MySQL docker images, it may not work for all is it overwrites the default command.

More info: Docker Compose - Command

1
  • It works in my docker compose file. Thanks!
    – Hua Zhang
    Jun 16, 2022 at 16:32
5

I think you should modify it in your Dockerfile like this :

RUN sed -ire 's/max_allowed_packet.*=.*/max_allowed_packet = YOURVALUE/g' /etc/mysql/my.cnf

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.