Djongo 没有连接到 mlab 服务器
Posted
技术标签:
【中文标题】Djongo 没有连接到 mlab 服务器【英文标题】:Djongo not connecting to mlab server 【发布时间】:2020-07-07 08:36:00 【问题描述】:我正在使用 djongo 和 django2 连接到 mlab mongodb 实例。
settings.py
'default':
'ENGINE': 'djongo',
'NAME': 'neon',
'HOST': 'mongodb://username:password@ds249605.mlab.com:49605/mlab1',
虽然我正在使用这个设置,但 PyMongo 连接到本地的 mongodb 实例。
log
Traceback (most recent call last):
File "/usr/lib/python3.7/threading.py", line 926, in _bootstrap_inner
self.run()
File "/usr/lib/python3.7/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "/home/rahul/projects/django/neon/lib/python3.7/site-packages/django/utils/autoreload.py", line 54, in wrapper
fn(*args, **kwargs)
File "/home/rahul/projects/django/neon/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 120, in inner_run
self.check_migrations()
File "/home/rahul/projects/django/neon/lib/python3.7/site-packages/django/core/management/base.py", line 453, in check_migrations
executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
File "/home/rahul/projects/django/neon/lib/python3.7/site-packages/django/db/migrations/executor.py", line 18, in __init__
self.loader = MigrationLoader(self.connection)
File "/home/rahul/projects/django/neon/lib/python3.7/site-packages/django/db/migrations/loader.py", line 49, in __init__
self.build_graph()
File "/home/rahul/projects/django/neon/lib/python3.7/site-packages/django/db/migrations/loader.py", line 212, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "/home/rahul/projects/django/neon/lib/python3.7/site-packages/django/db/migrations/recorder.py", line 73, in applied_migrations
if self.has_table():
File "/home/rahul/projects/django/neon/lib/python3.7/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/rahul/projects/django/neon/lib/python3.7/site-packages/django/db/backends/base/introspection.py", line 48, in table_names
return get_names(cursor)
File "/home/rahul/projects/django/neon/lib/python3.7/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/rahul/projects/django/neon/lib/python3.7/site-packages/djongo/introspection.py", line 47, in get_table_list
for c in cursor.db_conn.list_collection_names()
File "/home/rahul/projects/django/neon/lib/python3.7/site-packages/pymongo/database.py", line 856, in list_collection_names
for result in self.list_collections(session=session, **kwargs)]
File "/home/rahul/projects/django/neon/lib/python3.7/site-packages/pymongo/database.py", line 819, in list_collections
_cmd, read_pref, session)
File "/home/rahul/projects/django/neon/lib/python3.7/site-packages/pymongo/mongo_client.py", line 1454, in _retryable_read
read_pref, session, address=address)
File "/home/rahul/projects/django/neon/lib/python3.7/site-packages/pymongo/mongo_client.py", line 1253, in _select_server
server = topology.select_server(server_selector)
File "/home/rahul/projects/django/neon/lib/python3.7/site-packages/pymongo/topology.py", line 235, in select_server
address))
File "/home/rahul/projects/django/neon/lib/python3.7/site-packages/pymongo/topology.py", line 193, in select_servers
selector, server_timeout, address)
File "/home/rahul/projects/django/neon/lib/python3.7/site-packages/pymongo/topology.py", line 209, in _select_servers_loop
self._error_message(selector))
pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused
【问题讨论】:
nesdis.github.io/djongo/integrating-django-with-mongodb 中列出的配置显示host
应该在CLIENT
中,如果你使用它可以吗?
好的,我会尝试并告诉你。
是的,这行得通。非常感谢
【参考方案1】:
mLab
主机是这样在客户端里面的
DATABASES =
'default':
'ENGINE': 'djongo',
'NAME': 'fitbitt',
'CLIENT':
'host':'mongodb://your_username:your_password@ds363008.mlab.com:63008/fitbitt',
'username':'your_username',
'password':'your_password'
【讨论】:
以上是关于Djongo 没有连接到 mlab 服务器的主要内容,如果未能解决你的问题,请参考以下文章
Heroku部署后Django(djongo)无法连接到MondoDB Atlas