英文:
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
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的说法,你有一个拼写错误,我相信你还有另一个问题:
- 你转发的端口是27017:27107,其中容器端口是不正确的。请检查27107而不是27017。
- 看起来你连接的是localhost而不是mongo,因为我在你的代码中看到了localhost这个词组。
英文:
As @fly2matrix said you have a typo, and I believe you have another issue:
- you forward the ports 27017:27107, in which the container port is incorrect. Check 27107 instead of 27017
- It seems you're connecting to localhost instead of mongo as I see the phrase localhost in your code.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论