1

我尝试使用 Djongo 连接到远程 MongoDB 数据库,但是出现错误 - 据我了解,连接正在尝试连接到本地主机。

Mongo服务器使用我的IP,数据库中的连接用户状态为:ismaster,端口为默认(27017)。使用pymongo.MongoClient('')连接- 有效。MongoDB 版本是3.6.9

有错误

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

我使用以下版本:

Django==2.2.8
djongo==1.3.1
python3.6.3

我的settings.py文件包含:

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

有我的回溯

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

我查看了以下解决方案和指南,但这些都没有帮助我。

将 mongodb 与 djongo (Django) 连接

https://github.com/nesdis/djongo

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

4

1 回答 1

1

djongo 版本的问题。

请使用 djongo==1.2.38。

点安装 djongo==1.2.38。这将解决您的问题

于 2020-03-05T16:56:48.900 回答