I am trying to connect my Django application with the Atlas MongoDB cloud database using Djongo. The setting file database section is,
DATABASES = {
'default': {
'ENGINE': 'djongo',
'NAME': 'mydb',
'HOST': 'mongodb+srv://user:' + urllib.parse.quote_plus('password') + '@xx-xxxxx.mongodb.net/test?retryWrites=true&w=majority',
'USER': 'user',
'PASSWORD': 'password',
}
}
But after running python manage.py migrate
, it is throwing the following error,
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "C:\Users\XXXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:\Users\XXXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\XXXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\XXXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\base.py", line 364, in execute
output = self.handle(*args, **options)
File "C:\Users\XXXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "C:\Users\XXXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\commands\migrate.py", line 87, in handle
executor = MigrationExecutor(connection, self.migration_progress_callback)
File "C:\Users\XXXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\migrations\executor.py", line 18, in __init__
self.loader = MigrationLoader(self.connection)
File "C:\Users\XXXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\migrations\loader.py", line 49, in __init__
self.build_graph()
File "C:\Users\XXXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\migrations\loader.py", line 212, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "C:\Users\XXXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\migrations\recorder.py", line 73, in applied_migrations
if self.has_table():
File "C:\Users\XXXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\migrations\recorder.py", line 56, in has_table
return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor())
File "C:\Users\XXXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\base\introspection.py", line 48, in table_names
return get_names(cursor)
File "C:\Users\XXXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\base\introspection.py", line 43, in get_names
return sorted(ti.name for ti in self.get_table_list(cursor)
File "C:\Users\XXXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\djongo\introspection.py", line 47, in get_table_list
for c in cursor.db_conn.list_collection_names()
File "C:\Users\XXXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pymongo\database.py", line 856, in list_collection_names
for result in self.list_collections(session=session, **kwargs)]
File "C:\Users\XXXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pymongo\database.py", line 818, in list_collections
return self.__client._retryable_read(
File "C:\Users\XXXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pymongo\mongo_client.py", line 1453, in _retryable_read
server = self._select_server(
File "C:\Users\XXXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pymongo\mongo_client.py", line 1253, in _select_server
server = topology.select_server(server_selector)
File "C:\Users\XXXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pymongo\topology.py", line 233, in select_server
return random.choice(self.select_servers(selector,
File "C:\Users\XXXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pymongo\topology.py", line 192, in select_servers
server_descriptions = self._select_servers_loop(
File "C:\Users\XXXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pymongo\topology.py", line 208, in _select_servers_loop
raise ServerSelectionTimeoutError(
pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [WinError 10061] No connection could be made because the target machine actively refused it
If you look at the last line it says that pymongo is trying to connect localhost:27017
. But the host is already mentioned. As there is no localhost so it is getting the connection failure.
Now, I opened the mongo_client.py
inside C:\Users\XXXX\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\pymongo
and found the following snippet of code,
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 = "localhost"
PORT = 27017
# Define order to retrieve options from ClientOptions for __repr__.
# No host/port; these are retrieved from TopologySettings.
_constructor_args = ('document_class', 'tz_aware', 'connect')
def __init__(
self,
host=None,
port=None,
document_class=dict,
tz_aware=None,
connect=None,
type_registry=None,
**kwargs):
As per the overview the default HOST is never overloaded. Therefore it is always searching for localhost. Now, if I change localhost
to 'mongodb+srv://user:password@xx-xxxxx.mongodb.net/test?retryWrites=true&w=majority'
, the connection works correctly.
Now, definitely it is not the solution indeed. Can anyone throw some light on it?