【问题标题】:pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused django to herokupymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection denied django to heroku
【发布时间】:2020-07-09 17:45:35
【问题描述】:

我正在使用 django 和 mongodb atlas 并尝试使用 heroku 进行部署。不知何故,我一直面临pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused 这个错误。

DATABASES = {
    'default': {
        'ENGINE': 'djongo',
        'NAME': 'name',
        'HOST': 'mongodb+srv://user:pass@cluster0-xb9zh.mongodb.net/test?retryWrites=true&w=majority',
        'USER': 'user',
        'PASSWORD': 'pass',
        }
    }

对于 django setting.py,我只是将其从 sqlite 更改为 mongodb。它在我当地完美运行。我怀疑有一个变化是我必须将 mongo_client.py 文件从 localhost 更改为连接字符串,以使我的 mongodb 连接到 atlas 而不是我的本地数据库。不知道这是否与此有关。

class MongoClient(common.BaseObject):
    """
    A client-side representation of a MongoDB cluster.

    Instances can represent either a standalone MongoDB server, a replica
    set, or a sharded cluster. Instances of this class are responsible for
    maintaining up-to-date state of the cluster, and possibly cache
    resources related to this, including background threads for monitoring,
    and connection pools.
    """
    HOST = "mongodb+srv://user:pass@cluster0-xb9zh.mongodb.net/test?retryWrites=true&w=majority"
    PORT = 27017
    # Define order to retrieve options from ClientOptions for __repr__.

【问题讨论】:

    标签: python django mongodb heroku


    【解决方案1】:

    当您没有将运行客户端的 IP 列入白名单时,通常会出现此错误。在此处查看详细信息:

    https://docs.atlas.mongodb.com/security-whitelist/

    【讨论】:

    • 我确实有一个 ip,但它是用于我的本地主机的。我到处都试过了,还是不行。
    • 您帖子中的链接是指向 MongoDB Atlas 的链接。如果将其指向 localhost,则需要在本地下载并运行 MongoDB。
    • 是的,我实际上是在尝试将其链接到 mongodb 地图集。这就是问题所在。
    • 您需要登录 Atlas,确保集群正在运行并且您本地客户端的 IP 地址在集群的白名单地址列表中。服务器选择超时错误是客户端告诉你它无法连接到远程集群。
    【解决方案2】:

    不知道这是否有很大帮助,但你也可以这样做

    DATABASES = {
        'default': {
            'ENGINE': 'djongo',
            'CLIENT': {
                'host': 'mongodb+srv://<URL>',
                'username': 'something',
                'password': 'somepass',
                'authMechanism': 'SCRAM-SHA-1'
            }
        }
    }
    

    这对我有帮助,但对你来说可能不是这样。

    【讨论】:

      猜你喜欢
      • 2021-09-16
      • 2019-10-06
      • 2020-10-18
      • 2021-12-16
      • 1970-01-01
      • 2020-05-16
      • 2018-03-30
      • 2013-01-03
      • 1970-01-01
      相关资源
      最近更新 更多