0

I have a local spring boot application that connects to local MySQL and it works fine.

For connection I use the following property:

spring.datasource.url=jdbc:mysql://localhost:3306/pitstop?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=UTC

I would like to put my app in docker and try to connect to local DB.
So I need to modify MySQL url.

I used this command to obtain local IP ip route show | grep "default" | awk '{print $3}' the result is 192.168.1.1. I modify my url like this

spring.datasource.url=jdbc:mysql://192.168.1.1:3306/pitstop?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=UTC

and try to start docker container with my app by command docker run -p 9001:9001 --network=bizon4ik --rm bizon4ik/mycontainer the result is the exception:

Caused by: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

I had tried to find another IP. I used ip addr show command and found the next record:

 3: wlp3s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 30:52:cb:db:8d:e0 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.102/24 brd 192.168.1.255 scope global dynamic noprefixroute wlp3s0

So based on it I took 192.168.1.102 and modify my url but the result is the same exception.

To be sure that founded IPs are correct I run new one container with pure ubuntu docker run -it --rm --network=bizon4ik ubuntu and checked the mentioned above IP:

root@268c4d544328:/# nmap -p 3306 192.168.1.1

Starting Nmap 7.60 ( https://nmap.org ) at 2019-07-28 17:28 UTC
Nmap scan report for 192.168.1.1
Host is up (0.053s latency).

PORT     STATE    SERVICE
3306/tcp filtered mysql
root@268c4d544328:/# nmap -p 3306 192.168.1.102   

Starting Nmap 7.60 ( https://nmap.org ) at 2019-07-28 15:56 UTC
Nmap scan report for my-host (192.168.1.102)
Host is up (0.000088s latency).

PORT     STATE  SERVICE
3306/tcp closed mysql

So looks fine, the ubuntu container can ping my DB. Do you have any ideas on why I cannot connect through the app to DB?

NB:

I checked in DB SHOW GRANTS; the result is GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION

I also checked /etc/mysql/my.cnf file. It has only these records:

!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/

I don't have ~/.my.cnf

Bizon4ik
  • 2,604
  • 4
  • 20
  • 46
  • Possible duplicate of [From inside of a Docker container, how do I connect to the localhost of the machine?](https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach) – David Maze Jul 28 '19 at 19:17
  • The `nmap` output tells that port `3306` is `closed` or `filtered` not `open`[docs](https://nmap.org/book/man-port-scanning-basics.html). Where did you run the `ip route show` command? Host or app container? – b0gusb Jul 29 '19 at 07:38
  • Do you run the DB on your host, or it is also in a docker container? If it is on a host without a container, why? If the DB was in a container you wouldn't have issues connecting. – Sasha Shpota Jul 29 '19 at 08:21
  • @b0gusb I used `route` on the host – Bizon4ik Jul 29 '19 at 08:28
  • @Bizon4ik run `ip` inside container. It gives you the IP of default gateway. This it typically the `docker0` network interface on a bridged network. You should be able to connect to this IP – b0gusb Jul 29 '19 at 08:31
  • @SashaShpota my DB in the host – Bizon4ik Jul 29 '19 at 08:31
  • @b0gusb I tried. I gave me some IP that starts on 72..... But the result is the same. – Bizon4ik Jul 29 '19 at 08:33

1 Answers1

0

The problem was in bind-address for MySQL. I looked it in the /etc/mysql/my.cnf however, the right place is /etc/mysql/mysql.conf.d/mysqld.cnf

Bizon4ik
  • 2,604
  • 4
  • 20
  • 46