Connect to specific instance using mongosh

I have a Atlas M10 replica cluster with 3 nodes. I can connect to it successfully using the standard “+srv” connection string. However, I now need to connect to each replica node separately using mongosh. (I need to do this so I can run the ‘compact’ command on each node separately)

Is this possible? How do I find the host names of each of the nodes so I can pass it to mongosh connection string? I assume when connecting to direct nodes I should not specify the “+srv” in connection string. So far I have tried to using the “…shard-00-01…” domain names that I see in Atlas UI as the host.
Example I have tried (in Linux):
mongosh 'mongodb://<username>:<password>@<...shard-00-01 domain...>/dbname'

I get MongoNetworkError: getaddrinfo ENOTFOUND

Hi @Ravin_Perera

As not to repeat myself have a look at the DNS queries in this post to query for the required hosts and connection options.

All atlas connections use TLS. This is implicit when using a mongodb+srv uri but when using a mongodb:// the tls=true option will need to be specified.

mongosh will connect to the replicaset primary when multiple hosts or the replicaSet option is provided. To connect to a specific host exclude the replicaSet option or use the directConnection option.

So taking the TXT parameters, one host from the replicaset, and adding tls and direct connection options:

mongosh --quiet 'mongodb://username:password@cluster0-shard-00-02.9qfvh.mongodb.net/?authSource=admin&replicaSet=Cluster0-shard-0&tls=true&directConnection=true' --eval 'db.hello().me'
cluster0-shard-00-02.9qfvh.mongodb.net:27017
4 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.