通过Django连接到具有填充数据的外部MongoDB

时间:2019-01-10 07:28:12

标签: python django mongodb djongo

我可以使用由某些硬件收集的数据访问巨型MongoDB数据库。当前在远程服务器上。 我需要使用Django进行开发(最好)。

我可以使用直接的pymongo很好地进行迭代,但是我想使用Django的内置功能来做更多的事情。我绝对喜欢Django处理数据的方式,而不是运行mongodb查询。所以我安装了Djongo-Django的连接器。

有了Pymongo,我就这样使用它

client = MongoClient('ipaddress',
                     username='ronald',
                     password='password',
                     authSource='scanning',
                     authMechanism='SCRAM-SHA-1')

db = client["scanning"]

collection = db["some_collection"]

这很好,我可以毫无问题地连接到收藏夹。

所以继续前进到Djongo,我想我也可以类似地连接到它,但是,我一直在超时。这是我在Settings.py

中的数据库设置
DATABASES = {
    'default': {
        'ENGINE': 'djongo',
        'ENFORCE_SCHEMA': False,
        'NAME': 'some_collection',
        'HOST': 'ipaddress',
        'PORT': 27017,
        'USER': 'ronald',
        'PASSWORD': 'password',
        'AUTH_SOURCE': 'scanning',
        'AUTH_MECHANISM': 'SCRAM-SHA-1',
    }
}

这会在启动服务器时不断超时。

是否有可能这样做?

我是否最好将数据库转储到本地并将其存储在针对Django优化的干净的MongoDB中?

我应该只接受品尝,只运行mongo查询吗?

任何建议将不胜感激。

谢谢

编辑:

这是该错误的回溯

System check identified no issues (0 silenced).
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x10d726268>
Traceback (most recent call last):
  File "/Users/ronald/dev/clients/qc/datamanagement/venv/lib/python3.7/site-packages/django/utils/autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "/Users/ronald/dev/clients/qc/datamanagement/venv/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 120, in inner_run
    self.check_migrations()
  File "/Users/ronald/dev/clients/qc/datamanagement/venv/lib/python3.7/site-packages/django/core/management/base.py", line 442, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  File "/Users/ronald/dev/clients/qc/datamanagement/venv/lib/python3.7/site-packages/django/db/migrations/executor.py", line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/Users/ronald/dev/clients/qc/datamanagement/venv/lib/python3.7/site-packages/django/db/migrations/loader.py", line 49, in __init__
    self.build_graph()
  File "/Users/ronald/dev/clients/qc/datamanagement/venv/lib/python3.7/site-packages/django/db/migrations/loader.py", line 212, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "/Users/ronald/dev/clients/qc/datamanagement/venv/lib/python3.7/site-packages/django/db/migrations/recorder.py", line 61, in applied_migrations
    if self.has_table():
  File "/Users/ronald/dev/clients/qc/datamanagement/venv/lib/python3.7/site-packages/django/db/migrations/recorder.py", line 44, in has_table
    return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor())
  File "/Users/ronald/dev/clients/qc/datamanagement/venv/lib/python3.7/site-packages/django/db/backends/base/introspection.py", line 56, in table_names
    return get_names(cursor)
  File "/Users/ronald/dev/clients/qc/datamanagement/venv/lib/python3.7/site-packages/django/db/backends/base/introspection.py", line 51, in get_names
    return sorted(ti.name for ti in self.get_table_list(cursor)
  File "/Users/ronald/dev/clients/qc/datamanagement/venv/lib/python3.7/site-packages/djongo/introspection.py", line 46, in get_table_list
    for c in cursor.db_conn.collection_names(False)
  File "/Users/ronald/dev/clients/qc/datamanagement/venv/lib/python3.7/site-packages/pymongo/database.py", line 715, in collection_names
    nameOnly=True, **kws)]
  File "/Users/ronald/dev/clients/qc/datamanagement/venv/lib/python3.7/site-packages/pymongo/database.py", line 674, in list_collections
    read_pref) as (sock_info, slave_okay):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/contextlib.py", line 112, in __enter__
    return next(self.gen)
  File "/Users/ronald/dev/clients/qc/datamanagement/venv/lib/python3.7/site-packages/pymongo/mongo_client.py", line 1099, in _socket_for_reads
    server = topology.select_server(read_preference)
  File "/Users/ronald/dev/clients/qc/datamanagement/venv/lib/python3.7/site-packages/pymongo/topology.py", line 224, in select_server
    address))
  File "/Users/ronald/dev/clients/qc/datamanagement/venv/lib/python3.7/site-packages/pymongo/topology.py", line 183, in select_servers
    selector, server_timeout, address)
  File "/Users/ronald/dev/clients/qc/datamanagement/venv/lib/python3.7/site-packages/pymongo/topology.py", line 199, in _select_servers_loop
    self._error_message(selector))
pymongo.errors.ServerSelectionTimeoutError: ipaddress:27017: timed out

0 个答案:

没有答案