docker-library / official-images

Primary source of truth for the Docker "Official Images" program
https://hub.docker.com/u/library
Apache License 2.0
6.46k stars 2.34k forks source link

Question: Howto access mysql via external tool? #1611

Closed jangalinski closed 8 years ago

jangalinski commented 8 years ago

I am starting the DB with

docker run --name _NAME_ -e MYSQL_ROOT_PASSWORD=_PW_ -e MYSQL_DATABASE=_DB_ -e MYSQL_USER=_USER_ -e MYSQL_PASSWORD=_UPW_ -d mysql:latest

The DB starts and if I connect to the docker container, I can access it via mysql console.

But when I try to connect with a DB tool like squirrel or IDE, I get a connection error. I guess this has something todo with the bind-address ... but I cannot manage to configure it correctly. Can anyone enlighten me?

yosifkit commented 8 years ago

(by "connect to the docker container" I am assuming you mean docker exec). It does bind to allow remote connections. I am able to access it from another container via its IP address.

$ docker run --name sql -e MYSQL_ROOT_PASSWORD=_PW_ -e MYSQL_DATABASE=_DB_ -e MYSQL_USER=_USER_ -e MYSQL_PASSWORD=_UPW_ -d mysql:latest
265835d4950db472951b0348489103a7ff354507247b0a18f6ca8539efda2acb
$ docker run -it --rm mysql mysql -h172.17.0.3 -uroot -p_PW_
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.12 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select 1;
+---+
| 1 |
+---+
| 1 |
+---+
1 row in set (0.00 sec)

mysql> ^DBye
jangalinski commented 8 years ago

Thanks for the help, but that didn't work for me. What I want to do is to connect to the mysql running inside the docker container from an application NOT running in a docker container. What is the jdbc url I have to use? I cannot get a connection.

yosifkit commented 8 years ago

If you are running on Linux, then you can connect to either, the ip of the container docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container-name on port 3306; or run the container with -p 3306:3306 and connect to your host at localhost:3306.

If you are on Windows or OSX with docker-toolbox then you need to use the -p 3306:3306 argument on the docker run and then access the IP address of the virtual machine docker-machine ip default on the same port.

$ # be sure to put the `-p` somewhere before the image name
$ docker run --name sql -e MYSQL_ROOT_PASSWORD=_PW_ -e MYSQL_DATABASE=_DB_ -e MYSQL_USER=_USER_ -e MYSQL_PASSWORD=_UPW_ -d -p 3306:3306 mysql:latest
jangalinski commented 8 years ago

Thank you very much ... when I read that port 3306 is already exposed by the image, I thought I wouldn't have to provide a port binding. But I am on OSX, so you really helped me out here.