skip to Main Content

All my deployments are operated on virtual machines on linux.My MySQL container maps port 3306 to port 3307 of the host, and my Java project maps port 8080 to port 8080 of the host, and both work successfully.
However, when my Java project needs to use MySQL to connect to the database, I get an error.


10:07:27:498 ERROR 1 --- [reate-375097969] com.alibaba.druid.pool.DruidDataSource   : create connection SQLException, url: jdbc:mysql://mysql:3307/fakenews?useUnicode=true&characterE                        ncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai, errorCode 0, state 08001

java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:110) ~[mysql-connector-java-8.0.30.jar!/:8.0.30]
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) ~[mysql-connector-java-8.0.30.jar!/:8.0.30]
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89) ~[mysql-connector-java-8.0.30.jar!/:8.0.30]
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63) ~[mysql-connector-java-8.0.30.jar!/:8.0.30]
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73) ~[mysql-connector-java-8.0.30.jar!/:8.0.30]
        at com.mysql.cj.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:898) ~[mysql-connector-java-8.0.30.jar!/:8.0.30]
        at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:823) ~[mysql-connector-java-8.0.30.jar!/:8.0.30]
        at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:448) ~[mysql-connector-java-8.0.30.jar!/:8.0.30]
        at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:241) ~[mysql-connector-java-8.0.30.jar!/:8.0.30]
        at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198) ~[mysql-connector-java-8.0.30.jar!/:8.0.30]
        at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1657) ~[druid-1.2.6.jar!/:1.2.6]
        at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1723) ~[druid-1.2.6.jar!/:1.2.6]
        at com.alibaba.druid.pool.DruidDataSource$CreateConnectionThread.run(DruidDataSource.java:2801) ~[druid-1.2.6.jar!/:1.2.6]
Caused by: com.mysql.cj.exceptions.CJCommunicationsException: 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.
        at jdk.internal.reflect.GeneratedConstructorAccessor33.newInstance(Unknown Source) ~[na:na]
        at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:na]
        at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490) ~[na:na]
        at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61) ~[mysql-connector-java-8.0.30.jar!/:8.0.30]
        at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105) ~[mysql-connector-java-8.0.30.jar!/:8.0.30]
        at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151) ~[mysql-connector-java-8.0.30.jar!/:8.0.30]
        at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167) ~[mysql-connector-java-8.0.30.jar!/:8.0.30]
        at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:89) ~[mysql-connector-java-8.0.30.jar!/:8.0.30]
        at com.mysql.cj.NativeSession.connect(NativeSession.java:120) ~[mysql-connector-java-8.0.30.jar!/:8.0.30]
        at com.mysql.cj.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:842) ~[mysql-connector-java-8.0.30.jar!/:8.0.30]
        ... 7 common frames omitted
Caused by: java.net.UnknownHostException: mysql
        at java.base/java.net.InetAddress$CachedAddresses.get(InetAddress.java:797) ~[na:na]
        at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1509) ~[na:na]
        at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1368) ~[na:na]
        at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1302) ~[na:na]
        at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:130) ~[mysql-connector-java-8.0.30.jar!/:8.0.30]
        at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:63) ~[mysql-connector-java-8.0.30.jar!/:8.0.30]
        ... 9 common frames omitted

dockerfile:

FROM openjdk:11.0
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
COPY TextAnalyse.jar /app.jar
ENTRYPOINT ["java", "-jar", "/app.jar"]

docker ps:

CONTAINER ID   IMAGE         PORTS                                                  STATUS             NAMES
5bcdd5eff02f   textanalyse   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp              Up 24 minutes      ta
3f44be91fb18   mysql         33060/tcp, 0.0.0.0:3307->3306/tcp, :::3307->3306/tcp   Up About an hour   mysql
584173fb038f   nginx         0.0.0.0:80->80/tcp, :::80->80/tcp                      Up 44 minutes      nginx

When I change the url address to the VM IP address from localhost instead of VM: 3307 is able to connect to the database and access it normally.
The two containers are on the same bridge(flora), and the container in my java project is able to ping mysql.

root@1fce4b851205:/# ping mysql
PING mysql (172.18.0.2) 56(84) bytes of data.
64 bytes from mysql.flora (172.18.0.2): icmp_seq=1 ttl=64 time=3.09 ms
64 bytes from mysql.flora (172.18.0.2): icmp_seq=2 ttl=64 time=0.069 ms
64 bytes from mysql.flora (172.18.0.2): icmp_seq=3 ttl=64 time=0.106 ms
64 bytes from mysql.flora (172.18.0.2): icmp_seq=4 ttl=64 time=1.34 ms
64 bytes from mysql.flora (172.18.0.2): icmp_seq=5 ttl=64 time=0.093 ms
64 bytes from mysql.flora (172.18.0.2): icmp_seq=6 ttl=64 time=0.168 ms
64 bytes from mysql.flora (172.18.0.2): icmp_seq=7 ttl=64 time=0.072 ms

Please help me analyze what went wrong.

2

Answers


  1. Chosen as BEST ANSWER

    The port need to change to 3306. 3307 is a port mapped to a virtual machine, and docker containers are connected to each other through the original port. Hopefully my explanation is not wrong. The problem is solved


  2. You need to connect to DB from Java with IP:3307 or mysql:3306

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search