无法通过 SSH 隧道连接 Mongo 个副本

Cannot connect Mongo replica over SSH tunnel

我已经创建了一个从我的笔记本电脑到 linux 服务器的 SSH 隧道,其中一个 Mongo 节点是 运行。我能够连接实际节点:

mongo "mongodb://localhost:22222/bud"

但是我无法使用连接,因为没有设置副本参数(这与隧道无关,它也发生在服务器上):

rs_bud:SECONDARY> db.items.find({'info.author.id':'leos'},{'info':1}).sort({ 'info.date': -1 }).limit(3);
Error: error: {
        "topologyVersion" : {
                "processId" : ObjectId("61c7088a199c68867b798434"),
                "counter" : NumberLong(4)
        },
        "ok" : 0,
        "errmsg" : "not master and slaveOk=false",
        "code" : 13435,
        "codeName" : "NotPrimaryNoSecondaryOk",
        "$clusterTime" : {
                "clusterTime" : Timestamp(1641239861, 1),
 

但是当我使用完整的 URL 我无法连接:

mongo "mongodb://localhost:22222/bud?replicaSet=rs_bud"
MongoDB shell version v5.0.5
connecting to: mongodb://localhost:22222/bud?compressors=disabled&gssapiServiceName=mongodb&replicaSet=rs_bud
{"t":{"$date":"2022-01-03T20:03:27.106Z"},"s":"I",  "c":"NETWORK",  "id":4333208, "ctx":"ReplicaSetMonitor-TaskExecutor","msg":"RSM host selection timeout","attr":{"replicaSet":"rs_bud","error":"FailedToSatisfyReadPreference: Could not find host match
ing read preference { mode: \"nearest\" } for set rs_bud"}}
Error: Could not find host matching read preference { mode: "nearest" } for set rs_bud, rs_bud/localhost:22222 :
connect@src/mongo/shell/mongo.js:372:17
@(connect):2:6
exception: connect failed
exiting with code 1

当隧道不是 运行 时发生同样的错误。将阅读首选项更改为 primarysecondary 没有帮助。如何远程访问我的副本?

连接到副本集时,连接字符串中的 host:port 对是种子列表。

driver/client 将依次尝试连接到种子列表中的每个主机,直到它获得连接。

然后运行 ​​isMasterhello 命令来确定哪个节点是主节点,并获取所有副本集成员的列表。

然后它会断开原始连接,并尝试使用检索到的主机和端口信息连接到每个副本集成员。

hello命令返回的主机信息将是rs.conf()中列出的host:port,即用于启动副本集的主机名。

在此用法中,驱动程序将只能使用本地主机和 SSH 隧道正在侦听的本地端口成功连接。

当你在连接字符串中传递副本集名称时,它将首先使用连接字符串中的locahost/port连接,发现所有成员的host:port,然后尝试连接到每个成员。

为了通过 SSH 隧道连接到副本集,您需要让本地计算机将 rs.conf() 中列出的所有成员主机名解析为 127.0.0.1,并为每个成员打开一个 SSH 隧道这些成员中,使用 rs.conf() 中列出的端口号作为本地端口。