Mike Dull
Mike Dull

Reputation: 3

Connect to MongoDB atlas using host public IP

I'm trying to connect to my mongodb atlas instance using public IP instead of hostname.

All IPs are whitelisted in security settings.

I've looked up the public IP for each node in my cluster and attempted to connect to each, but fail each time.

Hosts ---> Public Address
(Secondary node) cluster0-shard-00-00-ubkbl.mongodb.net --->  54.221.247.60
(Primary node) cluster0-shard-00-01-ubkbl.mongodb.net ---> 54.145.147.81
(secondary node) cluster0-shard-00-02-ubkbl.mongodb.net ---> 184.73.253.26

How can one find the proper public IP to use?

Error example:

mongo admin --host 54.145.147.81 -u <USERNAME> --port 27017 -p --verbose
MongoDB shell version v4.2.0
Enter password:
connecting to: mongodb://54.145.147.81:27017/admin?compressors=disabled&gssapiServiceName=mongodb
2020-05-20T20:09:18.015-0400 D1 NETWORK  [js] creating new connection to:54.145.147.81:27017
2020-05-20T20:09:18.032-0400 D1 NETWORK  [js] connected to server 54.145.147.81:27017
2020-05-20T20:09:18.050-0400 I  NETWORK  [js] DBClientConnection failed to receive message from 54.145.147.81:27017 - HostUnreachable: Connection closed by peer
2020-05-20T20:09:18.050-0400 D1 -        [js] User Assertion: HostUnreachable: network error while attempting to run command 'isMaster' on host '54.145.147.81:27017'  src/mongo/client/dbclient_base.cpp 226
2020-05-20T20:09:18.050-0400 D1 -        [js] User Assertion: InternalError: network error while attempting to run command 'isMaster' on host '54.145.147.81:27017'  src/mongo/scripting/mozjs/mongo.cpp 832
2020-05-20T20:09:18.050-0400 E  QUERY    [js] Error: network error while attempting to run command 'isMaster' on host '54.145.147.81:27017'  :
connect@src/mongo/shell/mongo.js:341:17
@(connect):2:6
2020-05-20T20:09:18.050-0400 D1 -        [js] User Assertion: Location12513: connect failed src/mongo/shell/shell_utils.cpp 416
2020-05-20T20:09:18.050-0400 I  QUERY    [js] MozJS GC prologue heap stats -  total: 3762898 limit: 0
2020-05-20T20:09:18.053-0400 I  QUERY    [js] MozJS GC epilogue heap stats -  total: 2 limit: 0
2020-05-20T20:09:18.053-0400 D1 -        [main] User Assertion: Location12513: connect failed src/mongo/scripting/mozjs/proxyscope.cpp 320
2020-05-20T20:09:18.053-0400 F  -        [main] exception: connect failed
2020-05-20T20:09:18.053-0400 E  -        [main] exiting with code 1

Upvotes: 0

Views: 1423

Answers (1)

Joe
Joe

Reputation: 28316

Encryption

MongoDB Atlas requires all inbound connections to use TLS/SSL, and will simply drop any connection that does not.

Replica Set

Atlas clusters are replica sets of at least 3 members. Only the primary member will be able to accept writes at any given time.
Which member becomes primary is determined by election among the eligible nodes. A new primary may be elected in a number of situations:

  • rolling restart to modify certain settings
  • MongoDB or Atlas patches and version upgrades
  • hardware issues on the primary node
  • network, power, or other environmental issues
  • primary becomes unresponsive for 5 seconds or more for any reason

If you connect directly to a node by IP address without using the replica set option, you client will not be aware that it is connecting to a replica set and will not monitor for such changes. This means that if there is an election, the client may suddenly be unable to write.

Server Discovery

If you do connect using the replica set option, there will be a server discovery phase upon connecting. The driver/client will query the first connected node for the list of replica set members and the current primary. The client will then drop the original connection, and connect to each node in the list in order to monitor the replica set state.

This list will be provided in the same format that is stored in the internal replica set configuration document. For Atlas, this will be the hostname:port.

This means that even if the initial connection by IP address, during the server discovery phase the client will need to be able to resolve the host names.

If the driver/application will not be able to resolve names, it will have difficulty maintaining connection to Atlas.

Upvotes: 2

Related Questions