Heroku Deployment With MongoDB Error: Connection Refused
Heroku Deployment With MongoDB Error: Connection Refused
pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused
他们大家好,我在尝试在 python 中部署我的 Django 站点时遇到了一些麻烦。尝试连接我的 MongoDB atlas 数据库时出现此错误(如上所示)。我读到我必须将我的 IP 列入白名单,但是当我这样做时它不起作用。
这是我的 views.py 文件:
class Initialize():
def __init__(self, name):
self.name = name
myclient = pymongo.MongoClient('mongodb+srv://<MY Username>:<My Password>@cluster0-gicez.mongodb.net/test?retryWrites=true&w=majority')
global mydb
mydb = myclient[f"{self.name}"]
global userData
userData = mydb["userData"]
global authData
authData = mydb["auth_user"]
global storesCollection
storesCollection = mydb["stores"]
global mycolPostalCodes
mycolPostalCodes = mydb["postalCodes"]
当我在尝试部署之前 运行 我的代码时,代码运行良好。另外,这是我的 settings.py 文件:
DATABASES = {
'default': {
'ENGINE': 'djongo',
'NAME': 'cluster0',
'HOST' : 'mongodb+srv://<my username>:<my password>@cluster0-gicez.mongodb.net/test?retryWrites=true&w=majority',
'USER': '<my username>',
'PASSWORD': '<my password>',
}
}
如有任何帮助,我们将不胜感激。谢谢。如果需要,请给我发消息以获取更多信息。
您需要将客户端的 IP 列入白名单,在本例中为 Heroku。他们的 IP 地址描述为 here。将您的 home/laptop IP 列入白名单不允许 Heroku 连接到您的集群。
要找出特定机器的地址,您可以 运行 例如有问题的机器上的以下代码:
python -c "import urllib; print(urllib.urlopen('https://wtfismyip.com/text').read())"
所以,我所要做的就是更改我的设置文件。我连接到 mongodb atlas 的方式已经过时了。
所以这就是我的连接方式:
DATABASES = {
'default': {
'ENGINE': 'djongo',
'CLIENT': {
'host': 'mongodb+srv://{username}:{password}@cluster0-gicez.mongodb.net/test?retryWrites=true&w=majority',
'username': {username},
'password': {password},
'authMechanism': 'SCRAM-SHA-1'
}
}
}
pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused
他们大家好,我在尝试在 python 中部署我的 Django 站点时遇到了一些麻烦。尝试连接我的 MongoDB atlas 数据库时出现此错误(如上所示)。我读到我必须将我的 IP 列入白名单,但是当我这样做时它不起作用。
这是我的 views.py 文件:
class Initialize():
def __init__(self, name):
self.name = name
myclient = pymongo.MongoClient('mongodb+srv://<MY Username>:<My Password>@cluster0-gicez.mongodb.net/test?retryWrites=true&w=majority')
global mydb
mydb = myclient[f"{self.name}"]
global userData
userData = mydb["userData"]
global authData
authData = mydb["auth_user"]
global storesCollection
storesCollection = mydb["stores"]
global mycolPostalCodes
mycolPostalCodes = mydb["postalCodes"]
当我在尝试部署之前 运行 我的代码时,代码运行良好。另外,这是我的 settings.py 文件:
DATABASES = {
'default': {
'ENGINE': 'djongo',
'NAME': 'cluster0',
'HOST' : 'mongodb+srv://<my username>:<my password>@cluster0-gicez.mongodb.net/test?retryWrites=true&w=majority',
'USER': '<my username>',
'PASSWORD': '<my password>',
}
}
如有任何帮助,我们将不胜感激。谢谢。如果需要,请给我发消息以获取更多信息。
您需要将客户端的 IP 列入白名单,在本例中为 Heroku。他们的 IP 地址描述为 here。将您的 home/laptop IP 列入白名单不允许 Heroku 连接到您的集群。
要找出特定机器的地址,您可以 运行 例如有问题的机器上的以下代码:
python -c "import urllib; print(urllib.urlopen('https://wtfismyip.com/text').read())"
所以,我所要做的就是更改我的设置文件。我连接到 mongodb atlas 的方式已经过时了。 所以这就是我的连接方式:
DATABASES = {
'default': {
'ENGINE': 'djongo',
'CLIENT': {
'host': 'mongodb+srv://{username}:{password}@cluster0-gicez.mongodb.net/test?retryWrites=true&w=majority',
'username': {username},
'password': {password},
'authMechanism': 'SCRAM-SHA-1'
}
}
}