Я пытаюсь подключиться к mongoDB Atlas из моего приложения Django, используя модели. Я изменил конфигурацию базы данных в настройках, как показано ниже
DATABASES = {
'default': {
'ENGINE': "djongo",
'NAME': "test",
'HOST': "mongodb+srv://admin:<mypassword>@cluster0-9jtq2.mongodb.net/test?retryWrites=true&w=majority",
'USER': "admin",
'PASSWORD': "<mypassword>",
}
}
Теперь, когда я выполняю миграцию
# 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
, я не уверен, почему он подключается к localhost, когда я упомянул URL-адрес Atlas в HOST
переменная.
Когда я подключаюсь к той же БД из командной строки, я получаю правильный ответ.
# 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')
Что-то нужно настроить на уровне приложения Django, чтобы оно подключалось к нужному URL, а не localhost?