简体   繁体   English

从mongo shell连接到远程副本集吗?

[英]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 : 我在亚马逊云中创建了一个3成员副本集,我已经设置了所有必需的防火墙设置和mongo配置,下面是我的副本集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. 注意:我已经使用机器主机名机器的公共IP地址创建了副本集。

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. 我正在使用以下命令从这3个副本集计算机中的任何一个连接到副本集,并且我已连接到副本集的主副本。

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 . 由于网络中的差异,mongo无法解析副本集中使用的主机名。我没有使用Public Ip,因为这很不好,我猜是这样。

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() : 连接到副本集时,您的客户端/驱动程序(例如mongo shell)使用为rs.conf()定义的副本集配置的详细信息:

  • hostnames have to be resolvable to IP addresses 主机名必须可解析为IP地址
  • connections are established using the configured hostname/IP/port for each replica set member 使用每个副本集成员的已配置主机名/ IP /端口建立连接

The following error in particular indicates that the hostname is not resolvable from your mongo client: 特别是以下错误表明主机名无法从mongo客户端解析:

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 . 主机名ip-172-31-16-84表示主机的IP地址将为172.31.16.84,这是不可路由的专用网络地址

It is not possible to connect to an internal-only facing IP address from outside the network. 无法从网络外部连接到面向内部的IP地址。

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. 为了安全地连接到部署,您将需要打开到副本集所驻留的专用网络的SSH或VPN连接,并从该专用网络中运行mongo shell。

For more information on securing your deployment, please review the MongoDB Security Checklist . 有关保护部署的更多信息,请查看MongoDB安全清单

How about using a SSH tunnel to connect to one of the server, and then use mongo shell to connect on localhost ? 如何使用SSH隧道连接到其中一台服务器,然后使用mongo shell在localhost上连接?

This is very easy to setup, secure and supported in most MongoDB GUI now. 现在,这很容易设置,安全并在大多数MongoDB GUI中受支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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