Reputation: 21
I have an object that initiate ssh tunnel connection to a remote mongoDB. The same script works perfectly fine in other scraper running in a virtual machine that write data into the MongoDB. However, in object in my GUI application keep giving me could not establish connection to ssh gateway error, while the scraper with the same code works fine.
Version:
sshtunnel version = 0.1.5
pymongo version = 3.8.0
python version = 3.6.7
def initMongo(self):
for attempt in range(5):
try:
MONGO_HOST = "xxx.xxx.xxx.xx"
REMOTE_PORT = 22
MONGO_USER = "USER"
MONGO_PASS = "PASSWORD"
#I have tried this but doesnt work
#self.Mclient = MongoClient('mongodb://{}:{}@{}:{}/'.format(MONGO_USER,MONGO_PASS,MONGO_HOST,REMOTE_PORT),connect=False)
sshtunnel.SSH_TIMEOUT = 60.0
sshtunnel.TUNNEL_TIMEOUT = 60.0
self.remoteServer = sshtunnel.SSHTunnelForwarder((MONGO_HOST,22),
ssh_username=MONGO_USER,
ssh_password=MONGO_PASS,
remote_bind_address=('127.0.0.1',27017))
self.remoteServer.start()
self.client = MongoClient('127.0.0.1',self.remoteServer.local_bind_port)
self.db = self.client.db_I_want
return
except Exception as e:
print("From Object :{}".format(e))
#time.sleep(5)
self.initMongo()
2019-08-25 17:54:11,266| ERROR | Could not connect to gateway xxx.xxx.xxx.xxx:xx : xxx
From Object :Could not establish session to SSH gateway
2019-08-25 17:54:11,290| ERROR | Could not connect to gateway xxx.xxx.xxx.xxx:xx : xxx
From Object :Could not establish session to SSH gateway
2019-08-25 17:54:11,311| ERROR | Could not connect to gateway xxx.xxx.xxx.xxx:xx : xxx
From Object :Could not establish session to SSH gateway
2019-08-25 17:54:11,336| ERROR | Could not connect to gateway xxx.xxx.xxx.xxx:xx : xxx
Upvotes: 1
Views: 1465
Reputation: 21
Fixed it by increasing the remote database's max connection. https://www.howtogeek.com/240809/is-it-possible-to-have-multiple-ssh-connections-to-the-same-system/
Upvotes: 1