[英]Connecting to mongo atlas gives pymongo.errors.ServerSelectionTimeoutError: localhost:27017
I am trying to connect to mongoDB Atlas from my Django application using models.我正在尝试使用模型从我的 Django 应用程序连接到 mongoDB Atlas。 I have changed the database configuration in settings as below我在设置中更改了数据库配置,如下所示
DATABASES = {
'default': {
'ENGINE': "djongo",
'NAME': "test",
'HOST': "mongodb+srv://admin:<mypassword>@cluster0-9jtq2.mongodb.net/test?retryWrites=true&w=majority",
'USER': "admin",
'PASSWORD': "<mypassword>",
}
}
Now when i execute migrate现在当我执行迁移
# python3.7 manage.py migrate
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 "/usr/local/lib/python3.7/dist-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.7/dist-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python3.7/dist-packages/django/core/management/base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python3.7/dist-packages/django/core/management/base.py", line 364, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python3.7/dist-packages/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/django/core/management/commands/migrate.py", line 87, in handle
executor = MigrationExecutor(connection, self.migration_progress_callback)
File "/usr/local/lib/python3.7/dist-packages/django/db/migrations/executor.py", line 18, in __init__
self.loader = MigrationLoader(self.connection)
File "/usr/local/lib/python3.7/dist-packages/django/db/migrations/loader.py", line 49, in __init__
self.build_graph()
File "/usr/local/lib/python3.7/dist-packages/django/db/migrations/loader.py", line 212, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "/usr/local/lib/python3.7/dist-packages/django/db/migrations/recorder.py", line 73, in applied_migrations
if self.has_table():
File "/usr/local/lib/python3.7/dist-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 "/usr/local/lib/python3.7/dist-packages/django/db/backends/base/introspection.py", line 48, in table_names
return get_names(cursor)
File "/usr/local/lib/python3.7/dist-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 "/usr/local/lib/python3.7/dist-packages/djongo/introspection.py", line 47, in get_table_list
for c in cursor.db_conn.list_collection_names()
File "/usr/local/lib/python3.7/dist-packages/pymongo/database.py", line 856, in list_collection_names
for result in self.list_collections(session=session, **kwargs)]
File "/usr/local/lib/python3.7/dist-packages/pymongo/database.py", line 819, in list_collections
_cmd, read_pref, session)
File "/usr/local/lib/python3.7/dist-packages/pymongo/mongo_client.py", line 1454, in _retryable_read
read_pref, session, address=address)
File "/usr/local/lib/python3.7/dist-packages/pymongo/mongo_client.py", line 1253, in _select_server
server = topology.select_server(server_selector)
File "/usr/local/lib/python3.7/dist-packages/pymongo/topology.py", line 235, in select_server
address))
File "/usr/local/lib/python3.7/dist-packages/pymongo/topology.py", line 193, in select_servers
selector, server_timeout, address)
File "/usr/local/lib/python3.7/dist-packages/pymongo/topology.py", line 209, in _select_servers_loop
self._error_message(selector))
pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused
I am not sure why it's connecting to localhost when i mentioned the Atlas URL in the HOST
variable.当我在HOST
变量中提到 Atlas URL 时,我不确定它为什么要连接到 localhost。
When i connect to the same DB from command line i get the correct response.当我从命令行连接到同一个数据库时,我得到了正确的响应。
# python3.7
Python 3.7.7 (default, Mar 10 2020, 17:25:08)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pymongo
>>>
>>> client = pymongo.MongoClient('mongodb+srv://admin:<mypassword>@cluster0-9jtq2.mongodb.net/test?retryWrites=true&w=majority')
>>>
>>> client.test123
Database(MongoClient(host=['cluster0-shard-00-01-9jtq2.mongodb.net:27017', 'cluster0-shard-00-02-9jtq2.mongodb.net:27017', 'cluster0-shard-00-00-9jtq2.mongodb.net:27017'], document_class=dict, tz_aware=False, connect=True, retrywrites=True, w='majority', authsource='admin', replicaset='Cluster0-shard-0', ssl=True), 'test123')
Something needs to be set at the Django app level so that it connects to the proper URL rather than localhost?需要在 Django 应用程序级别设置一些东西,以便它连接到正确的 URL 而不是 localhost?
Thanks @Daniel F for the pointer.感谢@Daniel F 的指点。
Yes, below link helped in solving the issue是的,下面的链接有助于解决问题
https://github.com/nesdis/djongo/issues/132#issuecomment-432577860
In addition to that, need to Whitelist(in Atlas network settings) the "Access from anywhere" at the cluster level to allow connections.除此之外,需要在集群级别将“从任何地方访问”列入白名单(在 Atlas 网络设置中)以允许连接。
DATABASES = {
'default': {
'ENGINE': 'djongo',
'NAME': 'test',
'CLIENT': {
'host': 'mongodb+srv://<username>:<password>@<yourdomain>.mongodb.net/test?retryWrites=true&w=majority',
'port': 27017,
'username': 'youruser',
'password': 'yourdbpass',
}
},
}
I found this solution from Django fails to connect to remote MongoDB server using djongo我发现Django 的这个解决方案无法使用 djongo 连接到远程 MongoDB 服务器
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.