Search code examples
djangomongodbdjongo

Cant connect remote mongodb server with django/djongo


I try to connect to a remote MongoDB database using Djongo, however I get an error - as I understand it, the connection is trying to connect to the local host.

Mongo server works with my IP, the connection user in the database has the status: ismaster, the port is default (27017). Connection with using pymongo.MongoClient('') - works. MongoDB version is 3.6.9

There is ERROR

**pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused**

I use the following versions:

Django==2.2.8
djongo==1.3.1
python3.6.3

My settings.py file contains:

DATABASES = {
    'default': {
            'ENGINE': 'djongo',
            'NAME': 'remote_db',
            'HOST': 'mongodb://user:password@<remote_ip>:27017/remote_db',
            'USER': 'user',
            'PASSWORD': 'password',
    }
}

There is my traceback

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/dave/asr_project/asr_project/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/home/dave/asr_project/asr_project/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/dave/asr_project/asr_project/lib/python3.6/site-packages/django/core/management/base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/dave/asr_project/asr_project/lib/python3.6/site-packages/django/core/management/base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "/home/dave/asr_project/asr_project/lib/python3.6/site-packages/django/core/management/base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "/home/dave/asr_project/asr_project/lib/python3.6/site-packages/django/core/management/commands/makemigrations.py", line 101, in handle
    loader.check_consistent_history(connection)
  File "/home/dave/asr_project/asr_project/lib/python3.6/site-packages/django/db/migrations/loader.py", line 283, in check_consistent_history
    applied = recorder.applied_migrations()
  File "/home/dave/asr_project/asr_project/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 73, in applied_migrations
    if self.has_table():
  File "/home/dave/asr_project/asr_project/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/dave/asr_project/asr_project/lib/python3.6/site-packages/django/db/backends/base/introspection.py", line 48, in table_names
    return get_names(cursor)
  File "/home/dave/asr_project/asr_project/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/dave/asr_project/asr_project/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/dave/asr_project/asr_project/lib/python3.6/site-packages/pymongo/database.py", line 856, in list_collection_names
    for result in self.list_collections(session=session, **kwargs)]
  File "/home/dave/asr_project/asr_project/lib/python3.6/site-packages/pymongo/database.py", line 819, in list_collections
    _cmd, read_pref, session)
  File "/home/dave/asr_project/asr_project/lib/python3.6/site-packages/pymongo/mongo_client.py", line 1454, in _retryable_read
    read_pref, session, address=address)
  File "/home/dave/asr_project/asr_project/lib/python3.6/site-packages/pymongo/mongo_client.py", line 1253, in _select_server
    server = topology.select_server(server_selector)
  File "/home/dave/asr_project/asr_project/lib/python3.6/site-packages/pymongo/topology.py", line 235, in select_server
    address))
  File "/home/dave/asr_project/asr_project/lib/python3.6/site-packages/pymongo/topology.py", line 193, in select_servers
    selector, server_timeout, address)
  File "/home/dave/asr_project/asr_project/lib/python3.6/site-packages/pymongo/topology.py", line 209, in _select_servers_loop
    self._error_message(selector))
pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused

I reviewed the following solutions and guides, but none of this helped me.

connect mongodb with djongo (Django)

https://github.com/nesdis/djongo

https://www.youtube.com/watch?v=iKB_4HWKMCc


Solution

  • The issue with the djongo version.

    Please use djongo==1.2.38.

    pip install djongo==1.2.38. This will solve your problem