gpt4 book ai didi

java - 无法从另一个容器连接到docker中的Mysql服务器

转载 作者:行者123 更新时间:2023-11-29 15:16:58 25 4
gpt4 key购买 nike

我现在正在花费 6 个小时寻找解决方案。请帮我。首先我会尝试解释我的问题。我使用图像(在 dcoker 中)创建了 MySql 数据库,但是当我想创建另一个容器(Sprig Boot)并连接到数据库时,我不能。我尝试仅创建 MySql 的镜像并从 intellij 连接,一切都是正确的。仅当我尝试在容器中创建项目时才会出现问题...

我的 docer-compose

version: '3'
services:
api:
build: ./springboot-api-docker
ports:
- "9090:9090"
depends_on:
- database
database:
container_name: 'localhost'
image: mysql:latest
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_PASSWORD: password
MYSQL_USER: user
MYSQL_DATABASE: animaldb
ports:
- "3307:3306"
restart: always

应用程序属性

server.port = 9090

spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://localhost:3307/animaldb?autoReconnect=true&useSSL=false
spring.datasource.username=user
spring.datasource.password=password

和DocerFile

FROM openjdk:8u201-jdk-alpine3.9
ADD target/springboot-api-docker-0.0.1-SNAPSHOT.jar .
EXPOSE 9090
CMD java -jar springboot-api-docker-0.0.1-SNAPSHOT.jar

这很有趣,因为......首先我想将容器“localhost”的名称更改为另一个。但是当我改变这一点时,我什至无法从 intellij 运行 Spring Bot 应用程序。我在 docker 中创建了 MySql 的镜像,并尝试在 intellij 中连接。这是警告,但是当我想在 caontainer 中使用 SpringBoot 应用程序时,就会出现问题。我还没有找到适合我的解决方案。我将非常感谢您的每一次帮助。

这里是我的错误。

java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.

以及来自控制台的所有文本。也许你会更快地找到它。

Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] api_1 | at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:275) ~[hibernate-core-5.4.9.Final.jar!/:5.4.9.Final]

Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure api_1 | api_1 | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. api_1 |
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_201] api_1 | at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_201] api_1 | at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_201] api_1 | at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_201] api_1 | at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61) ~[mysql-connector-java-8.0.18.jar!/:8.0.18] api_1 | at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105) ~[mysql-connector-java-8.0.18.jar!/:8.0.18] api_1 | at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151) ~[mysql-connector-java-8.0.18.jar!/:8.0.18] api_1 | at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167) ~[mysql-connector-java-8.0.18.jar!/:8.0.18] api_1 | at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:91) ~[mysql-connector-java-8.0.18.jar!/:8.0.18] api_1 | at com.mysql.cj.NativeSession.connect(NativeSession.java:144) ~[mysql-connector-java-8.0.18.jar!/:8.0.18] api_1 | at com.mysql.cj.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:850) ~[mysql-connector-java-8.0.18.jar!/:8.0.18] api_1 | ... 58 common frames omitted api_1 | Caused by: java.net.ConnectException: Connection refused (Connection refused) api_1 | at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_201] api_1 | at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_201] api_1 | at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_201] api_1 | at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_201] api_1 | at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_201] api_1 | at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_201] api_1 | at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155) ~[mysql-connector-java-8.0.18.jar!/:8.0.18] api_1 | at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65) ~[mysql-connector-java-8.0.18.jar!/:8.0.18] api_1 | ... 60 common frames omitted

最佳答案

使用 localhost 作为 docker 容器名称并不好。

将 localhost 更改为数据库。

localhost 始终是您的本地计算机或本地 docker,而不是外部资源。

docker-compose

version: '3'
services:
api:
build: ./springboot-api-docker
ports:
- "9090:9090"
depends_on:
- database
links:
- database
database:
container_name: 'database'
image: mysql:latest
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_PASSWORD: password
MYSQL_USER: user
MYSQL_DATABASE: animaldb
ports:
- "3307:3306"
restart: always

application.properties:

server.port = 9090

spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://database: 3306/animaldb?autoReconnect=true&useSSL=false
spring.datasource.username=user
spring.datasource.password=password

关于java - 无法从另一个容器连接到docker中的Mysql服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59638202/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com