简体   繁体   中英

Connecting to remote replica set from mongo shell?

I have created a 3 member replica set in Amazon cloud.I have set all the requires firewall settings and mongo congigurations.Below is my replica set config :

{
    "_id" : "rs1",
    "version" : 3,
    "protocolVersion" : NumberLong(1),
    "members" : [
            {
                    "_id" : 1,
                    "host" : "ip-172-31-16-84:27017",
                    "arbiterOnly" : false,
                    "buildIndexes" : true,
                    "hidden" : false,
                    "priority" : 1,
                    "tags" : {

                    },
                    "slaveDelay" : NumberLong(0),
                    "votes" : 1
            },
            {
                    "_id" : 2,
                    "host" : "ip-172-31-23-212:27017",
                    "arbiterOnly" : false,
                    "buildIndexes" : true,
                    "hidden" : false,
                    "priority" : 1,
                    "tags" : {

                    },
                    "slaveDelay" : NumberLong(0),
                    "votes" : 1
            },
            {
                    "_id" : 3,
                    "host" : "ip-172-31-14-196:27017",
                    "arbiterOnly" : false,
                    "buildIndexes" : true,
                    "hidden" : false,
                    "priority" : 1,
                    "tags" : {

                    },
                    "slaveDelay" : NumberLong(0),
                    "votes" : 1
            }
    ],
    "settings" : {
            "chainingAllowed" : true,
            "heartbeatIntervalMillis" : 2000,
            "heartbeatTimeoutSecs" : 10,
            "electionTimeoutMillis" : 10000,
            "getLastErrorModes" : {

            },
            "getLastErrorDefaults" : {
                    "w" : 1,
                    "wtimeout" : 0
            },
            "replicaSetId" : ObjectId("5720fb585ef3baca32efe765")
    }
}

NOTE : I have created the replica set using the machine host name machines public ip address.

i am using below command to connect to replica set from any of these 3 replica set machines and i get connected to primary of the replica set.

mongo --host  "rs1/ip-172-31-16-84:27017"

But when i use the same command from any other machine (suppose my local machine) not in same lan . The command fails with below error :

C:\Users\gur35948>mongo --host "rs1/52.221.230.236:27017"
MongoDB shell version: 3.0.6
connecting to: rs1/52.221.230.236:27017/test
2016-04-28T11:54:02.851+0530 I NETWORK  starting new replica set monitor for     replica set rs1 with seeds 52.221.230.236:27017
2016-04-28T11:54:02.853+0530 I NETWORK  [ReplicaSetMonitorWatcher] starting
2016-04-28T11:54:03.043+0530 I NETWORK  changing hosts to rs1/ip-172-31-14-196:27017,ip-172-31-16-84:27017,ip-172-31-23-212:27017 from rs1/52.221.230.236:27017
2016-04-28T11:54:13.689+0530 I NETWORK  [ReplicaSetMonitorWatcher] getaddrinfo("ip-172-31-14-196") failed: errno:11001 No such host is known.
2016-04-28T11:54:14.335+0530 I NETWORK  getaddrinfo("ip-172-31-16-84") failed: errno:11001 No such host is known.
2016-04-28T11:54:16.682+0530 I NETWORK  getaddrinfo("ip-172-31-16-84") failed: errno:11001 No such host is known.
2016-04-28T11:54:17.505+0530 I NETWORK  getaddrinfo("ip-172-31-14-196") failed:errno:11001 No such host is known.
2016-04-28T11:54:17.505+0530 W NETWORK  No primary detected for set rs1
2016-04-28T11:54:17.511+0530 E QUERY    Error: ReplicaSetMonitor no master found for set: rs1
at connect (src/mongo/shell/mongo.js:181:14)
at (connect):1:6 at src/mongo/shell/mongo.js:181

The mongo is unable to resolve the hostname used in the replica set because of the difference in the network.I have not used Public Ip because it is bad Practice i guess .

So finally The Question is how do i connect to This remote replica set??

Thanks

When connecting to a replica set your client/driver (eg. the mongo shell) uses the details as configured for your replica set as defined in rs.conf() :

  • hostnames have to be resolvable to IP addresses
  • connections are established using the configured hostname/IP/port for each replica set member

The following error in particular indicates that the hostname is not resolvable from your mongo client:

2016-04-28T11:54:13.689+0530 I NETWORK [ReplicaSetMonitorWatcher] getaddrinfo("ip-172-31-14-196") failed: errno:11001 No such host is known. 2016-04-28T11:54:14.335+0530 I NETWORK getaddrinfo("ip-172-31-16-84") failed: errno:11001 No such host is known.

The hostname ip-172-31-16-84 suggests that the IP address for the host will be 172.31.16.84, which is a non-routable private network address .

It is not possible to connect to an internal-only facing IP address from outside the network.

To securely connect to your deployment you will need to open an SSH or VPN connection to the private network where your replica set resides and run your mongo shell from within that private network.

For more information on securing your deployment, please review the MongoDB Security Checklist .

How about using a SSH tunnel to connect to one of the server, and then use mongo shell to connect on localhost ?

This is very easy to setup, secure and supported in most MongoDB GUI now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM