连接被拒绝,尝试连接到 MongoDB Docker 实例。

huangapple go评论74阅读模式
英文:

Connection refused trying to connect to mongoDB docker instance

问题

我正在尝试部署一个带有 MongoDB 和 JPA 的简单应用程序,但无法使其正常工作。

当我将应用程序部署在 Docker 容器中时,我一直在收到以下错误消息:

2020-10-05 18:47:39.770 INFO 1 --- [localhost:27017] org.mongodb.driver.cluster : Exception in monitor thread while connecting to server localhost:27017

com.mongodb.MongoSocketOpenException: Exception opening socket
    at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:70) ~[mongodb-driver-core-4.0.5.jar!/:na]
    at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:127) ~[mongodb-driver-core-4.0.5.jar!/:na]
    at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117) ~[mongodb-driver-core-4.0.5.jar!/:na]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_212]
Caused by: java.net.ConnectException: Connection refused (Connection refused)
    at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_212]
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_212]
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_212]
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_212]
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_212]
    at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_212]
    at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:63) ~[mongodb-driver-core-4.0.5.jar!/:na]
    at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:79) ~[mongodb-driver-core-4.0.5.jar!/:na]
    at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65) ~[mongodb-driver-core-4.0.5.jar!/:na]
    ... 3 common frames omitted

这是我用于构建镜像的 Dockerfile:

FROM openjdk:8-jdk-alpine
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

我用于运行 MongoDB 的 docker-compose.yml 文件如下:

version: '3.1'

services:

  mongo:
    image: mongo
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example

  mongo-express:
    image: mongo-express
    restart: always
    ports:
      - 8081:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: example

我的所有实体都遵循以下模式(其中 <Name> 为实体的名称):

@Document
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class &lt;Name&gt;Entity {
    @Id
    private Integer &lt;Name&gt;Id;
    // 更多数据
}

我的 JPA 存储库(与上述相同,其中 <Name> 为 JPA 类和实体的名称):

public interface &lt;Name&gt;JPA extends MongoRepository&lt;&lt;Name&gt;Entity, Integer&gt;{
}

最后,在 application.properties 文件中,我定义了以下属性:

spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=api-database
spring.data.mongodb.username=root
spring.data.mongodb.password=example
spring.data.mongodb.repositories.enabled=true

我可以连接到 localhost:8081 查看 mongo-express 界面,但除此之外,我没有看到其他任何信息。

英文:

I'm trying to deploy a simple app with a MongoDB with JPA, but I can't make it work right.

When I deploy the app in a docker container, I keep getting the following error:

2020-10-05 18:47:39.770 INFO 1 --- [localhost:27017] org.mongodb.driver.cluster : Exception in monitor thread while connecting to server localhost:27017

com.mongodb.MongoSocketOpenException: Exception opening socket
    at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:70) ~[mongodb-driver-core-4.0.5.jar!/:na]
    at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:127) ~[mongodb-driver-core-4.0.5.jar!/:na]
    at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117) ~[mongodb-driver-core-4.0.5.jar!/:na]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_212]
Caused by: java.net.ConnectException: Connection refused (Connection refused)
    at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_212]
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_212]
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_212]
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_212]
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_212]
    at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_212]
    at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:63) ~[mongodb-driver-core-4.0.5.jar!/:na]
    at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:79) ~[mongodb-driver-core-4.0.5.jar!/:na]
    at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65) ~[mongodb-driver-core-4.0.5.jar!/:na]
    ... 3 common frames omitted

This is the dockerfile I use to build the image:

FROM openjdk:8-jdk-alpine
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT [&quot;java&quot;,&quot;-jar&quot;,&quot;/app.jar&quot;]

And the docker-compose.yml I used to run mongoDB:

# Use root/example as user/password credentials
version: &#39;3.1&#39;

services:

  mongo:
    image: mongo
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example

  mongo-express:
    image: mongo-express
    restart: always
    ports:
      - 8081:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: example

My entities all follow the following pattern (being <Name> the name of the entity):

@Document
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class &lt;Name&gt;Entity {
    @Id
    private Integer &lt;Name&gt;Id;
    // More data
}

And my JPA Repositories (as above, being <Name> the name of the jpa class & entity):

public interface &lt;Name&gt;JPA extends MongoRepository&lt;&lt;Name&gt;Entity, Integer&gt;{
}

And finally, I have defined the following properties under application.properties:

spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=api-database
spring.data.mongodb.username=root
spring.data.mongodb.password=example
spring.data.mongodb.repositories.enabled=true

I am able to connect to localhost:8081 to see the mongo-express interface, but appart from that, I don't see anything.

答案1

得分: 1

无法通过环回地址进行连接,因为 MongoDB 和应用位于不同的容器中。它们就像不同的机器,对于它们各自来说,localhost 不是主机,而是容器本身。请改用服务名称(来自 docker-compose.yml):

spring.data.mongodb.host=mongo:27017
英文:

They can't connect via loopback address because mongodb and the app are in different containers. They're like like different machines and localhost for each of them is not the host, but the container itself. Use service name (from docker-compose.yml) instead:

spring.data.mongodb.host=mongo:27017

答案2

得分: 1

如果您试图通过本地主机与容器化的MongoDB Docker实例连接,那么您可能会遇到此问题。

解决方案:

mongodb://localhost:27017

替换为使用特殊DNS名称host.docker.internal连接主机:

mongodb://host.docker.internal:27017

localhost替换为host.docker.internal即可解决问题。

英文:

If you are trying to connect with containerized MongoDB docker instance with localhost then you might face this issue.

Solution :

In place of

mongodb://localhost:27017

To connect the host out of the box by using the special DNS name: host.docker.internal

mongodb://host.docker.internal:27017

Replacing localhost with host.docker.internal will do the trick.

huangapple
  • 本文由 发表于 2020年10月6日 02:50:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/64214503.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定