使用Docker部署的MySQL和Java项目连接失败

问题描述 投票:0回答:1

我的所有部署都是在linux上的虚拟机上操作的。我的MySQL容器将端口3306映射到主机的端口3307,我的Java项目将端口8080映射到主机的8080端口,并且都工作成功。 但是,当我的Java项目需要使用MySQL连接数据库时,出现错误。


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

docker文件:

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"]

码头工人:

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

当我将 url 地址从 localhost 而不是 VM:3307 更改为 VM IP 地址时,可以正常连接到数据库并访问它。 这两个容器位于同一个桥(flora)上,并且我的java项目中的容器能够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

请帮我分析一下哪里出了问题。

mysql docker virtual-machine mysql-connector
1个回答
0
投票

端口需要更改为3306。 3307是映射到虚拟机的端口,docker容器之间通过原来的端口连接。 希望我的解释没有错。 问题解决了

© www.soinside.com 2019 - 2024. All rights reserved.