Getting Timeout Error While connecting to MongoDB atlas URI in Django Project

Hello, Can Anyone Help me with this error,
I am using Django to connect with MongoDB using the Djongo library and dnspython

But I am getting this error

    Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/home/bluethink/Desktop/Abdulla/MongoDBProject/env/lib/python3.6/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/home/bluethink/Desktop/Abdulla/MongoDBProject/env/lib/python3.6/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/bluethink/Desktop/Abdulla/MongoDBProject/env/lib/python3.6/site-packages/django/core/management/base.py", line 328, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/bluethink/Desktop/Abdulla/MongoDBProject/env/lib/python3.6/site-packages/django/core/management/base.py", line 369, in execute
    output = self.handle(*args, **options)
  File "/home/bluethink/Desktop/Abdulla/MongoDBProject/env/lib/python3.6/site-packages/django/core/management/base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "/home/bluethink/Desktop/Abdulla/MongoDBProject/env/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 86, in handle
    executor = MigrationExecutor(connection, self.migration_progress_callback)
  File "/home/bluethink/Desktop/Abdulla/MongoDBProject/env/lib/python3.6/site-packages/django/db/migrations/executor.py", line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/home/bluethink/Desktop/Abdulla/MongoDBProject/env/lib/python3.6/site-packages/django/db/migrations/loader.py", line 49, in __init__
    self.build_graph()
  File "/home/bluethink/Desktop/Abdulla/MongoDBProject/env/lib/python3.6/site-packages/django/db/migrations/loader.py", line 212, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "/home/bluethink/Desktop/Abdulla/MongoDBProject/env/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 76, in applied_migrations
    if self.has_table():
  File "/home/bluethink/Desktop/Abdulla/MongoDBProject/env/lib/python3.6/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 "/home/bluethink/Desktop/Abdulla/MongoDBProject/env/lib/python3.6/site-packages/django/db/backends/base/introspection.py", line 48, in table_names
    return get_names(cursor)
  File "/home/bluethink/Desktop/Abdulla/MongoDBProject/env/lib/python3.6/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 "/home/bluethink/Desktop/Abdulla/MongoDBProject/env/lib/python3.6/site-packages/djongo/introspection.py", line 47, in get_table_list
    for c in cursor.db_conn.list_collection_names()
  File "/home/bluethink/Desktop/Abdulla/MongoDBProject/env/lib/python3.6/site-packages/pymongo/database.py", line 863, in list_collection_names
    for result in self.list_collections(session=session, **kwargs)]
  File "/home/bluethink/Desktop/Abdulla/MongoDBProject/env/lib/python3.6/site-packages/pymongo/database.py", line 826, in list_collections
    _cmd, read_pref, session)
  File "/home/bluethink/Desktop/Abdulla/MongoDBProject/env/lib/python3.6/site-packages/pymongo/mongo_client.py", line 1462, in _retryable_read
    read_pref, session, address=address)
  File "/home/bluethink/Desktop/Abdulla/MongoDBProject/env/lib/python3.6/site-packages/pymongo/mongo_client.py", line 1279, in _select_server
    server = topology.select_server(server_selector)
  File "/home/bluethink/Desktop/Abdulla/MongoDBProject/env/lib/python3.6/site-packages/pymongo/topology.py", line 243, in select_server
    address))
  File "/home/bluethink/Desktop/Abdulla/MongoDBProject/env/lib/python3.6/site-packages/pymongo/topology.py", line 200, in select_servers
    selector, server_timeout, address)
  File "/home/bluethink/Desktop/Abdulla/MongoDBProject/env/lib/python3.6/site-packages/pymongo/topology.py", line 217, in _select_servers_loop
    (self._error_message(selector), timeout, self.description))
pymongo.errors.ServerSelectionTimeoutError: localhost:27017: timed out, Timeout: 30s, Topology Description: <TopologyDescription id: 5fdc9bf1e88fc8cd3e5e3310, topology_type: Single, servers: [<ServerDescription ('localhost', 27017) server_type: Unknown, rtt: None, error=NetworkTimeout('localhost:27017: timed out',)>]>

Here is my DB settings

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.sqlite3',
    'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
},
'myDB': {
    'ENGINE': 'djongo',
    'NAME': 'myDB',
    'HOST': 'mongodb+srv://TrupinionSurvey:<password>@test-db-shard-00-01.jtj0h.mongodb.net/myDB?retryWrites=true&w=majority',
    'USER': 'TrupinionSurvey',
    'PASSWORD': 'TrupinionSurvey',
}

}

When I try the command python manage.py migrate --database=myDB
I get this error. I use --database=myDB because I am using two database.

Also I tried this method but After using this method I got connectionTimeout Error after 21 second with dnspython

If anybody can solve this thanks in advance

Hi @Abdulla_Ansari

Are you sure you are loading your settings correctly? It says right here it is connecting to localhost:27017

Hi @Abdulla_Ansari,

Have a closer look at the djongo README . It looks to me like the MongoDB URL should be provided as a ‘host’ value inside a ‘CLIENT’ dict, instead of a ‘HOST’ value at the top level. If that’s it, then I totally see why you made the mistake!

Let me know if this fixes your problem.

Mark

1 Like

Some more documentation here which might help.