Docker - MySQL commands within Dockerfile using RUN (ERROR 2002) - mysql

I am using Docker to create a dockerfile with mysql as the base image:
FROM mysql
#set root pass
ENV MYSQL_ROOT_PASSWORD password
#update linux
RUN apt-get update
#create database
RUN mysql -u root -ppassword -e "CREATE DATABASE dbname"
#install vim
RUN apt-get install vim -y
The dockerfile fails on the step where I try to create a database, it doesn't finish building and i receive this error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'
When I remove the #create database run command the dockerfile will build and I am able to run a container from that image. I know that it isn't a problem with the mysql server as I can enter the container and run the mysql command manually with success and the service status is running.
Using an environment variable i.e MYSQL_ROOT_PASSWORD within the file also allows me to create a database successfully but this will only work with a single database, I need to be able to use the mysql command to make queries, such as creating additional databases / assigning users etc.
This may be because I need to specify the host and port of the docker container but this still does not allow me to connect
RUN mysql -u root -ppassword -h 127.0.0.1 -P 3308 -e "CREATE DATABASE dbname"
Strangely, doing this also often crashes the container and puts it in a state where it will crash again on start-up every time that I try to restart it again.

I think the issue might be that in the service hasn't started within the container used to build your Dockerfile.
Try starting and configuring MySQL server within a single step. As a reference please check this file: https://github.com/dockerfile/mysql/blob/master/Dockerfile

Use below-given commands in your Dockerfile:
RUN service mysql restart && echo 'CREATE DATABASE db_name;' | mysql -uroot -
pYOUR_ROOT_PASSWORD

Had the very same problem: When starting the container and running a set of RUN instructions, or .sh or .sql scripts in /docker-entrypoint-initdb.d/ no connection to the database server could be established.
I found the solution by a comment of #wpalmer on the mysql-image:
The init scripts run by the entrypoint, internally, use the variable "${mysql[#]}" to call mysql (for example, when loading .sql files placed in the docker-entrypoint-initdb.d directory. Any .sh files which are processed by the entrypoint are included by "sourcing" them, meaning that variable is available for use by any .sh files which are run).
So what this means for you, instead of providing the plain mysql command with user, pass etc. as in
RUN mysql -u root -ppassword -e "CREATE DATABASE dbname"
use the placeholder instead:
RUN "${mysql[#]}" -e "CREATE DATABASE dbname"

You can try to build other image and run the create DB from there.
Example of docker-compose.yml
web:
build: web
links:
- "db:db.local"
entrypoint: entrypoint.sh
db:
build: db
environment:
MYSQL_ROOT_PASSWORD: password
command: mysqld
For entrypoint.sh you put something like this:
#!/bin/sh
#this is a hack to wait until the DB image is up and the port is open
until mysqladmin -u root -ppassword -e -h db.local ping; do
echo "$(date) - waiting for mysql"
sleep 3
done
if ! mysql -u root -ppassword -e -h db -e 'use dbname'; then
mysql -u root -ppassword -e "CREATE DATABASE dbname"
fi
exec "$#"

You can copy your queries as .sql file into "/docker-entrypoint-initdb.d" container directory. mysql will execute them after starting container
COPY ./init/db.sql /docker-entrypoint-initdb.d/
read official doc https://hub.docker.com/_/mysql?tab=description&page=1
Initializing a fresh instance

Related

How to get download a sql dump from docker MYSQL container from terminal

How to get download a SQL dump from docker MYSQL container from terminal
Version: mysql:5.7
I need to download a SQL dump, from the docker container. The database is available inside the docker container.
First we should be aware of running MySQL container host & port.
If you are not aware of it, please refer this link How to Find Running Docker container host & port
This is the command to download the mysql dump in terminal
mysqldump -u root -p --column-statistics=0 --host=0.0.0.0 --port=3310 local_db > localDb04Dec2021.sql
Why we have added in this flag --column-statistics=0 Refer this link to know mysqldump: Couldn't execute. Unknown table 'column_statistics' in information_schema
Hostname for this container is 0.0.0.0
Port 3310
local_db is the name of the mysql database
Dump file name is localDb04Dec2021.sql
After run this command the dump will be available is the path, which you are running this command
If you not aware of the current path, get to know by hit this pwd command

How to run mysql scripts in docker image?

In my Dockerfile
FROM mysql:5.7
ADD ./mysql_scripts /mysql_scripts
WORKDIR /mysql_scripts
RUN mysql -u root -p < create_user.sql &&\
mysql -u root -p < create_database.sql &&\
mysql -u root -p < create_tables.sql
EXPOSE 3306
but when I build that image I have problem like:Enter password: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'
You cannot run those mysql commands, because the container is not running yet, so neither is MySQL.
What you can do is copy your .sql files to /docker-entrypoint-initdb.d. MySQL will run all .sql, .sql.gz and .sh located in this directory after initialization, which would result in the following Dockerfile:
FROM mysql:5.7
COPY ./mysql_scripts /docker-entrypoint-initdb.d
EXPOSE 3306
More information can be found on the dockerhub page of mysql
You can not import database at build time, as every RUN command run in separate shell plus MySQL process also not ready to accept the connection.
You can take benefits from docker entrypoint that does these out of the box.
COPY create_user.sql /docker-entrypoint-initdb.d/
COPY create_database.sql /docker-entrypoint-initdb.d/
COPY create_tables.sql /docker-entrypoint-initdb.d/
So when the container started it will run the above script in alphabetical order
When a container is started for the first time, a new database with the specified name will be created and initialized with the provided configuration variables. Furthermore, it will execute files with extensions .sh, .sql and .sql.gz that are found in /docker-entrypoint-initdb.d. Files will be executed in alphabetical order. You can easily populate your mysql services by mounting a SQL dump into that directory and provide custom images with contributed data. SQL files will be imported by default to the database specified by the MYSQL_DATABASE variable.
mysql-docker-init-db
Also you use MYSQL_USER to create user.

Lost connection to MySQL server when creating database from script in a Docker container

I'm setting an script to configure Docker environments for developer local machines. A specifically part of the script consists to create a temporary container with a local volume to set the database i'll use in next step. this is the code used in the script:
docker run -d --name mysql_temp -v ~/dev/mysql:/var/lib/mysql/data -e MYSQL_ROOT_PASSWORD=test -p 3306:3306 centos/mysql-57-centos7:latest
the next step in the script is this line:
mysql -u root -h 127.0.0.1 --protocol=tcp --password=test -e "CREATE DATABASE db_test;"
then, the script returns me this error:
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 2
Curiously, if i force the exit with exit command after the first command and i execute the second one by my hand, directly in the terminal, it works and creates me the database.
Any idea what is this happening?
thanks in advance,
In your script, when the first line executes, it creates the docker container. But it takes time to initialise the mysql server on the container. The script executes the second line without waiting for mysql to initialise. Hence it cannot connect.
I'm assuming you have written a shell script. Try the following script.
docker run -d --name mysql_temp -v ~/dev/mysql:/var/lib/mysql/data -e MYSQL_ROOT_PASSWORD=test -p 3306:3306 centos/mysql-57-centos7:latest
sleep 10
mysql -u root -h localhost --protocol=tcp -ptest -e "CREATE DATABASE db_test;"
Note the second line having sleep 10. Basically this will create a delay of 10 seconds before executing the next command. Try varying the sleep time to higher if it still doesn't work.
Also change 127.0.0.1 to localhost and --password=test to -ptest
Note: You said that if you exit the script and run the mysql command manually, it works. Thats because by the time you tried running manually, the container has initialised mysql. Try running the docker run command in one terminal window. Once the container starts, try running the mysql command immediately in another terminal window to see if it works as quickly as you expected.

executing mysql script docker

I have a mysql docker container and it is up and running. I can get into it and see the mysql prompt. I do not want to mount external storage on it. Everytime I start this container, I want to execute a test.sql script from my file system to create DB and do few such actions. When I run the script which exists in my current working directory, it complaints. I know this is trivial but I am unable to catch the issue.
/microservices/mysqlcontainer]docker exec 3a21e5e3669d /bin/sh -c 'mysql -u root -ppwd <./test.sql'
/bin/sh: ./test.sql: No such file or directory
Since your script is outside the container, there's no need to catch the shell input redirection. You should be able to run the following:
docker exec -i 3a21e5e3669d mysql -u root -ppwd <./test.sql

Error start mysql docker

I am creating an image in the docker to the mysql install mode, but it is giving error to start mysql.
My Dockerfile
The error occurs when processing and line 25 of Dockerfile:
Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)
I found that the error occurs because mysql is not running. The docker print below to rotate the line 22:
bin boot dev etc home lib lib64 media mnt opt proc root run sbin scripts srv sys tmp usr var MySQL is stopped.
Stack trace complete
Any suggestion?
The docker daemon will execute RUN command one by one and commit the result, and seems your mysql service status is not committed to the image. To solve this problem, you may try these ways
Put all commands into one RUN command
RUN echo $(service mysql restart) && echo $(service mysql status) && sudo mysql -uroot -pmysql_pass -e "CREATE DATABASE wordpress;" && sudo mysql -uroot -pmysql_pass -e "CREATE USER 'wordpressuser'#'%';" && sudo mysql -uroot -pmysql_pass -e "SET PASSWORD FOR 'wordpressuser'#'%'= PASSWORD('${mysql_pass}');" && sudo mysql -uroot -pmysql_pass -e "GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpressuser'#'%'; FLUSH PRIVILEGES;"
initialize your mysql in entrypoint with a bash script.
init_mysql.sh
#!/bin/sh
sudo mysql -uroot -pmysql_pass -e "CREATE DATABASE wordpress;"
sudo mysql -uroot -pmysql_pass -e "CREATE USER 'wordpressuser'#'%';"
sudo mysql -uroot -pmysql_pass -e "SET PASSWORD FOR 'wordpressuser'#'%'= PASSWORD('${mysql_pass}');"
sudo mysql -uroot -pmysql_pass -e "GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpressuser'#'%'; FLUSH PRIVILEGES;"
and add following lines to dockerfile
ADD init_mysql.sh /
RUN \
cd / \
chmod 777 init_mysql.sh
CMD ./init_mysql.sh
You need to figure out why MySQL isn't starting. Log into the environment and manually try to start MySQL and see what the error is.
Have you considered basing your image on one of the official MySQL Docker images?
https://registry.hub.docker.com/_/mysql/