简体   繁体   中英

connect to remote mongodb using python not working from my local host

I am using python and MongoClient from pmongo to connect to Remote mongodb , note that my local machine can ssh on the remote one using public key but whenever I try to connect to the db it timeouts

client = MongoClient('mongodb://ip/dataset?replicaSet=replica')
db = client.db_name
db['collection'].find({})

this will always timeout

pymongo.errors.ServerSelectionTimeoutError: ip:27017: timed out

  • When I try to connect using Mongo compass it works fine, but I provide my private key for ssh tunneling
  • when I use the same code inside another remote machine (not the same one with mongodb) it works fine, so I don't know why in my local machine this is the case
import pymongo

url = "mongodb://ip/dataset?replicaSet=replica"
client = pymongo.MongoClient(url )
mongo = client.database

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM