Jepzen
Jepzen

Reputation: 3162

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 ?

Upvotes: 1

Views: 4479

Answers (2)

Prabal Srivastava
Prabal Srivastava

Reputation: 782

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. 

Upvotes: 9

ptrk
ptrk

Reputation: 1830

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

Upvotes: 0

Related Questions