Can't access SQL db inside Docker container - mysql

I have a SQL db running successfully. However, I cannot access it in my SQL client.
docker run -it --name sql_key_container -e MYSQL_ROOT_PASSWORD=testpw -e MYSQL_DATABASE=key_volume -p 3306:3306 -d myimgname/ubuntumaria:0.2
I verified it's running by docker exec fcd12bf1da81 /etc/init.d/mysql status
I mapped 3306 -> 3306 and using the host IP. Still getting a timeout. What could this be?

You have now a database in container fcd12bf1da81 running, listening on its local IP and on the host IP on port 3306.
If you want your new container sql_key_container to access that database, then the container needs access to fcd12bf1da81.
You need to --link:
docker run -it --link fcd12bf1da81:mydb --name sql_key_container -e MYSQL_ROOT_PASSWORD=testpw -e MYSQL_DATABASE=key_volume -p 3306:3306 -d myimgname/ubuntumaria:0.2
and now you can access the db inside that container with mydb:3306.

Related

MySQL shell cannot connect to MySQL docker container

I have a MySQL docker image running on Windows. I created it like this
docker pull mysql
docker run --name localmysql -p 3307:3306 -e MYSQL_ROOT_PASSWORD=r00t -d MySQL
and docker ps returns the ports as
33060/tcp, 0.0.0.0:3307->3306/tcp
When I go into MySQL shell and try to connect, I get an error. This is how I connect
\c -h localhost -P 3307 -u root -p r00t
If I am connecting wrongly, how should I connect in MySQL shell and should it work? Is there anything in my docker setup that would stop the connection working?

Accessing local MySQL server from my docker container

I have a mysql server and a docker container running on my machine. In the docker container, I run a django website. I want to connect to the local mysql server from the docker container. How can I do that?
I usually do ( for testing purposes ) :
docker network create -d my-bridge
docker run --network my-bridge --name app-db -e MYSQL_ROOT_PASSWORD=secret -e
MYSQL_DATABASE=myapp -e MYSQL_USER=myapp_user -e MYSQL_PASSWORD=myapp_secret
mysql:latest
docker run --network my-bridge --name app -p 80:80 -e DB_HOST=app-db -e DB_USER=myapp_user -e DB_PASS=myapp_secret -e DB_NAME=myapp myapp:latest
In my app Dockefile I am using in entrypoint something like envsubst, or if code language can read from environment variables I dont need to setup this.
Forget about --link docker parameter -> its obsolete

Cant connect to Mysql docker container from host

Im running docker on windows and I start up a docker container with MySql like this
docker run -p 3306:3306 --name test -e MYSQL_ROOT_PASSWORD=secret-pw -d mysql/mysql-server:5.5
Then on my host I start up Mysql workbench and try to connect but it does not work.
docker inspect test reveal IP address on 172.17.0.2 but when I ping this I get no reply
Got this working on a linux host and I am pretty sure I have done the exact same steps
What am I doing wrong ?
Help Doc: https://docs.docker.com/samples/library/mysql/
Image link: https://store.docker.com/images/mysql
Command: docker run --name mysql_container_name --expose=3306 -p 3306 -v /my/own/datadir:/path/to/data/dir -e MYSQL_ROOT_PASSWORD=root_pwd -d mysql:tag --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
mysql_container_name: docker container name
expose: contaner exposeing port
p: host buinding port
/path/to/data/dir: share path between container and host
root_pwd: mysql root password
tag: repository tag
utf8mb4: mysql server character set and collation type
utf8mb4_unicode_ci: mysql server character set and collation type
Example: docker run --expose=3306 -p 3306 --name mysql -v /my/own/datadir:/opt/mysql -e MYSQL_ROOT_PASSWORD=0112358139 -d mysql:latest --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
Following steps to connect remotely:
docker exec -it mydb bash --> this will connect to mySql container.
echo "bind-address = 0.0.0.0" >> /etc/mysql/my.cnf --> this will update the my.cnf file.
service mysql restart --> restart the mySql service.
exit --> the mySql container.
docker inspect mysql | grep IPAddress --> grep the IP address of the contaner.
mysql -h 172.17.0.2 -u root –p --> remotely connect to the mySql.
Your host 3306 port should be forwarding to the container, so try connecting on localhost:3306. When I tried to replicate, got the "Host 172.17.0.1 not allowed to connect to this MySQL server" which means it got through at least.
More on the latter: https://github.com/fideloper/docker-mysql/issues/10

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.

Connecting to Docker container from host

I just pulled and run the official Docker MySQL image and have it running locally on my machine:
docker run --name mydb -e MYSQL_ROOT_PASSWORD=12345 -d mysql:5.7.11
The instructions on that screen tell you how to connect to the MySQL server (container) from inside yet another container (which is configured as a command-line client). But I have a generic JDBC fat client (SQuirreL) and am wondering how to connect to my docker container.
For the JDBC connection string, I need to provide both a hostname and a dbname, what can I use? I would imagine the Docker container is somehow addressable from my host OS, but I haven't actually created any databases yet, so I'm not sure what dbname value I can supply:
jdbc:mysql://<hostname>:3306/<dbname>
You could run your instance with forwarding 3306:
$ docker run --expose=3306 -p 3306 mysql
See incoming ports.
The you specify:
jdbc:mysql://127.0.0.1:3306/<dbname>
You command become:
$ docker run --name mydb -e MYSQL_ROOT_PASSWORD=12345 -d --expose=3306 -p 3306 mysql:5.7.11
You might need to change the MySQL configuration.
Can go inside the container with:
$ docker exec -it mydb bash
And then you could:
$ echo "bind-address = 0.0.0.0" >> /etc/mysql/my.cnf
Don't forget to reload mysql.
Then you have to create the database and import your schema (if needed).
$ mysql -uroot -p12345 -e"CREATE DATABASE mydb"
$ mysql -uroot -p12345 mydb < mydb-schema.sql