The mysql container in the Docker bridge network cannot connect

Springboot application visits database. Error:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any

enter the container where the SpringBoot application is located. Myapp-in-net can ping the mysql-test container:

 ping mysql-test
    PING mysql-test (172.18.0.4): 56 data bytes
    64 bytes from 172.18.0.4: seq=0 ttl=64 time=0.308 ms
    64 bytes from 172.18.0.4: seq=1 ttl=64 time=0.186 ms
    

application.properties of SpringBoot (I used a multi-data source configuration so I customized the variable name):

spring.datasource.test1.driverClassName = com.mysql.jdbc.Driver
spring.datasource.test1.url = jdbc:mysql://172.18.0.4:3306/test1?useUnicode=true&characterEncoding=utf-8
spring.datasource.test1.username = root
spring.datasource.test1.password = root

this is docker network information


$ sudo docker network inspect my-net

[
    {
        "Name": "my-net",
        "Id": "9351cf7ce0dc4b2f1d7f259ed6674eeda27997b375e113648e589d9ce35939a7",
        "Created": "2018-02-17T08:45:01.476631336Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.18.0.0/16",
                    "Gateway": "172.18.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "1bc6f9393c45f7aa405eb837d33a924b4310321cca855655247e657d1697c034": {
                "Name": "redis-in-net",
                "EndpointID": "af77c4b6d91698965605a42d2d8ad05e67d2671c1ff0a03a458630e5998203f3",
                "MacAddress": "02:42:ac:12:00:02",
                "IPv4Address": "172.18.0.2/16",
                "IPv6Address": ""
            },
            "39863741c672d21a715f54788bcde08d85239613b205563eb425c66894ab01a2": {
                "Name": "busybox2",
                "EndpointID": "34ead20723641dbd5067b82f1d2ea1f5eddccf84e7eb0272b2a622a3fd85edf2",
                "MacAddress": "02:42:ac:12:00:06",
                "IPv4Address": "172.18.0.6/16",
                "IPv6Address": ""
            },
            "8cb8107bcf1a456392971eac3a3926accfe55d1ed1a428fc6d33cd1ccdeec4f5": {
                "Name": "mysql-test",
                "EndpointID": "6458537bce07ce59dad7c46175865c80622bb27346455e0945c163731081b94c",
                "MacAddress": "02:42:ac:12:00:04",
                "IPv4Address": "172.18.0.4/16",
                "IPv6Address": ""
            },
            "d90df0352762b29ea9381003dd48ec230ae916f2e1507b20d1f79648fd131c52": {
                "Name": "myapp-in-net",
                "EndpointID": "594a039bc68d31acc11d0a6988bcc75c30c84a77aa5accfa759b1e2cc89aa203",
                "MacAddress": "02:42:ac:12:00:03",
                "IPv4Address": "172.18.0.3/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]


it is strange that there is no problem of connection failure when accessing the redis container in the same network. Configuration:

spring.redis.host=172.18.0.2
-sharp Redis
spring.redis.port=6379  


Feb.28,2021

telnet try to see if you can connect


if you use Link, then note that the port behind should be the port inside the container rather than the port exposed to the public network or host


the same question: I can't connect to mysql and Telnet, but the project can't be connected. What's the matter? Has the landlord solved


probably because the exposed port is different from the private network port. For example, 3307 docker 3306 is the docker internal network interface, 3307 is the interface for communicating with the host, and all docker containers are connected using 3306 interface

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1be57ae-30adb.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1be57ae-30adb.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?