pymongo ssh 跳板机连远程 mongodb 数据库一直 timeout?有有经验的伙计帮忙看看吗?

242 天前
 dyllen
# -*- coding: UTF-8 -*-

import pymongo
import urllib.parse
import logging
from sshtunnel import open_tunnel, SSHTunnelForwarder

sshHost = "1.2.3.4"
sshPort = 4022
sshUser = "sshUser"
sshPass = "sshPass"

mongoHost = '4.5.6.7'
mongoPort = 27017
mongoUser = urllib.parse.quote_plus('root')
mongoPass = urllib.parse.quote_plus('pass123')

server = SSHTunnelForwarder(
    (sshHost, sshPort),
    ssh_username=sshUser,
    ssh_password=sshPass,
    remote_bind_address=(mongoHost, mongoPort),
)

# server =  open_tunnel(
#     (sshHost, sshPort),
#     ssh_username=sshUser,
#     ssh_password=sshPass,
#     remote_bind_address=(mongoHost, mongoPort),
#     local_bind_address=('0.0.0.0', 10022)
# )
server.start()
# mongoDSN = 'mongodb://%s:%s@localhost:10022/dbname?authSource=admin' % (mongoUser, mongoPass)
# client = pymongo.MongoClient(mongoDSN, server.local_bind_port)
client = pymongo.MongoClient(
    '127.0.0.1', 
    server.local_bind_port, 
    username=mongoUser, 
    password=mongoPass, 
    authSource='admin'
)
db = client[dbName]

先用注释掉的代码试了一下,总提示:

Could not establish connection from local ('127.0.0.1', 10022) to remote ('4.5.6.7', 27017) side of the tunnel: open new channel ssh error: SSH session not active

再提示:pymongo.errors.ServerSelectionTimeoutError: 127.0.0.1:10022: [WinError 10061] 由于目标计算机积极拒绝,无法连接。

未注释的代码就提示pymongo.errors.ServerSelectionTimeoutError: 4.5.6.7:27017: timed out

ip 我隐藏修改过了,不是原本的 ip

753 次点击
所在节点    Python
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1027142

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX