【问题标题】:Pymongo: None of DNS query names existPymongo:不存在任何 DNS 查询名称
【发布时间】:2023-01-31 13:51:43
【问题描述】:

我正在做一个同事交给我的项目。拉取存储库后我无法运行,出现错误

Exception has occurred: ConfigurationError
None of DNS query names exist: _mongodb._tcp.stage.xxxx.mongodb.net., _mongodb._tcp.stage.xxxx.mongodb.net.xxxx.com., _mongodb._tcp.stage.xxxx.mongodb.net.hsd1.tx.comcast.net., _mongodb._tcp.stage.xxxx.mongodb.net.tx.comcast.net., _mongodb._tcp.stage.xxxx.mongodb.net.comcast.net.

奇怪的是,这个回购协议对我的同事来说工作得很好。他有相同的 python 版本,相同的依赖项/等等,没有问题。下面是requirements.txt

fastapi==0.65.2 
uvicorn==0.11.7
gunicorn==20.0.4
elastic-apm==5.8.0
pymongo[srv]==3.12.0
pydantic==1.7.4
singleton-decorator==1.0.0
requests==2.27.1
requests-oauthlib==1.3.1
oauthlib==3.2.0
python-multipart==0.0.5
cachetools==5.0.0
pytz==2022.1
python-dotenv==0.14.0

我正在使用 Python 3.8.6,也尝试使用 3.9.10 但同样的问题。

这正是我出错的地方。

@singleton
class MongoDB:
    """MongoDB Class"""

       def __init__(self, config: MongoDbConfig):
            """Initial MongoDB Connection on collection level"""
            _uri = "mongodb+srv://{username}:{password}@{host}/{db}?authSource=admin&replicaSet={replica_set}&readPreference=primary&connectTimeoutMS={timeout}&authMechanism=SCRAM-SHA-1".format(
                username=config.username,
                password=config.password,
                host=config.host,
                db=config.db,
                replica_set=config.replicate_set,
                timeout=1000,
                maxPoolSize=config.max_pool,
            )
    
            self.client = MongoClient(_uri)

当我安装 dnspython==2.2.1 时,我的错误减少到

The DNS query name does not exist: _mongodb._tcp.stage.XXXX.mongodb.net.

我的环境变量和系统变量与我的同事完全一致。我们目前都在远程工作。我尝试了 google dns 修复,但它似乎没有用。关于我应该如何解决这个问题的任何建议/提示。我完全迷路了。

【问题讨论】:

    标签: python mongodb dns pymongo


    【解决方案1】:

    在运行应用程序的服务器上,在文件/etc/hosts 中,您可以为应该存在 mongodb 的主机添加条目,例如:

    <IP_of_mongodb_server> _mongodb._tcp.stage.XXXX.mongodb.net
    

    这只是一个解决方法,它将明确地将 _mongodb._tcp.stage.XXXX.mongodb.net 指向您在文件中设置的 IP ...


    最好的选择是在 mngodb.net 网站上将公共 IP 设置为 DNS 条目。可以在 MongoDB 社区找到相关信息。示例:https://www.mongodb.com/community/forums/t/mongo-connection-test-dns-name-does-not-exist/86048https://www.mongodb.com/community/forums/t/dnshostnotfound-failed-to-look-up-service/109605

    更新:

    由于上述论坛链接似乎已在论坛中转换为私有链接,这里提供另一个DNS 故障排除来源:https://www.mongodb.com/docs/atlas/troubleshoot-connection/#internet-service-provider-dns-blocks-connection-string

    或者尝试搜索上述论坛链接的 Google) 缓存版本:http://webcache.googleusercontent.com/search?q=cache:zX8_TX_F_R8J:https://www.mongodb.com/community/forums/t/mongo-connection-test-dns-name-does-not-exist/86048&hl=en&gl=bg&strip=1&vwsrc=0

    【讨论】:

    • 这两个都是403死链接:“糟糕!那个页面是私人的。”
    • 答案已更新。但总的来说,设置公共 DNS 的行为在 Internet 上都有很好的记录……而不是 MongoDB 特定的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-02
    相关资源
    最近更新 更多