
# -*- 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