簡體   English   中英

連接到 mongo 圖集給出 pymongo.errors.ServerSelectionTimeoutError: localhost:27017

[英]Connecting to mongo atlas gives pymongo.errors.ServerSelectionTimeoutError: localhost:27017

我正在嘗試使用模型從我的 Django 應用程序連接到 mongoDB Atlas。 我在設置中更改了數據庫配置,如下所示

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

當我在HOST變量中提到 Atlas URL 時,我不確定它為什么要連接到 localhost。

當我從命令行連接到同一個數據庫時,我得到了正確的響應。

# 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?

感謝@Daniel F 的指點。

是的,下面的鏈接有助於解決問題

https://github.com/nesdis/djongo/issues/132#issuecomment-432577860

除此之外,需要在集群級別將“從任何地方訪問”列入白名單(在 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',
    } 
  },
}

我發現Django 的這個解決方案無法使用 djongo 連接到遠程 MongoDB 服務器

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM