doulei6330
2018-12-21 16:04
浏览 284

Docker出现“服务器选择超时错误” MongoDB Go驱动程序

I'm working on a very basic (I thought) starter program in Go using MongoDB and Docker. Trying to get a handle on these before we start using them at work. I've got my MongoDB running in a docker container, just using my local host, using the official Docker image. This is running fine, I can connect to it through MongoDB Compass and modify the DB.

My next task was to build a separate Docker container that is able to read and write to the DB. I'm using MongoDB-Go-Driver (https://godoc.org/github.com/mongodb/mongo-go-driver/mongo) for this as mgo is no longer kept up.

This is my code, I'm just following the numerous tutorials online to make a simple connection and then ping the DB to ensure connectivity.

client, err := mongo.Connect("mongodb://localhost:27017")

if err != nil {
    log.Fatal("error ", err)
}

// Check the connection
err = client.Ping(context.TODO(), nil)

if err != nil {
    log.Fatal("error2 ", err)
}

fmt.Println("Connected to MongoDB!")

It always fails on doing any operation on the DB (Find, FindOne, Ping, etc.) with error2 server selection timeout

This is my docker-compose file I'm running.

version: "3"

services:
  datastore:
    image: mongo
    ports: 
      - "27017:27017"
    networks: 
      - maccaptionNet
    volumes: 
      - .:/go/src/maccaption_microservice/dbdata
  jobservice:
    image: jobservicemaccaption:1.0
    networks:
      - maccaptionNet
    depends_on:
      - "datastore"


networks: 
  maccaptionNet:
    driver: bridge

I'm brand new to MongoDB and after hours of research haven't made any progress on this. I've read through https://docs.mongodb.com/manual/core/read-preference-mechanics/ https://docs.mongodb.com/manual/replication/

Can anyone point me in the right direction for this? I haven't been able to find a lot on this specific issue.

Thanks!

图片转代码服务由CSDN问答提供 功能建议

我正在使用MongoDB和Docker开发Go中一个非常基本的(我认为)启动程序。 在我们开始在工作中使用它们之前,先设法弄清这些问题。 我已经将MongoDB运行在D​​ocker容器中,仅使用本地主机并使用了官方Docker映像。 运行正常,我可以通过MongoDB Compass连接到它并修改数据库。

我的下一个任务是构建一个单独的Docker容器,该容器能够读取和写入数据库。 我正在使用MongoDB-Go-Driver( https://godoc.org /github.com/mongodb/mongo-go-driver/mongo ),因为mgo不再跟上。

这是我的代码,我只是在线上阅读众多教程以进行简单连接,然后对数据库进行ping操作以确保连接。

 客户端,错误:= mongo.Connect(“ mongodb:// localhost:27017”)
 
if错误!= nil {
 log.Fatal(“ error  “,err)
} 
 
 //检查连接
err = client.Ping(context.TODO(),nil)
 
if err!= nil {
 log.Fatal(” error2“,  err)
} 
 
fmt.Println(“ Connected to MongoDB!”)
   
 
 

在数据库上执行任何操作总是失败(Find,FindOne ,Ping等),并且 error2服务器选择超时

这是我正在运行的docker-compose文件。

 版本:“ 3” 
 
服务:
数据存储区:
图片:mongo 
端口:
-“ 27017:27017” 
网络:\  n-maccaptionNet 
卷:
-。:/ go / src / maccaption_microservice / dbdata 
 jobservice:
映像:jobservicemaccaption:1.0 
网络:
-maccaptionNet 
depends_on:
-“数据存储” \  n 
 
网络:
 maccaptionNet:
驱动程序:网桥
   
 
 

我是MongoDB的新手,经过数小时的研究并没有取得任何进展 我已通读 https://docs.mongodb.com / manual / core / read-preference-mechanics / https://docs.mongodb .com / manual / replication /

有人可以为此指出正确的方向吗? 在这个特定问题上,我找不到很多东西。

谢谢!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douzhang2092 2018-12-21 16:31
    已采纳

    When you running the service and mongodb in docker you can't use localhost since the service is in a different container than mongodb, and from docker point of view it's under a different ip address.

    You can connect with the service name you specify in docker-compose datastore

    mongo.Connect("mongodb://datastore:27017")
    

    Edit:

    from: https://docs.docker.com/compose/networking/

    By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name

    Meaning that if you run multiple containers via compose, you can access one container from the other by the container name,

    Basically when docker-compose starts, it sets up the network, and each container in the compose joins the network under its container name. For a container's point if view, localhost is just the container itself, while he can search for other container's name and get back the container’s IP address.

    Assuming that the docker is running on your localhost, you can set the name in etc/hosts file like this:

    127.0.0.1 datastore
    

    (if not just replace 127.0.0.1 with the docker ip)

    And in the app you will connect with mongodb://datastore:27017

    So you will be able to run the service both in the docker and from outside, if you'll decide to run only the db in docker

    docker-compose start datastore
    
    点赞 打赏 评论
  • dtzhfb4869 2018-12-21 16:31

    If you are connecting to one docker from another (like it is written in your docker-compose file, and using bridge network mode, you have to change your localhost to the hostname, like datastore

    client, err := mongo.Connect("mongodb://datastore:27017")
    

    When your go script uses localhost, it expects the database to located in the same docker

    点赞 打赏 评论
  • doujia1939 2019-04-20 19:13

    Somehow i've fix this problem in a different way: by changing ports from "27018:27017" to "27017:27017". IDK why this helps. Maybe if Mongo sees not default port it thinks there are cluster of Mongo's nodes.

    点赞 打赏 评论

相关推荐 更多相似问题