Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set mysql system variables such as max_allowed_packet max_allowed_packet, character_set_server and collation-server through Dockerfile?

I am using Docker for mysql. Need to set the system variables such as max_allowed_packet, character_set_server and collation-server of mysql in the Dockerfile. I tried with the below commands providing in the Dockerfile. but its not working.

Dockerfile:

FROM mysql:latest
VOLUME /opt/data:/var/lib/mysql
ENV MYSQL_ROOT_PASSWORD password
RUN sed -i 's/^max_allowed_packet.*/max_allowed_packet = 1073741824/' /etc/mysql/my.cnf
RUN sed -i "/\[mysqld]/a character-set-server=utf8mb4" /etc/mysql/my.cnf
RUN sed -i 's/^collation-server.*/collation-server = utf8mb4_unicode_ci/' /etc/mysql/my.cnf 
CMD ["mysqld"]

Build command:

sudo docker build -t mysqltest:1 .

Run command:

sudo docker run -p 3306:3306 -restart=always -d mysqltest:1
like image 573
User123 Avatar asked Dec 14 '22 22:12

User123


1 Answers

There are several approaches, this is one:

CMD ["--character-set-server=utf8mb4", "--collation-server=utf8mb4_unicode_ci", "--max-allowed-packet=1073741824"]

As an alternative you can put a custom config file in /etc/mysql/conf.d

custom-mysql.cnf:

[mysqld]
max_allowed_packet = 1073741824
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci

Dockerfile:

FROM mysql:latest
COPY ./custom-mysql.cnf /etc/mysql/conf.d/

More here

like image 125
Robert Avatar answered Apr 08 '23 19:04

Robert