Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect to Mongo Atlas Secondary

On Cloud Mongo (Mongo Atlas) Free tier, it has 3 members of servers. How can I connect to the Secondary host from Mongo shell? Their example only shows how to connect to Primary.

"members" : [
        {
            "_id" : 0,
            "name" : "***-shard-00-00-***.mongodb.net:27017",
            ....
        },
        {
            "_id" : 1,
            "name" : "***-shard-00-01-***.mongodb.net:27017",
            .....
        },
        {
            "_id" : 2,
            "name" : "***-shard-00-02-***.mongodb.net:27017",
            .....
        }
    ]
like image 882
angelokh Avatar asked Nov 23 '25 13:11

angelokh


2 Answers

You need to use the --ssl flag and specify authSource.

Try:

mongo "mongodb://***-shard-00-02-***.mongodb.net:27017/?authSource=admin" --ssl

at the very minimum. Of course you can add options for username, password, database to connect, etc...

mongo "mongodb://<username>:<password>@***-shard-00-02-***.mongodb.net:27017/<database>?authSource=admin" --ssl

I hope this helps.

like image 162
t-brito Avatar answered Nov 28 '25 16:11

t-brito


Another way to connect to secondary through mongosh

Here are the steps:

  • Retrieve the hostname from the Atlas cluster by clicking View Monitoring on the cluster

  • Copy the secondary node hostname without the port number. For example: clustername-shard-00-01.abcde.mongodb.net

  • To connect to the node, run the following mongosh command from the command line, replacing <secondary hostname> with the secondary hostname you copied in the previous step, and replacing <database user> with the database user created for the project:

mongosh "mongodb://<secondary hostname>/myFirstDatabase" --tls --authenticationDatabase admin --username <database user>

like image 32
zangw Avatar answered Nov 28 '25 15:11

zangw



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!