如何通过ssh隧道将数据保存到远程mongoDB? (拒绝连接)

时间:2019-10-15 13:10:34

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

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

我尝试和失败的事情: 1.我安装了mongo DB,创建了sacred3,我有ssh键。 我编辑了composer install -vvv,并添加了:

/etc/mongod.conf

然后我启用了端口转发功能

# network interfaces net: port: 27017 bindIp: 0.0.0.0 //(使用正确的ip)

因此,据我所知,如果我连接到本地主机:6666,它将被转发到106.969.696.969:27017

所以在那之后,我对Sacred framework进行了实验:

python exp1.py -m localhost:6666:sacred3

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

ssh -fN -i ~/.ssh/sacred_key-pair.pem -L 6666:localhost:27017 ubuntu@106.969.696.969

这让我发疯了。请帮忙!

exp1.py的内容下方:

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

1 个答案:

答案 0 :(得分:1)

根据文档supplied,看起来您正在创建两个观察者,或者覆盖了-m传递的连接参数,其中MongoObserver.create()在使用以下代码的代码中指定默认的mongo主机和端口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