[英]Pymongo Connection with MongoDB atlas - Error 111 Connection refused
I'm setting up a cron job, where it fetches some data from MongoDB atlas in Python3 through Pymongo in Cpanel.我正在设置一个 cron 作业,它通过 Cpanel 中的 Pymongo 从 Python3 中的 MongoDB atlas 获取一些数据。 I always get a Error 111 Connnection refused.
我总是收到错误 111 连接被拒绝。
I using python3.6 and pymongo-3.9.0, Cloud MongoDB-4.0.2我使用 python3.6 和 pymongo-3.9.0,Cloud MongoDB-4.0.2
I have tried via SSHtunnel forwarder, but not sure how to give host_ip_addres, where MongoDB is in cluster我已经尝试通过 SSHtunnel 转发器,但不确定如何提供 host_ip_addres,其中 MongoDB 在集群中
class DbConnection():
def __init__(self):
self.connectionServer = "mongodb+srv://"
self.userName = "name"
self.userPass = "pass"
self.connectionCluster = "@temp-cluster0-lt2rb.mongodb.net"
self.connectionDb = "developmentDB"
def db_connect(self):
''' This function is used to connect to remote db with authentication
Return type --> returns the url string of the db
parameters--> self
'''
try:
connectionUrl = self.connectionServer + self.userName + ":" + self.userPass + self.connectionCluster + "/test?retryWrites=true&w=majority"
print(connectionUrl)
myClient = pymongo.MongoClient(connectionUrl, port=12312)
db = myClient.test
print(myClient.test)
I'm expecting it to connect to the MongoDB cluster DB and read/Write the documents through it.我期待它连接到 MongoDB 集群数据库并通过它读取/写入文档。
So the comments for the question solved the issue, so i'll just put this here for any future reference所以问题的评论解决了这个问题,所以我把它放在这里以供将来参考
The error can be resulted for must people because of a miss-configuration when moving from a self hosted MongoDB to a Remoted host service (Like MongoDB Atlas)由于从自托管 MongoDB 移动到远程主机服务(如 MongoDB Atlas)时配置错误,必须人员可能会出现该错误
So, the +srv
is DNS Seed List Connection Format .所以,
+srv
是DNS Seed List Connection Format 。
When switching from port based connection to DNS seed based connection, we should remove any port configuration from our connection string, ie:从基于端口的连接切换到基于 DNS 种子的连接时,我们应该从连接字符串中删除任何端口配置,即:
class DbConnection():
def __init__(self):
self.connectionServer = "mongodb+srv://"
self.userName = "name"
self.userPass = "pass"
self.connectionCluster = "@temp-cluster0-lt2rb.mongodb.net"
self.connectionDb = "developmentDB"
def db_connect(self):
''' This function is used to connect to remote db with authentication
Return type --> returns the url string of the db
parameters--> self
'''
try:
connectionUrl = self.connectionServer + self.userName + ":" + self.userPass + self.connectionCluster + "/test?retryWrites=true&w=majority"
print(connectionUrl)
// myClient = pymongo.MongoClient(connectionUrl, port=12312)
// We remove the PORT
myClient = pymongo.MongoClient(connectionUrl)
db = myClient.test
print(myClient.test)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.