Docker和mongo-go-driver“服务器选择错误" [英] Docker and mongo-go-driver "server selection error"

查看:194
本文介绍了Docker和mongo-go-driver“服务器选择错误"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Docker创建了MongoDB副本集,并且可以使用控制台或Robo3T进行访问并运行任何查询.

容器如下所示:

$ 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

当我尝试使用 mongo-go-driver

时,问题是一个错误a>(我尝试使用1.0.0和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)
}

Ping调用引发的错误如下:

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

谢谢大家的支持!

解决方案

这是由于Docker主机中未解析的hostname所致.在Docker中,实例mongo1mongo2mongo3可以通过这些名称访问.但是,无法从Docker主机访问这些名称.这行很明显:

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

MongoDB驱动程序将从给定的副本集成员尝试server discovery;它将找到副本集中的所有其他节点(通过 rs.conf ).这里的问题是副本集设置为名称mongo<N>,驱动程序(在Docker主机中运行)将无法解析这些名称.您可以通过尝试从Docker主机ping mongo1来确认这一点.

您可以尝试从另一个与副本集共享同一Docker网络的Docker实例运行该应用程序.或者,这样修改Docker网络以允许使用可解析的主机名.

更新:

关于您为什么使用 mongo shell或直接连接示例

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!

解决方案

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. See also Example Connect Direct

这篇关于Docker和mongo-go-driver“服务器选择错误"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆