How to connect with MySQL Docker image from host? - mysql

I am Running a MySQL Docker image and creating database and table. The container is lauched with the command:
docker run --network host -p 3306:3306 --name mysql -e MYSQL_ROOT_PASSWORD=root -d mysql/mysql-server
Spark is running on my host machine, So i want to write data from Spark into the database running in the container.
But the connection does not succeed and I am getting an Exception:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

You can try this 2 solutions:
Check if your connection URL is not wrong in your java file.
Check if your container accept connections that is not localhost of the container:
Go to your cmd and enter in the container:
docker exec -it containerID /bin/bash
install vim inside container apt install vim
vim /etc/mysql/mysql.conf.d/mysqld.cnf
comment bind-adress or make it listen to 0.0.0.0
exit from container
restart
Hope this help.

As Schwarz54 mentioned in his answer, it could be host issue.
I usually pass it at the time of initialising the container using the MYSQL_HOST parameter.
docker run --name mysql_5.7 -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root1234 -e MYSQL_HOST=localhost mysql:5.7
I think you don't have to map the ports if you are running it on the host network.
docker run --name mysql_5.7 -d --network host -e MYSQL_ROOT_PASSWORD=root1234 -e MYSQL_HOST=localhost mysql:5.7
You can mount a local directory to the local container if you want the data to be persisted across container restarts using -v flag:
mkdir -p /Users/projects/data/mysql5.7
And mount this directory in the container at /var/lib/mysql
docker run --name mysql_5.7 -d --rm -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root1234 -e MYSQL_HOST=localhost -v /Users/projects/data/mysql5.7:/var/lib/mysql mysql:5.7

Related

Can't connect to Docker container running MySQL Server

The Docker run command is:
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=flurpgraSS -d mysql:5.7
Docker Desktop shows the container:
I can access the server from the CLI launched by Docker Desktop and the root account appears to be OK:
I can't access the server using MySQL Workbench
We might need to expose port from docker by -p parameter, otherwise, we can't access it outside.
docker run --name some-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=Danger42! -d mysql:5.7
There is some description from Container networking
-p 3306:3306:Map TCP port 3306 in the container to port 3306 on the Docker host.

Basic MySQL Docker Container

I just created a mysql docker container using
docker run -p 3310:3310 --restart always --env MYSQL_ROOT_HOST=% --name=ipca-mysql -e MYSQL_ROOT_PASSWORD=admin -d mysql/mysql-server:5.7
I am able to connect to my database using docker exec -ti ipca-mysql bash and mysql -u root -p
But when I tried to connect to my database at localhost:3310 using mysql workbench I get:
lost connection to mysql server at 'reading initial communication
packet' system error 0
Any idea what is this issue?
MySQL container start using this command it's worked.
docker run -p 3310:3306 --restart always --env MYSQL_ROOT_HOST=% --name=ipca-mysql -e MYSQL_ROOT_PASSWORD=admin -d mysql/mysql-server:5.7
You could use this docker command:
docker run --name mysql-tacs -e MYSQL_ROOT_PASSWORD=root -d -p 3310:3306 mysql
In the link below there is a video which shows step by step the creation of mysql server with docker until the connection in its server using MySQL Workbench.
https://www.youtube.com/watch?v=ZZwvhMvUJiw

Mysql docker container exited after start, option `-d` has no effect

I have problem with mysql-server container.
Post mysql with Exited(1) from docker has no solution for me included.
Here my workflow on windows 10.
0. Docker version:
Docker version 17.12.0-ce, build c97c6d6
1. My Dockerfile:
FROM mysql/mysql-server
ENV MYSQL_ROOT_PASSWORD root
ENV MYSQL_DATABASE blockchain
ENV MYSQL_USER block
ENV MYSQL_PASSWORD blockchain
COPY create_schema.sql /docker-entrypoint-initdb.d/create_schema.sql
EXPOSE 3306
Build command:
docker build -t mysqlserver .
Run command (option -d is used):
docker run -ti -p 3306:3306 --name mysqlserver1 -v C:/Users/user/sandbox/mysql:/var/lib/mysql -d --net testnetwork mysqlserver --innodb_use_native_aio=0
But result after start is: Exited (1) 11 minutes ago, declared path folder for database is initialized.
Where is my error ?
Thx for help
For mysql docker container, you need to specify the following parameters in your Dockerfile;
MYSQL_ROOT_PASSWORD
Initial Database to be created
Expose Port from the docker container to the host machine
For instance if you're creating the running mysql docker image using terminal, it could be: sudo docker run -e MYSQL_ROOT_PASSWORD=dev --name testdb -p 3800:3600 -d mysql:8.0.17

Docker containers run async

I need to develop docker in my application, i create docker image with maven-docker-plugin. After that I run sh script whit is here.
docker run --name app-mysql -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=app -d mysql
docker run --name app -p --link app-mysql:localhost -d app
docker run --name app-nginx -d -p 80:80 --link app:app nginx
docker exec app-nginx rm -rf /etc/nginx/conf.d/default.conf
docker cp app:/default.conf default.conf
docker cp default.conf app-nginx:/etc/nginx/conf.d/default.conf
docker restart app-nginx
But i have a problem. First mysql container runns good. Second, app container runns good. But nginx container throws error :
docker: Error response from daemon: Cannot link to a non running container: /app AS /app-nginx/app.
If i run this scrip with command
sleep 120
between the containers it setups normally, as i understand nginx container runs before app container finishes. Is there any way to run containers without sleep command as it is hardcoded. How can i figure if container finished successfully. Can you help me with solution?
I would prefer to use something like docker-compose for this. But you can still manage with your code. The key is not to start the container but create it first and then start it
docker run --name app-mysql -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=app -d mysql
docker run --name app -p --link app-mysql:localhost -d app
docker create --name app-nginx -d -p 80:80 --link app:app nginx
docker cp app:/default.conf app-nginx:etc/nginx/conf.d/default.conf
docker start app-nginx
Also the --link is deprecated and should not be used. You should be using docker network create to create a network and then assign that network to your containers using --net. docker-compose does a lot of these things automatically for you and that's why it makes more sense to use that instead

how to extend docker mysql image

I pull the mysql/mysql-server image, then I execute the following commands below:
docker run --name myapp -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql/mysql-server
docker exec -it myapp bash
After this, I install jdk and tomcat in the newly created myapp container, then I exit the shell and run:
docker commit myapp myappwithjdk
Then I run the following, but the container exits immediately:
docker run -e MYSQL_ROOT_PASSWORD=my-secret-pw -d myappwithjdk
I don't know why it does this.
It could be leftovers from running mysql (like pid file). It is bad approach to use exec & commit for creating own container based on different one. Much better to create own image via Dockerfile:
FROM mysql/mysql-server
RUN <your commands here>
and then
docker build -t myappwithjdk .
The mysql image have the way to execute init scripts (both bash and sql) once on container creation. Simply put them to /docker-entrypoint-initdb.d/ folder:
FROM mysql:latest
ENV MYSQL_ROOT_PASSWORD=root
COPY 00-extra_env_setup.sh /docker-entrypoint-initdb.d/00-extra_env_setup.sh
COPY 01-user_setup.sql /docker-entrypoint-initdb.d/01-user_setup.sql
build:
docker build -t mymysql .
and run(note port exposing):
docker run -p 3306:3306 mymysql
now you able connect to it outside(change the ip to the ip of your docker server):
mysql --host 127.0.0.1 --port 3306 --user root --password
That's it!