docker 尝试访问Mysql服务器的Sping Boot 应用程序连接被拒绝

wi3ka0sx  于 2022-12-26  发布在  Docker
关注(0)|答案(2)|浏览(445)

我在Docker上配置了一个产品数据库:

productsdb:
container_name: productsdb 
volumes:
  - productsdb_data1:/var/lib/mysql
image: mysql:5.7
environment:
  MYSQL_ROOT_PASSWORD: "admin"
  MYSQL_PASSWORD: "admin"
networks:
  - commerce_net  
ports:
  - "3309:3306"

当我尝试从我的Sping Boot 项目连接到这个DB时,在服务器启动期间显示了以下异常:

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 java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[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.21.jar!/:8.0.21]
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
    at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
    at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:91) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
    at com.mysql.cj.NativeSession.connect(NativeSession.java:144) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
    at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:956) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:826) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
    ... 33 common frames omitted
Caused by: java.net.ConnectException: Connection refused (Connection refused)
    at java.base/java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:na]
    at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:412) ~[na:na]
    at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:255) ~[na:na]
    at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:237) ~[na:na]
    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:na]
    at java.base/java.net.Socket.connect(Socket.java:609) ~[na:na]
    at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
    at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
    ... 36 common frames omitted

application.properties被设置为以下列身份连接到主机“productsdb”:

spring.datasource.url=jdbc:mysql://productsdb:3309/products?createDatabaseIfNotExist=true&useSSL=false

尝试使用Docker compose和以下命令启动服务器:

docker run --network=commerce_net  -p 8085:8085 producttest -d

发生此异常的任何可能原因?

disho6za

disho6za1#

如果要在数据库所在的容器中启动应用程序,则必须使用容器端口,而不是使用数据库的主机端口。因此,应用程序中使用的数据库地址应为:

spring.datasource.url=jdbc:mysql://productsdb:3306/products?createDatabaseIfNotExist=true&useSSL=false

docker-compose.yml中的端口定义是以HOST:CONTAINER的形式进行的。这就是为什么您的第一个端口指的是您的主机端口,而第二个端口指的是您的容器端口。您可以从Docker文档访问详细信息。

j7dteeu8

j7dteeu82#

如果springboot也在相同的docker合成文件中,则使用MySQL端口3306
如果它在不同的Docker合成文件中,请将网络更新为--network={db_compose_file_directory_name}_commerce_netspring.datasource.url=jdbc:mysql://localhost:3309/products?createDatabaseIfNotExist=true&useSSL=false
尝试为服务提供Docker合成文件并完成Docker运行命令

相关问题