Mongosh: cannot connect to selected node

Hi

When I connect to my cluster like this everything is fine:

mongosh "mongodb+srv://<USER>:<PASS>@<CLUSTER-NAME>.a6uds.mongodb.net"
Current Mongosh Log ID: 61124b0978c007112d0cd89a
Connecting to:          mongodb+srv://<credentials>@<CLUSTER-NAME>.a6uds.mongodb.net/
Using MongoDB:          4.4.8
Using Mongosh:          1.0.1

but when I try to connect to selected secondary node it cannot connect, any ideas way?

$ mongosh "mongodb://<SECONDARY-NAME>.a6uds.mongodb.net:27017" --username <USER>--password <PASSS> --authenticationDa
tabase flightmgmt
Current Mongosh Log ID: 61124bf3d39dc6552a363b1a
Connecting to:          mongodb://<NAME>.a6uds.mongodb.net:27017/?directConnection=true
MongoServerSelectionError: connection <monitor> to 3.120.95.31:27017 closed

And my public IP address is added to the white list in network access page.

Any ideas why I cannot connect to the secondary node?

-Jacek

For M103 you should be using the provided IDE.

As for the command that gives:

most likely the authentication database is wrong. See https://docs.atlas.mongodb.com/security-add-mongodb-users/

2 Likes

User I am using has atlasAdmin@admin role and it should be enough to connect to a cluster node.
I also get the same error in provided IDE MongoDB Compass.
So I think the problem is somewhere else.

hm or maybe something is not right with the permissions because also I get an error here:

Atlas atlas-mritki-shard-0 [primary] local> rs.printSecondaryReplicationInfo()
MongoServerError: namespace not found
Atlas atlas-mritki-shard-0 [primary] local> db.printSecondaryReplicationInfo()
MongoServerError: namespace not found

Did you try with authenticationDatabase as admin?

I think that is what steevej was hinting

Even if I use this option I cannot connect to a secondary

$ mongosh “mongodb://.a6uds.mongodb.net:27017” --username --password --authenticationDatabase admin
Current Mongosh Log ID: 6113618fe4c21c139ee4c4d0
Connecting to: mongodb://.a6uds.mongodb.net:27017/?directConnection=true
MongoServerSelectionError: connection to :27017 closed

And if I sign in to my cluster then still cannot get replication info

$ mongosh “mongodb+srv://:@<CLUSTER_NAME>.a6uds.mongodb.net” --authenticationDatabase admin
Current Mongosh Log ID: 6113628f966924a2373798e0
Connecting to: mongodb+srv://@<CLUSTER_NAME>.a6uds.mongodb.net/
Using MongoDB: 4.4.8
Using Mongosh: 1.0.1

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

Atlas atlas-mritki-shard-0 [primary] test> rs.printSecondaryReplicationInfo()
MongoServerError: namespace not found
Atlas atlas-mritki-shard-0 [primary] test> db.printSecondaryReplicationInfo()
MongoServerError: namespace not found
Atlas atlas-mritki-shard-0 [primary] test>

There was a typo, it should be:

Even if I use this option I cannot connect to a secondary

$ mongosh “mongodb://<SECONDARY_NAME>.a6uds.mongodb.net:27017” --username --password --authenticationDatabase admin
Current Mongosh Log ID: 6113618fe4c21c139ee4c4d0
Connecting to: mongodb://<SECONDARY_NAME>.a6uds.mongodb.net:27017/?directConnection=true
MongoServerSelectionError: connection to :27017 closed

And if I sign in to my cluster then still cannot get replication info

$ mongosh “mongodb+srv://:@<CLUSTER_NAME>.a6uds.mongodb.net” --authenticationDatabase admin
Current Mongosh Log ID: 6113628f966924a2373798e0
Connecting to: mongodb+srv://@<CLUSTER_NAME>.a6uds.mongodb.net/
Using MongoDB: 4.4.8
Using Mongosh: 1.0.1

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

Atlas atlas-mritki-shard-0 [primary] test> rs.printSecondaryReplicationInfo()
MongoServerError: namespace not found
Atlas atlas-mritki-shard-0 [primary] test> db.printSecondaryReplicationInfo()
MongoServerError: namespace not found
Atlas atlas-mritki-shard-0 [primary] test>

Your secondary server may be wrong

On primary try these commands
db.printReplicationInfo()
db.printSlaveReplicationInfo()–This is deprecated in new versions but just try it

db.printReplicationInfo() works fine but db.printSecondaryReplicationInfo() fails but it should work because in docs it is said that it is supported: https://docs.mongodb.com/manual/reference/method/db.printSecondaryReplicationInfo/#mongodb-method-db.printSecondaryReplicationInfo

Atlas atlas-mritki-shard-0 [primary] test> db.printSecondaryReplicationInfo()
MongoServerError: namespace not found

So it looks nothing is working if data has to be retrieved from the secondaries. I have no clue way.
In https://cloud.mongodb.com/ I see that both secondary nodes are running.

Maybe this problem occurs because I am using free tier?

Did you try this?
If yes what is the output or error you get

This is what I get:

Atlas atlas-mritki-shard-0 [primary] local> db.printSlaveReplicationInfo()
MongoshDeprecatedError: [COMMON-10003] Method deprecated, use db.printSecondaryReplicationInfo instead
Atlas atlas-mritki-shard-0 [primary] local> db.printSecondaryReplicationInfo()
MongoServerError: namespace not found
Atlas atlas-mritki-shard-0 [primary] local>

First fix your secondary connect string and try if it works there (use long form of string remove replicasetname from it.It will put you in secondary)
In may case slightly older version of DB so db.printSlaveReplicationInfo() working fine

May be some system commands restricted in new version.Check M0 restrictions

I do not understand how I should change connection string to secondary, it looks like this:

mongosh “mongodb://.a6uds.mongodb.net:27017” --username –password --authenticationDa
tabase flightmgmt

how I should correct it?

I also checked M0 limitation and there is nothing about blocking connections for secondary nodes

I can connect like this “mongodb+srv://USR:PASS@CLUSTER-NAME.a6uds.mongodb.net”
but then I am in primary and I do not know how to switch to secondary.

Unless we see what is the value you are putting in cannot say why it is failing
I tried different ways.I can connect to secondary of my cluster

If you are not sure of secondary server name from Atlas choose i have already shell installed then choose shell version <3.4
It will give you long form of string
Remove replicaset portion from the connect string and try.It will connect to secondary

Also i tried replication command on 4.4.8 version DB but failing

MongoDB Enterprise :PRIMARY> db.printSecondaryReplicationInfo()
2021-08-12T20:22:23.624+0530 E QUERY [js] TypeError: db.printSecondaryReplicationInfo is not a function :

If I am not mistaken, Ramachandra_Tummala you are in the mongo shell since your prompt is:

while Jacek_Kowalski, your are in mongosh shell from your prompt:

I think printSecondaryReplicationInfo is mongosh only. I do not know why you get namespace not found but I would try rs.status() which is supposed to work in both mongo and mongosh.

Yes steevej.I was using mongo shell

I am getting this on 4.4.8 with mongo.

MongoDB Enterprise:PRIMARY> db.printSlaveReplicationInfo()
2021-08-12T20:22:58.429+0530 E QUERY [js] Error: count failed: {
“ok” : 0,
“errmsg” : “namespace not found”,
“code” : 8000,
“codeName” : “AtlasError”

These are the calls with DNS names, first is ok when I connect to the cluster, second is failing when I try to connect to a secondary and the last is when I try to connect directly to primary. So it looks that I cannot directly connect to any node and I can connect only to cluster. I know DNS names of the nodes from https://cloud.mongodb.com/

C:>mongosh “mongodb+srv://kicaj:qwer1@myfirstcluster.a6uds.mongodb.net”
Current Mongosh Log ID: 611610bc26491d4337305d5a
Connecting to: mongodb+srv://@myfirstcluster.a6uds.mongodb.net/
Using MongoDB: 4.4.8
Using Mongosh: 1.0.1

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

Atlas atlas-mritki-shard-0 [primary] test> exit

C:>mongosh “mongodb://myfirstcluster-shard-00-00.a6uds.mongodb.net:27017” --username kicaj --password qwer1 --authenticationDatabase admin
Current Mongosh Log ID: 611610c5e8af9f78279d2c04
Connecting to: mongodb://myfirstcluster-shard-00-00.a6uds.mongodb.net:27017/?directConnection=true
MongoServerSelectionError: connection to 3.120.94.59:27017 closed

C:>mongosh “mongodb://myfirstcluster-shard-00-01.a6uds.mongodb.net:27017” --username kicaj --password qwer1 --authenticationDatabase admin
Current Mongosh Log ID: 6116114da94043d70d5e8055
Connecting to: mongodb://myfirstcluster-shard-00-01.a6uds.mongodb.net:27017/?directConnection=true
MongoServerSelectionError: connection to 3.120.95.31:27017 closed

Is your cluster up?
Can you connect with SRV string you have shown?

2021-08-13T22:11:52.998+0530 I NETWORK [js] Cannot reach any nodes for set atlas-mritki-shard-0. Please check network c
onnectivity and the status of the set. This has happened for 3 checks in a row.
2021-08-13T22:11:53.000+0530 E QUERY [js] Error: connect failed to replica set atlas-mritki-shard-0/myfirstcluster-sh

The first connection was with SRV: mongodb+srv (it is on the top of previous post)

I tried SRV string only from your post.It is not working for me
I tried long form and others but none working
All with mongo ( i am not suing mongosh)