increase max_allowed_packet size in mysql docker - mysql

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.

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

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

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

Related

bash command for runniung multiple commands in docker container

I'm trying to set up a database with a table inside a docker container. For correct working of DB, I need to run the following command:
--default-authentication-plugin=mysql_native_password
ps: I don't understand what this command exactly is for, but it prevents some strange logs by setting the DB up.
For set up, I use docker-compose as follow:
db:
image: mysql
command: >
bash -c "--init-file /pictureapi_mydb_response.sql
&& --default-authentication-plugin=mysql_native_password"
volumes:
- ./pictureapi_mydb_response.sql:/pictureapi_mydb_response.sql
restart: always
ports:
- 3307:3306
environment:
MYSQL_ROOT_PASSWORD: ${DB_MYSQL_PASS}
source
I'm getting the following errors:
bash: --: invalid option
db_1 | Usage: bash [GNU long option] [option] ...
db_1 | bash [GNU long option] [option] script-file ...
db_1 | GNU long options:
db_1 | --debug
db_1 | --debugger\
How should I actually run two or more commands if "bash" instruction doesn't work?
The Docker Hub mysql image is configured so that, if the command: starts with -, the entire command is assumed to be mysqld startup options. It's not actually "a command", and you can't use bash to run it. If you need multiple startup options, just combine them together into a single command::
command: --init-file /pictureapi_mydb_response.sql --default-authentication-plugin=mysql_native_password
When you launch a Docker container, its "entrypoint" and "command" parts are combined into a single command, so the command is passed as additional arguments to the entrypoint if both are present. The most common pattern is that the command is a complete executable command on its own, but there's an alternate pattern where the entrypoint is (or provides) the main command to run and the command just provides extra options.
The Docker Hub mysql container has a rather involved entrypoint script, but it eventually concludes with this logic:
# if command starts with an option, prepend mysqld
if [ "${1:0:1}" = '-' ]; then
set -- mysqld "$#"
fi
That is, if you run a container with
command: --an-option-starting-with-minus
the actual command the container runs is mysqld --an-option-starting-with-minus. So if you have multiple mysqld options you need to set, you can just pass them as the "command" and they'll get handled appropriately.

Build docker container for Mysql 8.0 fail

I need create a docker container for several projects tha use Mysql 8.0 with PHP 7.3
I like create it, because I need modify mysql startup configuration
For this I create
Dockerfile
FROM mysql:8.0
COPY mysqld_charset.cnf /etc/mysql/conf.d/mysqld_charset.cnf
ENV MYSQL_ROOT_PASSWORD="123456"
mysqld_charset.cnf
[mysqld]
default-authentication-plugin = mysql_native_password
collation-server = utf8mb4_general_ci
character-set-server = utf8mb4
License and readme files.
Execute
$ docker build --no-cache -t mysql8_legacy_password .
Sending build context to Docker daemon 14.85kB
Step 1/3 : FROM mysql:8.0
---> 62a9f311b99c
Step 2/3 : COPY mysqld_charset.cnf /etc/mysql/conf.d/mysqld_charset.cnf
---> 0e21143ae822
Step 3/3 : ENV MYSQL_ROOT_PASSWORD="123456"
---> Running in a8d350dbd651
Removing intermediate container a8d350dbd651
---> 7dd66b27be00
Successfully built 7dd66b27be00
Successfully tagged mysql8_legacy_password:latest
$ docker run --name mysql8_legacy_password -it mysql:8.0
error: database is uninitialized and password option is not specified
You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
The issue is in the run command.
docker run --name mysql8_legacy_password -it mysql:8.0
You are trying to start a container from mysql:8.0 image in which no env has been setup.
The last argument of the docker run command should be the image name. Since you have tagged your image as mysql8_legacy_password, this should work:
docker run --name container_name -it mysql8_legacy_password
docker run

Docker Using a custom MySQL configuration file Always fail

Follow the documentation here,Always fail
Using a custom MySQL configuration file
The default configuration for MySQL can be found in /etc/mysql/my.cnf, which may !includedir additional directories such as /etc/mysql/conf.d or /etc/mysql/mysql.conf.d. Please inspect the relevant files and directories within the mysql image itself for more details.
If /my/custom/config-file.cnf is the path and name of your custom configuration file, you can start your mysql container like this (note that only the directory path of the custom config file is used in this command):
$ docker run --name some-mysql -v /my/custom:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag
This will start a new container some-mysql where the MySQL instance uses the combined startup settings from /etc/mysql/my.cnf and /etc/mysql/conf.d/config-file.cnf, with settings from the latter taking precedence.
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:
My version Docker version 18.09.7, build 2d0083d
The commands I run
docker run --name mysql2 -v /my/custom:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=root -d mysql:8.0.16
My custom configuration file
[mysqld]
sql_mode="STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION"
I found the reason. Because I set the mysql.cnf permissions to 777.

how to start multi instances in one mysql docker container

I want to deploy data slot to the distributed mysql databases via middleware,need one mysql docker container running two instances with different port, eg. 3306 and 3316.
tried many ways, such as:
Add mysql_3316.sh:
#!/bin/bash
/entrypoint.sh --defaults-file=/etc/mysql/my_3316.cnf
in the rc.local:
#!/bin/sh -e
/usr/local/bin/mysql_3316.sh || exit 1
exit 0
and modified the Dockerfile like below,
RUN touch /etc/mysql/my_3316.cnf
COPY mysql_3316.sh /usr/local/bin/mysql_3316.sh
RUN chmod +x /usr/local/bin/mysql_3316.sh
COPY rc.local /etc/rc.local
RUN chmod +x /etc/rc.local
RUN chown root:root /etc/rc.local
ENTRYPOINT ["docker-entrypoint.sh"]
EXPOSE 3306 3316
CMD ["mysqld"]
it doesn't work while the mysql container comes up, but the 3316 mysql
port instance works by run the /entrypoint.sh --defaults-file=/etc/mysql/my_3316.cnf shell line manually.
tried the init.d ,
RUN touch /etc/mysql/my_3316.cnf
COPY mysql_3316.sh /etc/init.d/mysql_3316
RUN chmod +x /etc/init.d/mysql_3316
RUN update-rc.d mysql_3316 defaults 99
ENTRYPOINT ["docker-entrypoint.sh"]
EXPOSE 3306 3316
CMD ["mysqld"]
doesn't work too.
tried the crontab,
#reboot /usr/local/bin/mysql_3316.sh
#Don't remove the empty line at the end of this file. It is required to run the cron job
and the Dockerfile as that,
COPY mysql_3316.sh /usr/local/bin/mysql_3316.sh
RUN chmod +x /usr/local/bin/mysql_3316.sh
ADD crontab /etc/cron.d/docker-cron
RUN chmod +x /etc/cron.d/docker-cron
RUN crontab /etc/cron.d/docker-cron
ENTRYPOINT ["docker-entrypoint.sh"]
EXPOSE 3306 3316
CMD ["mysqld"]
doesn't work third.
It's been spent much on this key, almost give up...
Any kindly suggestion are welcomed please.
The docker-compose.yml for mysql right here:
services:
mysql:
image: mysql:latest
container_name: mysql
hostname: mysql
restart: unless-stopped
networks:
dockernet:
ipv4_address: 172.18.0.5
ports:
- 3306:3306
- 3316:3316
volumes:
- /Docker/mysql/:/var/lib/mysql/
- ./docker/mysql/mysql/my.cnf:/etc/mysql/my.cnf
- ./docker/mysql/mysql/my_3316.cnf:/etc/mysql/my_3316.cnf
- ./docker/mysql/mysql/logs/:/var/log/mysql/
- ./docker/mysql/mysql/init/:/docker-entrypoint-initdb.d/
entrypoint: ['/entrypoint.sh', '--default-authentication-plugin=mysql_native_password']
Normally you do NOT want to run more than one process in the same container. Despite your title I really think that what you are looking for is to start two containers, both from a MySQL image.
You should not need to change any startup scripts, Dockerfile or anything else to start up similar containers bound to different ports.
Remember that the EXPOSE command only exposes the ports to different containers, not to the outside world.
To access the port you need to use the -p flag with your docker run: https://docs.docker.com/engine/reference/run/#expose-incoming-ports
You can use the same docker image from the same Dockerfile. Just give different -p parameter when you run.
Edit:
You added your docker-compose.yml after my initial response. Using docker-compose will make my advice about -p obsolete, and you should use the ports: section of the docker-compose.yml to vary the port numbers instead.
This answer, however, might not be what you are looking for because based on your comment I think I do not fully understand your use case here.
Use the stock mysql container and just run:
docker run -p3306:3306 --name mysql1 mysql
docker run -p3316:3306 --name mysql2 mysql
# plus appropriate -d -e ... -v ... as needed on both commands
Don't try to build your own image and definitely don't try to run two servers with expected different lifetimes in a single container.

How to execute query using script in MySQL Docker image?

I am trying to give user a web interface in which , user can write a query and then i will be executing that query on my server.
I am using the following MySQL docker image with the latest tag i.e. mysql:latest
https://hub.docker.com/_/mysql/
So i am runnig the docker image using this command
docker run -it --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -v /root/ServerCode/:/usercode mysql /bin/bash
My root/ServerCode directory contains a script which i want to use for running mysql server and user's query.
My script is
#!/bin/bash
set -e
/etc/init.d/mysqld start
It gives me error
bash: /etc/init.d/mysqld: No such file or directory
I have also tried using this
service mysqld start
It is also giving error
mysqld: unrecognized service
Edit:
#!/bin/bash
set -e
exec 1> $"/usercode/logfile.txt"
exec 2> $"/usercode/errors"
# These output and error files are in mounted folder which i check after running script
/etc/init.d/mysqld start // run sql server here
#here i want to run that query and then get out of conatiner `
The entyrypoint scipt only does the initdb if mysqld is the argument; in your case it sees bash and so skips the initdb and just runs bash with its arguments.
If you are just trying to run some setup scripts once mysql is running have you looked at /docker-entrypoint-initdb.d/?
Create a docker-compose.yml
version: '2'
services:
db:
image: mysql
container_name: mysql
restart: always
volumes:
- /var/db/startuphry/mysql:/var/lib/mysql
- ./conf/my.cnf:/etc/mysql/conf.d/settings.cnf
- ./conf/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
environment:
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
- MYSQL_DATABASE=${MYSQL_DATABASE}
- MYSQL_USER=${MYSQL_USER}
- MYSQL_PASSWORD=${MYSQL_PASSWORD}
ports:
- "${MYSQL_PORT}:3306"
Create a conf folder add the my.cnf file to it
[mysqld]
local-infile=0
Create folder docker-entrypoint-initdb.d inside conf folder and all sql files inside this folder
Tree looks likes this
|____docker-compose.yml
|____conf
|___my.cnf
|___docker-entrypoint-initdb.d
|___one.sql
|___two.sql
You can put any .sh files or .sql files in there and they will be run/imported before the mysql service is available outside the container.
Try running the "/etc/init.d/mysqld start" inside the mysql docker container.
/root/server is a host machine path . Mysql has been installed in container not in the host machine. Please run the "/etc/init.d/mysqld start " not in the host machine.