Connecting to MongoDB Atlas Cluster

Reading Time: 3 minutes

Connecting to Mongodb Atlas

Well as the title suggest this post is about connecting to a Mongodb Atlas instance. I needed a cloud service for my database so I could build a small test application. Though it should’ve been easier to just use the docs there’s seems to be some things missing for someone who’s very new to Mongodb and trying to use the Atlas as a datastore.

I used the free sandbox from Mongodb Atlas where the Mongodb Atlas creates a cluster containing three  AWS instances . So once you’re done with creating the cluster you’ll probably have to do the following in case you don’t have a static IP address from where this service needs to be accessed.

MongoDB Whitelist
Public facing MongoDB cluster

By adding an entry of the wildcard IP address 0.0.0.0/0 we make the cluster open to the entire internet.

The next step would be to create at least one Mongodb user which is different from your Mongodb cluster user. So let’s go ahead and create a Mongodb user

MongoDB user creation

With those thing in place, we can begin to connect to our database. Click on the connect button once the provisioning is done for your cluster and you’ll see several options to connect. Choose the Mongodb shell option and follow the steps as shown below

MongoDB connect

Uh oh… I got an error …!

Well that’s why you’re here probably to check if anyone else had the same errors. Ok so I used the version 4.2.5 for mongo shell and initially I had the error that looked like the following,

 

./mongo "mongodb+srv://-shard-khz3a.mongodb.net:27017/test"  --username my_username
2020-04-05T21:37:33.806+0530 D1 -        [main] User Assertion: DNSHostNotFound: Failed to look up service "_mongodb._tcp.XXXXX-shard-khz3a.mongodb.net": Success src/mongo/util/dns_query_posix-impl.h 324
DNSHostNotFound: Failed to look up service "_mongodb._tcp.pks-db-shard-00-01-khz3a.mongodb.net": Success
try './mongo --help' for more information

The one above seems like a DNS failure so the first thing to resolve this is we can try and use the URI of one of the instances created. Use the primary one as shown here

Mongodb Atlas cluster instances.

The reason for choosing primary ec2 instance is that the secondary ones don’t allow for commands to be executed on them.

Error even after changing it to specific instance URI

Ok so if you still see the same error of DNS drop the +srv and instead use the standard Mongodb URI connection string. So 

 

./mongo "mongodb+srv://XXXX-shard-khz3a.mongodb.net:27017/test" --username my_username

becomes

./mongo "mongodb://XXXX-shard-khz3a.mongodb.net:27017/test" --username my_username
Now the error I got was not related to DNS however it was to do with the SSL not being enabled.

Mongodb Atlas requires the tls or ssl must be enabled for client to connect to the cluster. Using +srv format enables this by default but when using standard format we’ll have to enable it ourselves thus you’ll need to change it and add the –tls option to above URI

./mongo "mongodb://XXXX-shard-khz3a.mongodb.net:27017/test" --username my_username --tls
Error 3: The server now accepts connection but fails to authenticate user.

You might see something like the following,

 

2020-04-05T22:07:07.054+0530 D1 -        [js] User Assertion: AuthenticationFailed: Authentication failed. src/mongo/util/future_impl.h 657
 
*** It looks like this is a MongoDB Atlas cluster. Please ensure that your IP whitelist allows connections from your network.
 
2020-04-05T22:07:07.054+0530 E  QUERY    [js] Error: Authentication failed. :

Now to resolve this the last piece is to specify the database that contains the users we created on the Database Access  page. These users are stored in the admin database so provide that option in the above URI string and the command now becomes

./mongo "mongodb://XXXX-shard-khz3a.mongodb.net:27017/test" --username my_username --authenticationDatabase admin

Using secondary instance instead of primary

If you use secondary instance instead of the primary one then you’ll see errors like the following,

 

MongoDB Enterprise XXXX-shard-0:SECONDARY> show databases
2020-04-05T22:17:04.230+0530 E  QUERY    [js] uncaught exception: Error: listDatabases failed:{
	"operationTime" : Timestamp(1586105218, 1),
	"ok" : 0,
	"errmsg" : "not master and slaveOk=false",
	"code" : 13435,
	"codeName" : "NotMasterNoSlaveOk",
	"$clusterTime" : {
		"clusterTime" : Timestamp(1586105218, 1),
		"signature" : {
			"hash" : BinData(0,"CNeS3h3y3d/Th3JtBDfNt30G1g4="),
			"keyId" : NumberLong("6811052274362613762")
		}
	}
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13

As you can see if you try to execute any command you’ll get the above error. Thus use only primary instance for running commands.

Leave a Reply