I'm using django with mongodb atlas and trying to deploy using heroku. Somehow I keep facing pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused this error.
DATABASES = {
'default': {
'ENGINE': 'djongo',
'NAME': 'name',
'HOST': 'mongodb+srv://user:pass@cluster0-xb9zh.mongodb.net/test?retryWrites=true&w=majority',
'USER': 'user',
'PASSWORD': 'pass',
}
}
For django setting.py I simply changed it from sqlite to mongodb. It works perfectly in my local. There is one change I am suspicious is I had to change a mongo_client.py file from localhost to the connection string in order to make my mongodb connect to atlas instead of my local database. Not sure if that has anything to do with this.
class MongoClient(common.BaseObject):
"""
A client-side representation of a MongoDB cluster.
Instances can represent either a standalone MongoDB server, a replica
set, or a sharded cluster. Instances of this class are responsible for
maintaining up-to-date state of the cluster, and possibly cache
resources related to this, including background threads for monitoring,
and connection pools.
"""
HOST = "mongodb+srv://user:pass@cluster0-xb9zh.mongodb.net/test?retryWrites=true&w=majority"
PORT = 27017
# Define order to retrieve options from ClientOptions for __repr__.