Chapter 2: Importing, Exporting, and Querying Data

Hi, I’m trying to connect to the server, and it works fine, but it connects to the secondary. I looked for the address of the primary server and when I connected the following message appears.

DNSHostNotFound: Failed to look up service "_mongodb._tcp.sandbox-shard-00-02.wdvhc.mongodb.net": Success
try 'mongo --help' for more information

And I have access to Atlas, I have Internet.

Now, inside the secondary server, I try to execute the commands, “show dbs”, “show collections”, “db.zips.find ({” state “:” NY “})”, it gives me the following message:

2020-11-17T16:21:12.150-0300 E  QUERY    [js] Error: listDatabases failed:{
	"operationTime" : Timestamp(1605640871, 7),
	"ok" : 0,
	"errmsg" : "not master and slaveOk=false",
	"code" : 13435,
	"codeName" : "NotMasterNoSlaveOk",
	"$clusterTime" : {
		"clusterTime" : Timestamp(1605640871, 7),
		"signature" : {
			"hash" : BinData(0,"v0hIK0eMQGof9oXjpfQ6HYh6fN4="),
			"keyId" : NumberLong("6894171157239431171")
		}
	}
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs/<@src/mongo/shell/mongo.js:147:19
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:99:12
shellHelper.show@src/mongo/shell/utils.js:906:13
shellHelper@src/mongo/shell/utils.js:790:15
@(shellhelp2):1:1

Mongodb version 4.2.10

Help me, please.

Hi @Tomas_Valfue,

Well, you find an interesting problem…

First, this command gives me the same error:

 mongo "mongodb+srv://sandbox-shard-00-02.wdvhc.mongodb.net:27017"`
DNSHostNotFound: Failed to look up service "_mongodb._tcp.sandbox-shard-00-02.wdvhc.mongodb.net": Success
try 'mongo --help' for more information

The reason is mongo expects a DNS server, not a mongod server.

Second (this is out of scope for m001 so don’t worry)

  • From a secondary, the only way to do a read op is to run rs.slaveOk() (or a similar command).

  • To know the state of the replica set run rs.status().

Third, you should be able to connect using

mongo mongodb+srv://sandbox.wdvhc.mongodb.net/admin -u m001-student

Fourth,

This should also work:

mongo "mongodb://sandbox-shard-00-02.wdvhc.mongodb.net:27017"
As you can see, I just removed the srv.

You are using the wrong cluster address. Try

mongodb+srv://sandbox.wdvhc.mongodb.net:27017

When you have shard-Something in your name it is a host address rather than a cluster address. When you want to connect to a specific host you have to use the non-SRV mongodb:// URI.

Hi and thank you very much for answering.

@Santiago_Miranda:

-Point three
I tried and it still connects me to secondary.

$ mongo "mongodb+srv://sandbox.wdvhc.mongodb.net/admin" -u m001-student
MongoDB shell version v4.2.10
connecting to: mongodb://sandbox-shard-00-00.wdvhc.mongodb.net:27017,sandbox-shard-00-01.wdvhc.mongodb.net:27017,sandbox-shard-00-02.wdvhc.mongodb.net:27017/admin?compressors=disabled&gssapiServiceName=mongodb&ssl=true
Implicit session: session { "id" : UUID("[...]") }
MongoDB server version: 4.2.10
MongoDB Enterprise atlas-jrq5cv-shard-0:SECONDARY> 

-Point four
Give the following error or message.

$ mongo "mongodb://sandbox-shard-00-02.wdvhc.mongodb.net:27017"
MongoDB shell version v4.2.10
connecting to: mongodb://sandbox-shard-00-02.wdvhc.mongodb.net:27017/?compressors=disabled&gssapiServiceName=mongodb
2020-11-17T21:00:41.746-0300 I  NETWORK  [js] DBClientConnection failed to receive message from sandbox-shard-00-02.wdvhc.mongodb.net:27017 - HostUnreachable: Connection closed by peer


*** It looks like this is a MongoDB Atlas cluster. Please ensure that your IP whitelist allows connections from your network.


2020-11-17T21:00:41.747-0300 E  QUERY    [js] Error: network error while attempting to run command 'isMaster' on host 'sandbox-shard-00-02.wdvhc.mongodb.net:27017'  :
connect@src/mongo/shell/mongo.js:353:17
@(connect):2:6
2020-11-17T21:00:41.749-0300 F  -        [main] exception: connect failed
2020-11-17T21:00:41.749-0300 E  -        [main] exiting with code 1

I assume it has to do with something related to the administration of MongoDB, that the permissions have been changed due to a security issue of the main server?



@steevej-149:

I had also tried, looking for the name of the main server in Atlas, “Sandbox”, “Overview”, where all the replicas appear, and it gives me the error that I mentioned earlier:

$ mongo "mongodb+srv://m001-student:m001-mongodb-basics@sandbox-shard-00-02.wdvhc.mongodb.net/admin" --port 27017
DNSHostNotFound: Failed to look up service "_mongodb._tcp.sandbox-shard-00-02.wdvhc.mongodb.net": Success
try 'mongo --help' for more information

Now trying:

$ mongo mongodb://sandbox.wdvhc.mongodb.net:27017
MongoDB shell version v4.2.10
connecting to: mongodb://sandbox.wdvhc.mongodb.net:27017/?compressors=disabled&gssapiServiceName=mongodb


*** It looks like this is a MongoDB Atlas cluster. Please ensure that your IP whitelist allows connections from your network.


2020-11-17T22:04:10.785-0300 E  QUERY    [js] Error: couldn't connect to server sandbox.wdvhc.mongodb.net:27017, connection attempt failed: HostNotFound: Could not find address for sandbox.wdvhc.mongodb.net:27017: SocketException: Host not found (authoritative) :
connect@src/mongo/shell/mongo.js:353:17
@(connect):2:6
2020-11-17T22:04:10.790-0300 F  -        [main] exception: connect failed
2020-11-17T22:04:10.790-0300 E  -        [main] exiting with code 1

Well I can connect, not primary, but I can enter secondary.
Using the “IDE” provided by “MongoDB University” at the end of chapter 2, it allows me to log into the main server and practice.

Additional data, I use Fedora 32.

Thanks.

Oops…then I don’t really have an answer, but will do some research.

Check if the Atlas>Network is 0.0.0.0/0 but this is probably not the issue.

Edit: are you prompted for the password in the first case?

Another possibility @Tomas_Valfue

Just realize your connection string has no name of the replica set, so it’s not being able to find the primary.

You could still try this:

mongo "mongodb+srv://sandbox.wdvhc.mongodb.net/admin?authSource=admin&compressors=disabled&gssapiServiceName=mongodb&replicaSet=atlas-jrq5cv-shard-0&ssl=true" -u m001-student

Not sure why but --replSet flag isn’t recognized.

The replica set name and authSource do not have to be specified when using mongodb+srv as this information is included in the DNS information. For the cluster sandbox.wdvhc.mongodb.net, DNS provides the following:

;; QUESTION SECTION:
;sandbox.wdvhc.mongodb.net.	IN	ANY

;; ANSWER SECTION:
sandbox.wdvhc.mongodb.net. 59	IN	TXT	"authSource=admin&replicaSet=atlas-jrq5cv-shard-0"

You are trying to connect wrong.

When using mongodb+srv:// you must use the cluster name which is sandbox.wdvhc.mongodb.net.

You also tried

Which is wrong because when using mongodb:// you must use host names like sandbox-shard-00-02.wdvhc.mongodb.net. But you have to specify the replica set name with replicaSet=atlas-jrq5cv-shard-0.

In addition, you do not need to specify the port number as 27017 is the default port. And for mongo+srv:// URI you should not be using the port number as it is part of the SRV record of the cluster. See:

;; QUESTION SECTION:
;_mongodb._tcp.sandbox.wdvhc.mongodb.net. IN ANY

;; ANSWER SECTION:
_mongodb._tcp.sandbox.wdvhc.mongodb.net. 59 IN SRV 0 0 27017 sandbox-shard-00-00.wdvhc.mongodb.net.
_mongodb._tcp.sandbox.wdvhc.mongodb.net. 59 IN SRV 0 0 27017 sandbox-shard-00-01.wdvhc.mongodb.net.
_mongodb._tcp.sandbox.wdvhc.mongodb.net. 59 IN SRV 0 0 27017 sandbox-shard-00-02.wdvhc.mongodb.net.