我使用 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进行 ping 时出现错误(我尝试使用版本 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)
}
错误如下:
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
杨__羊羊
相关分类