mongodb pymongo客户端无法通过ssh隧道连接

7bsow1i6  于 2023-03-07  发布在  Go
关注(0)|答案(1)|浏览(230)

在本地计算机上,我设置了一个ssh隧道

ssh -i "mykey.pem" -L 27017:mymongo.us-east-1.docdb.amazonaws.com:27017 ec2-user@<ec2_ip> -N

然后,我可以连接到mongo从我的本地机器罚款通过mongo shell

mongo --ssl --host 127.0.0.1:27017 --sslCAFile rds-combined-ca-bundle.pem --sslAllowInvalidHostnames --username <user> --password <password>

但是

client = MongoClient('mongodb://<user>:<password>@127.0.0.1:27017/?ssl=true', tlsAllowInvalidCertificates=True, tlsCAFile='rds-combined-ca-bundle.pem')
db = client.testdb
print(db.list_collection_names())

但我得到一个超时错误为我的aws mongo

pymongo.errors.ServerSelectionTimeoutError: Could not reach any servers in [('mymongo.us-east-1.docdb.amazonaws.com', 27017)]. Replica set is configured with internal hostnames or IPs?, Timeout: 30s, Topology Description: <TopologyDescription id: 63f876f1363f3ba7754c885sada, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('mymongo.us-east-1.docdb.amazonaws.com', 27017) server_type: Unknown, rtt: None, error=NetworkTimeout('mymongo.us-east-1.docdb.amazonaws.com:27017: timed out')>]>

不知道为什么它与mongo shell一起工作,而不是与pymongo一起工作
任何帮助都很感激

oaxa6hgo

oaxa6hgo1#

添加directConnection=True参数,这样就可以了。

client = MongoClient('mongodb://<user>:<password>@127.0.0.1:27017/?ssl=true', tlsAllowInvalidCertificates=True, tlsCAFile='rds-combined-ca-bundle.pem', directConnection=True)

查看更多信息:www.example.comhttps://pymongo.readthedocs.io/en/stable/common-issues.html?highlight=tunnel#timeout-when-accessing-mongodb-from-pymongo-with-tunneling

相关问题