Connect to specific instance using mongosh

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