douhuan1979 2019-05-31 10:38 采纳率: 100%
浏览 399
已采纳

Docker和mongo-go-driver“服务器选择错误”

I had creating a MongoDB replica set using Docker and I can access using console or Robo3T and run any query.

The containers appear like this:

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                      NAMES
efe6ae03323d        mongo               "docker-entrypoint.s…"   10 minutes ago      Up 10 minutes       0.0.0.0:30001->27017/tcp   mongo1
57d2701c8a43        mongo               "docker-entrypoint.s…"   10 minutes ago      Up 10 minutes       0.0.0.0:30002->27017/tcp   mongo2
7553966b9ff5        mongo               "docker-entrypoint.s…"   10 minutes ago      Up 10 minutes       0.0.0.0:30003->27017/tcp   mongo3

The problem is an error when I try to ping using the mongo-go-driver (I tried with version 1.0.0 and 1.0.2)

// Create MongoDB client    
client, err := mongo.NewClient(options.Client().ApplyURI("mongodb://localhost:30001"))
if err != nil {
    t.Fatalf("Exit error: %v", err)
}

ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
err = client.Connect(ctx)
if err != nil {
    t.Fatalf("Exit error: %v", err)
}

ctx, cancel = context.WithTimeout(context.Background(), time.Minute)
defer cancel()

// Ping
err = client.Ping(ctx, readpref.Primary())
if err != nil {
    t.Fatalf("Exit error Ping: %v", err)
}

the error raised by the Ping call is the following:

Exit error Ping: server selection error: server selection timeout
        current topology: Type: ReplicaSetNoPrimary
        Servers:
        Addr: mongo2:27017, Type: Unknown, State: Connected, Average RTT: 0, Last error: dial tcp: lookup mongo2: no such host
        Addr: mongo3:27017, Type: Unknown, State: Connected, Average RTT: 0, Last error: dial tcp: lookup mongo3: no such host
        Addr: mongo1:27017, Type: Unknown, State: Connected, Average RTT: 0, Last error: dial tcp: lookup mongo1: no such host

Thank you all for the support!

  • 写回答

1条回答 默认 最新

  • dongzhou5344 2019-06-03 05:35
    关注

    This is due to unresolved hostname from Docker host. In Docker, the instances mongo1, mongo2, and mongo3 are reachable by those names. However, these names are not reachable from the Docker host. This is evident by this line:

    Addr: mongo2:27017, Type: Unknown, State: Connected, Average RTT: 0, Last error: dial tcp: lookup mongo2: no such host
    

    MongoDB driver will attempt server discovery from given a replica set member(s); it will find all of other nodes within the replica set (via rs.conf). The problem here is the replica set is set with name mongo<N>, the driver (run in Docker host) would not be able to resolve these names. You can confirm this by trying to ping mongo1 from Docker host.

    You can either try running the application from another Docker instance sharing the same Docker network as the replica set. Or, modify the Docker networking as such to allow resolvable hostnames.

    UPDATE:

    Regarding your comment on why using mongo shell, or PyMongo works.

    This is due to the difference in connection mode. When specifying a single node, i.e. mongodb://node1:27017 in shell or PyMongo, server discovery are not being made. Instead it will attempt to connect to that single node (not as part as a replica set). The catch is that you need to connect to the primary node of the replica set to write (you have to know which one). If you would like to connect as a replica set, you have to define the replica set name.

    In contrast to the mongo-go-driver, by default it would perform server discovery and attempt to connect as a replica set. If you would like to connect as a single node, then you need to specify connect=direct in the connection URI.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题