18

I have two machines. My machine with IP1(Europe), and other machine with public IP2(USA). On IP2 I have mysql container running with volume /var/lib/mysql set to be replicated in some folder on the host machine ~/mysqldatabase. Firewall rule for port 3306 is added. Also I have ssh connection to the machine. So I'm not sure where to start. Usually when there is not docker I add just

bind-address = 0.0.0.0

as configuration in mysql and I open the firewall port 3306 (or an other one that points to mysql) and the things work. So probably I can install mysql-server package (the host is ubuntu16.04) outside of docker on the IP2 machine and to set it to point to the ~/mysqldatabase folder, but is it really necessary to do that? Is there way to connect directly from IP1 to IP2:mysql_container:mysql_database

I run the mysql docker container in two ways. One is with docker file. And the other one is with systemctl service.

Part of the docker-compose.yml:

version: "3"
services:
  mysql:
    image: mysql:5.7
    volumes:
      - /home/username/mysqldatabase:/var/lib/mysql
    ports:
      - '3306:3306'
    environment:
        MYSQL_ROOT_PASSWORD: rootpass
        MYSQL_DATABASE: somedb
        MYSQL_USER: someuser
        MYSQL_PASSWORD: someuserpassword

mysql.service

[Unit]
Description=Run %p
Requires=docker.service
After=docker.service

[Service]
Restart=always
ExecStartPre=-/usr/bin/docker kill %p
ExecStartPre=-/usr/bin/docker rm -f %p
docker run --rm --name mysql -v /home/username/mysqldatabase:/var/lib/mysql -p 3306:3306 \
-e MYSQL_ROOT_PASSWORD=rootpass -e MYSQL_DATABASE=somedb -e MYSQL_USER=someuser -e MYSQL_PASSWORD=someuserpassword \
mysql:5.7
ExecStop=/usr/bin/docker stop %p

[Install]
WantedBy=multi-user.target

To make the things more simple lets say that I use only the second approach.

I DON"T have :

  • 1.mysql-client, mysql-server or mysql on the IP2 machine
  • 2.and I haven't set anywhere to bind to 0.0.0.0 because I want to connnect directly to the mysql container. Probably I have to set it to bind to 0.0.0.0 inside this container.

Result for the firewall

sudo netstat -tunlp | grep 3306
tcp6       0      0 :::3306                 :::*                    LISTEN      24717/docker-proxy
makkasi
  • 6,328
  • 4
  • 45
  • 60
  • 1
    I am not still not clear as to what the question is here? There are two things `I have to set it to bind to 0.0.0.0 inside this container.` By default inside the container mysql bind is `0.0.0.0` only. Next when you use `-p 3306:3306` in docker that itself is a bind to `0.0.0.0` on the host machine. Which means the 3306 is exposed on the host a well. Now can you please clarify what exactly is the problem that you want to get resolved? – Tarun Lalwani May 29 '18 at 15:02
  • OK. I didn't know that part that by default in the container mysql is bind to 0.0.0.0 Thank you for that info. I don't have to try again then, because it didn't worked. I've used digital ocean droplet. But seems that I still don't have access to it. Somehow I cannot connect from IP1. I checked and the database is inside the mysql container. Also I can connect with the root password to mysql shell . The only thing that left is firewall may be. – makkasi May 29 '18 at 19:21
  • 1
    Yes there are two firewall rules, machine level and cloud provider level. Both need to have 3306 enabled for this to work – Tarun Lalwani May 29 '18 at 20:28
  • Using the same firewall settings and docker-compose I managed to connect finally. Using just docker doesn't work for some reason. Seems that there are new things with the new docker that I don't understand. There was not service created with 'docker run' command and the containers couldn't connect to each other. I couldn't fix this. But good thing is that docker-compose worked right away. I was two days on this problem with 'docker run' and I'm happy that I'm out of it. Seems that with docker 17.06 there is some 'docker swarm init' and a lot of additional things how to connect the containers – makkasi Jun 03 '18 at 14:24
  • @makkasi, could you post how you try to connect to mysql container and the reponse that you get? – andolsi zied Jun 05 '18 at 10:27

5 Answers5

7

for those who have not yet managed to connect even after all these tips. Consider using another port

Example:

ports:
      - '3308: 3306'

I solved my problem this way.

José Reis
  • 86
  • 1
  • 2
6

I think your mysql container is listening on ipv6 (seeing your netstat result). In cause may be your docker configuration. I don't know how to resolve it, I personnally just turn off ipv6 on my docker hosts to avoid this problem.

Hope it helps.

Webvoid
  • 501
  • 2
  • 7
3

You do not specify how you run your mysql container...

You need to specify which port should be "published", i.e. which port should go from "outside" to the "inside" of your mysql container.

To achieve that, you either specify a -p option when you do docker run or add it to your docker-compose.yml.

docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -p 3306:3306 -d mysql:tag

or docker-compose.yml

version: '3.1'

services:
  db:
    image: mysql
    restart: always
    ports:
      - 3306:3306
    environment:
      MYSQL_ROOT_PASSWORD: example

Read more about publishing ports here.

Kevin Kopf
  • 13,327
  • 14
  • 49
  • 66
  • Hi Alex. I have edited my post. I already have 3306 port public. Thank you for your answer. According this answer I don't need to install mysql-server on the IP2 host machine and I can connect directly to IP2 host mysql docker container. But there is some configuration missing. I guess that I have to set 0.0.0.0 inside the mysql container. Am I on the right track? – makkasi May 27 '18 at 05:42
0

You can modify your my.cnf and add bind_address=0.0.0.0

That is working for me. mysql configuration doc:bind_address

Timbus Calin
  • 13,809
  • 5
  • 41
  • 59
0

If mysql server and client are in version 8.x try

--protocol=TCP

to mysql client parameters

RaulRM
  • 11
  • 1