【问题标题】:How to save data to remote mongoDB via ssh tunnel? (connection refused)如何通过 ssh 隧道将数据保存到远程 mongoDB? (拒绝连接)
【发布时间】:2020-02-12 04:28:18
【问题描述】:

我有两台电脑:Ubuntu1 和 Ubuntu2。 Ubuntu1 运行带有数据库 Sacred3 的 MongoDB。 我想通过 ssh 从 U2 连接到 U1 并将我的实验结果存储在那里。

我尝试过但失败了: 1.我安装了mongo DB,创建了sacred3,我有ssh密钥。 我编辑了/etc/mongod.conf 添加:

# network interfaces net: port: 27017 bindIp: 0.0.0.0

然后我启用了端口转发

ssh -fN -i ~/.ssh/sacred_key-pair.pem -L 6666:localhost:27017 ubuntu@106.969.696.969 //(使用正确的 ip)

所以,据我所知,如果我连接到我的 localhost:6666,它将被转发到 106.969.696.969:27017

所以在那之后,我正在用 Sacred framework 进行实验:

python exp1.py -m localhost:6666:sacred3

这应该将实验写入远程数据库,但是我得到了:

pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused

这让我发疯。请帮忙!

#

exp1.py 的以下内容:

from sacred import Experiment
from sacred.observers import MongoObserver

ex = Experiment()
ex.observers.append(MongoObserver.create())

def compute():
    summ = layer1 - layer2
    return summ


@ex.config
def my_config():

    hp_list = [{"neurons" : [32,32] , "dropout": 1.0},
            {"neurons" : [32,32] , "dropout": 0.7},
            {"neurons" : [32,16] , "dropout": 0.9},
            {"neurons" : [24,16] , "dropout": 0.9},
            {"neurons" : [24,8] , "dropout":  0.9},
            {"neurons" : [16,8] , "dropout":  0.9},
            {"neurons" : [64,64] , "dropout": 0.9},
            {"neurons" : [64,64] , "dropout": 0.7},
            {"neurons" : [64,32] , "dropout": 0.9},
            {"neurons" : [64,32] , "dropout": 0.7},
            {"neurons" : [48,32] , "dropout": 0.9},
            {"neurons" : [48,32] , "dropout": 0.7},
            {"neurons" : [48,16] , "dropout": 0.9},
            {"neurons" : [48,16] , "dropout": 0.7},]

    n_epochs = 2 


@ex.capture
def training_loop(hp_list, n_epochs):
    for j in hp_list:
        print("Epoch: ", n_epochs)
#       layer1 = random.randint(18,68)
#       layer2 = random.randint(18,68)
#       layer3 = random.randint(18,68)
        layer1 = j["neurons"][0]
        layer2 = j["neurons"][1]
        dropout_ratio = j["dropout"]


        print("WHATS UUUUUP",j, layer1, layer2, dropout_ratio, sep="_")
        # vae_training_loop_NN_DO(i, layer1, layer2, dropout_ratio )


@ex.automain
def my_main():
    training_loop()

【问题讨论】:

  • 看起来您没有将端口正确传递给脚本,因此连接到 localhost:27017 而不是 localhost:6666
  • 什么意思?我用 python exp1.py -m localhost:6666:sacred3 运行脚本
  • afaik -m switch isn't for passing arguments 到一个脚本.. 我们可以看到你的脚本的内容吗?
  • 我添加了 exp1.py 的内容,就像这里一样 sacred.readthedocs.io/en/stable/observers.html

标签: mongodb ubuntu ssh ssh-tunnel python-sacred


【解决方案1】:

根据文档supplied,看起来您正在创建两个观察者,或者覆盖您使用-m 传递的连接参数,并在使用默认mongo 主机和端口的代码中指定MongoObserver.create() localhost:27017。您可以通过 -m 参数或在代码中提供观察者连接,而不是两者。

尝试完全删除 MongoObserver.create() 行,或硬编码连接参数:MongoObserver(url='localhost:6666', db_name='sacred3')

此外,您的 mongo 主机似乎是 not liking the binding to localhost,因此您还应该将 ssh 命令中的 localhost 替换为 127.0.0.1[::1],例如 ssh -fN -i ~/.ssh/sacred_key-pair.pem -L 6666:127.0.0.1:27017 ubuntu@106.969.696.969ssh -fN -i ~/.ssh/sacred_key-pair.pem -L 6666:[::1]:27017 ubuntu@106.969.696.969

【讨论】:

  • 我删除了 ex.observers.append(MongoObserver.create()) 行,现在我收到此错误:通道 2:打开失败:连接失败:连接被拒绝
  • 现在看来您的 mongo 主机是 not liking the binding to localhost。您可以尝试用127.0.0.1[::1] 替换您的ssh 命令中的localhost,例如ssh -fN -i ~/.ssh/sacred_key-pair.pem -L 6666:127.0.0.1:27017 ubuntu@106.969.696.969
  • 哈利路亚,现在成功了!请将此评论添加到您的答案中,以便其他人可以看到所有内容。
  • 完成。然后,您还可以使用第二个错误更新您的问题,以便它显示在该错误的搜索结果中。
猜你喜欢
  • 1970-01-01
  • 2021-02-01
  • 2017-04-25
  • 2017-08-24
  • 2017-08-16
  • 2015-03-01
  • 2014-07-20
  • 2019-05-13
  • 2019-11-19
相关资源
最近更新 更多