无法连接到在 Docker 容器中运行的 MongoDB。

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

Can't connect to MongoDB running in docker container

问题

我在docker容器中运行MongoDB,命令如下:

docker run --name mongo -d -p 27017:27107 mongo

使用docker ps命令检查,显示如下:

77f1a11295c3 mongo "docker-entrypoint.s…" 20 minutes ago Up 20 minutes 27017/tcp, 0.0.0.0:27017->27107/tcp mongo

所以它正在正确映射端口运行。

当我尝试通过Intellij设置连接时,出现以下错误:

com.mongodb.MongoTimeoutException: Timed out after 10000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketReadException: Exception receiving message}, caused by {java.net.SocketException: Connection reset}}].

当我尝试从golang web服务器连接时,出现以下错误并关闭了web服务器:

server selection error: server selection timeout, current topology: { Type: Unknown, Servers: [{ Addr: localhost:27017, Type: Unknown, Average RTT: 0, Last error: connection() error occured during connection handshake: connection(localhost:27017[-64]) incomplete read of message header: read tcp 127.0.0.1:40700->127.0.0.1:27017: read: connection reset by peer }, ] }

这是MongoDB docker镜像的一个错误吗?还是我需要做其他操作?

英文:

I run MongoDB in a docker container like this

docker run --name mongo -d -p 27017:27107 mongo

Check with docker ps shows

77f1a11295c3 mongo "docker-entrypoint.s…" 20 minutes ago Up 20 minutes 27017/tcp, 0.0.0.0:27017->27107/tcp mongo

so it's running with the port mapped correctly.

When I try to setup a connection via Intellij

无法连接到在 Docker 容器中运行的 MongoDB。

it fails with

com.mongodb.MongoTimeoutException: Timed out after 10000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketReadException: Exception receiving message}, caused by {java.net.SocketException: Connection reset}}].

When I try to connect to connect from a golang web server

clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")
client, err := mongo.Connect(context.TODO(), clientOptions)

I get this error and it shuts down the web server:

server selection error: server selection timeout, current topology: { Type: Unknown, Servers: [{ Addr: localhost:27017, Type: Unknown, Average RTT: 0, Last error: connection() error occured during connection handshake: connection(localhost:27017[-64]) incomplete read of message header: read tcp 127.0.0.1:40700->127.0.0.1:27017: read: connection reset by peer }, ] }

Is this a bug in the MongoDB docker image, or is there something else I need to do?

答案1

得分: 0

我认为在创建容器时你有一个拼写错误。27017 and 27107

docker run --name mongo -d -p 27017:27017 mongo

这在docker ps命令中清楚可见。

你可以尝试一下,看看是否解决了问题?

英文:

I think you have a typo mistake while creating a container. 27017 and 27107

docker run --name mongo -d -p 27017:27017 mongo

This is clearly visible in docker ps command.

Can you try and see if it solves the problem ?

答案2

得分: 0

根据@fly2matrix的说法,你有一个拼写错误,我相信你还有另一个问题:

  1. 你转发的端口是27017:27107,其中容器端口是不正确的。请检查27107而不是27017
  2. 看起来你连接的是localhost而不是mongo,因为我在你的代码中看到了localhost这个词组。
英文:

As @fly2matrix said you have a typo, and I believe you have another issue:

  1. you forward the ports 27017:27107, in which the container port is incorrect. Check 27107 instead of 27017
  2. It seems you're connecting to localhost instead of mongo as I see the phrase localhost in your code.

huangapple
  • 本文由 发表于 2021年6月1日 00:23:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/67777308.html
匿名

发表评论

匿名网友

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

确定