Go driver, can't connect to an uninitialized replica set

Hi all, wondering if anybody can help diagnose an issue connecting to a replica set using the go driver.

Environment
A replica set running as a pod in k8s cluster on port 27017 with replica set name and --bind_ip_all option. The mongo images are mongo:4.4.5 and the go driver is v1.5.1.

Issue
When I try to execute client.Ping or run “replSetGetStatus” command by db.RunCommand in my go program, the program will return "server selection error: server selection timeout, current topology: { Type: ReplicaSetNoPrimary, Servers: [{ Addr: 10.244.12.108:27017, Type: RSGhost, Average RTT: 1407516 }, ] }”. But I can connect to the replica set using mongo client and run command,such as rs.status(). The URI used in my program and mongo client are same as "mongodb://serviceip:27017/.

Thanks

Hi @yi_liu,

Per the Server Discovery and Monitoring specification, an uninitialized member of a replica set is represented via the RSGhost server type. Such servers are not considered selectable for operations, so attempting to execute a command will fail with a server selection error.

To work around this issue in the Go Driver, you can specify that you would like to create a direct connection to the node by either appending directConnection=true to the URI or setting the Direct option in ClientOptions:

uri := "mongodb://user:password@host/?directConnection=true"
client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))

// OR

uri := "mongodb://user:password@host"
opts := options.Client().ApplyURI(uri).SetDirect(true)
client, err := mongo.Connect(ctx, opts)

Note that this will connect only to that specific node, so if there are other members in the replica set, they will not be targetted for operations.

– Divjot

2 Likes

Hi @Divjot, thanks for your reply.

Direct option fix server selection error,but there are some other errors. If I using ‘localhost’ or ‘127.0.0.1’ as host in URI, the error is ‘(NotYetInitialized) no replset config has been receivedresult’. If I using network card ip address, the error is ‘(NotPrimaryOrSecondary) node is not in primary or recovering state’.

@yi_liu Based on the error messages, it seems that an uninitialized replica set member cannot be used to execute commands like ping and replsetGetStatus. You may have to run the replSetInitiate command to actually initialize the node.

– Divjot

1 Like