How can I to connect to a Docker running mysql? - mysql

The instructions here: https://hub.docker.com/_/mysql indicate to run
$ docker run -it --network some-network --rm mysql mysql -hsome-mysql-container -uexample-user -p
But i have no idea what some-network is? So i run this instead and get and 'unknown MySQL host' error even though some-mysql-container is definitely the name of my container.
$ docker run -it--rm mysql mysql -hsome-mysql-container -uexample-user -p
What am I doing wrong here?

'some-network' refers to a docker-network. You need to create it first. I named it 'mysql-network' to make its purpose a bit more clear:
docker network create mysql-network
Then, start database container:
docker run --network mysql-network --name mysql-db -e MYSQL_ROOT_PASSWORD=mysecretpassword -d mysql
Then, start a the client container to connect to the first one:
docker run -it --network mysql-network --rm mysql mysql -hmysql-db -uroot -p
By adding both containers to the same network, they are able to communicate with each other.

Related

run a docker mysql container and open it in one command

I want to run and open a mysql Cli in docker just with one command . Something like this is not working:
docker run --rm -it -p 33060:3306 --name mydb -e MYSQL_ROOT_PASSWORD=secret mysql mysql -p
I know I can connect to mysql after running my container this way
docker -it docker exec -it mydb mysql -p
but i want to do it in one liner.
Thanks
(Updated)*****
Seems that you can do it in version 8 calling MySQLsh at the end of the command. But unable to do it for previous versions
docker run --name=mk-mysql -p3306:3306 -e MYSQL_ROOT_PASSWORD=my-secret-pw -it mysql/mysql-server:8.0.20 mysqlsh
The database server and client are two separate programs. A container only runs one program, so you can't run both the server and the client in the same container, both as the main process. You could write a script that starts the container and then runs mysql to connect to it, but that's about the best you can do.
#!/bin/sh
docker run -d -p 33060:3306 --name mydb -e MYSQL_ROOT_PASSWORD=secret mysql
exec mysql --host=127.0.0.1 --port=33060 --connect-timeout=60 --wait --password
If you're trying to do this to create a database or do other first-time initialization, you can bind-mount an initialization script into /docker-entrypoint-initdb.d and it will run as part of the database setup (only the very first time the database is started).
# Create the storage for the database
# (delete and recreate to rerun the init script)
docker volume create mysql-data
docker run \
-v mysql-data:/var/lib/mysql \
-v $PWD/init.sql:/docker-entrypoint-initdb.d/init.sql \
... \
mysql
If you're just trying to experiment with SQL commands, a serverless database like SQLite might fit your needs better.
the -p parameter is for the ports to be published and should not be part of the -it interactive, that should be your error,
Have a read of the docker run command, in the docker documentation,
https://docs.docker.com/engine/reference/run/

using docker images for mySQL and redmine, how do I resolve "Unknown MySQL server host"?

I am using the docker images supplied at https://hub.docker.com/_/redmine
I have chosen to use MySQL as my database backend. So I have 2 docker containers: MySQL and Redmine, as downloaded from dockerhub.
Following the instructions on the docker/redmine link above, I ran through the commands and found that the redmine docker would not start. Inspecting the docker logs, I see:
rake aborted!
Mysql2::Error::ConnectionError: Unknown MySQL server host redmine (-5)
I thought the 2 dockers were having difficulty talking to each other, so I setup a new docker network for both containers to use:
docker network create --driver bridge redmine-net
Adapting the instructions, on the docker/redmine link above, I run
docker run -d name our-mysql --network redmine-net -e MYSQL_USER=redmine -e MYSQL_PASSWORD=todays-password -e MYSQL_DATABASE=redmine -e MYSQL_RANDOM_ROOT_PASSWORD=1 -p 3306:3306 mysql:5.7
docker run -d name our-redmine --network redmine-net -e REDMINE_DB_MYSQL=redmine -e REDMINE_DB_USERNAME=redmine -e REDMINE_DB_PASSWORD=todays-password redmine:latest
However, the redmine contain still falls over instantly, with the same error.
EDIT Using the *.yml file as provided in the dockerhub redmine instructions works pretty faultlessly.
So the question is: what is the docker-compose method doing that docker run isn't handling?
Thank you.
The REDMINE_DB_MYSQL arg of the redmine container do reference to the mysql container, so, if you define the database service like our-mysql, then set REDMINE_DB_MYSQL=our-mysql

MySQL data directory stays default with -v flag in docker image

I am trying to start MySQL using docker image, I wanted to have a look at the binlog files, however I couldn't find them in /var/lib/mysql. From a few stackoverflow and Google reads, potential reason could be that mysql doesn't have permissions to write in /var/lib/mysql.
So I tried providing a different path using -v flag while starting the docker using the command docker run -it --rm --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=debezium -e MYSQL_USER=mysqluser -e MYSQL_PASSWORD=mysqlpw -v /home/username/mysql debezium/example-mysql:1.1
However, even after this, datadir variable in client still remains /var/lib/mysql. Can someone help me in this?
Using docker run -it --rm --name mysqlterm --link mysql --rm mysql:5.7 sh -c 'exec mysql -h"$MYSQL_PORT_3306_TCP_ADDR" -P"$MYSQL_PORT_3306_TCP_PORT" -uroot -p"$MYSQL_ENV_MYSQL_ROOT_PASSWORD" to start the MySQL client.
You are running client on different container and expect yo find logs there?
if you want see log files, you should run docker exec mysql bash, sometimes bash is not available, then use sh.

Connecting 2 conatiners with docker --network

I am trying to connect a phpmyadmin container to another mysql container. I have created a network with docker network create and added my two containers. But when I tried to login with phpmyadmin, the password is incorrect error displayed each time.
I checked that the two containers are available to my networks which I already created so I tried to ping mysql container to phpmyadmin container but unfortunately I had negative results.
I followed this tutorial:
https://tecadmin.net/tutorial/docker/docker-networking-example/
Can you explain to me how the commend docker run --network works exactly ?
If everything is ok, so you can ping one container from another using container name. When you create a network between 2 containers, it creates an alias with the container name and the container IP.
For instance
docker network create my-bridge-network
docker run --name mysql -e MYSQL_ROOT_PASSWORD=secret -d mysql/mysql-server --network my-bridge-network
docker run --name phpmyadmin -d -e PMA_HOST=mysql -p 8080:80 phpmyadmin/phpmyadmin --network my-bridge-network
In this case when you exec ping from phpmyadmin container you should be able to ping mysql container
docker exec phpmyadmin ping mysql # if in phpmyadmin `ping` program is available

Avoid hard coding the mysql container ip in my apache container script

I have a mysql container which runs fine. I can start it and see it up and running in the docker ps list.
I then try to run another learnitouch container in which an engine-db-seed.sh shell script tries to connect to the mysql container server.
The learnitouch container Dockerfile contains:
ENTRYPOINT ["/bin/bash", "/usr/bin/learnintouch/engine-db-seed.sh"]
The engine-db-seed.sh file contains:
/usr/bin/mysql/install/bin/mysql --protocol=tcp -h 172.17.0.2 -u root -proot -v < /usr/bin/learnintouch/db_engine-db.sql
The db_engine-db.sql is being seeded all right in the mysql database.
But I had to hard code the mysql container IP as you can see in the -h option. I got the 172.17.0.2 IP address from a docker inspect on the mysql container. Not the most automated way...
How can I do without such hard coding ?
Running the mysql container:
docker run -d -p 3306:3306 -v /home/stephane/dev/php/learnintouch/docker/mysql/data:/usr/bin/mysql/install/data --name mysql stephane/mysql:5.6.30
Running the learnintouch container:
docker run -p 127.0.0.1:80:80 --link mysql:mysql --name learnintouch stephane/learnintouch
I'm using Docker version 1.12.1, build 23cf638
Just use the service name and make sure that both services are running on the same network (bridge0 by default).
So if you create your mysql service like this
docker run -d --name foo mysql-image
your engine-db-seed.sh could then be
/usr/bin/mysql/install/bin/mysql --protocol=tcp -h foo -u root -proot -v < /usr/bin/learnintouch/db_engine-db.sql
mysql will make a dns request for foo which will be resolved by Docker to the ip of your foo service.