简体   繁体   English

从Docker连接到MongoDB Cloud时出错

[英]Error connecting to MongoDB Cloud from Docker

I tested the application on my laptop, my server and another laptop. 我在笔记本电脑,服务器和另一台笔记本电脑上测试了应用程序

I changed the DNS each time but this is the problem I am having: 我每次都更改了DNS,但这是我遇到的问题:

pymongo.errors.ConfigurationError: None of DNS query names exist: _mongodb._tcp.xxxxxx-shard-00-00-uzn6y.mongodb.net., _mongodb._tcp.xxxxxx-shard-00-00-uzn6y.mongodb.net.

The line generating this error is: 生成此错误的行是:

myclient = MongoClient(con_string)

and the con_stirng is: 而con_stirng是:

mongodb+srv://root:<password>@xxxxxx-xxxxxx.mongodb.net/test?retryWrites=true

I also tried without "srv": 我也试过没有“srv”:

mongodb://root:<password>@xxxxxx-xxxxxx.mongodb.net/test?retryWrites=true

All of this is running in a Docker container and I am using Flask. 所有这些都在Docker容器中运行,我正在使用Flask。 When I run the application without Docker, the DNS is resolved, however, this is not the case when I run it on a container. 当我在没有Docker的情况下运行应用程序时,DNS已解析,但是,当我在容器上运行它时,情况并非如此。

These are what I tried until now: 这些是我到现在为止所尝试的:

docker run -it -p 5000:5000 --env-file vars.env myapp

docker run -it -p 5000:5000  --dns=8.8.8.8 --env-file vars.env myapp

docker network create demo && docker run -it -p 5000:5000  --network demo --env-file vars.env myapp

docker run -it -p 5000:5000  --network demo --volumes /etc/resolv.conf:/etc/resolv.conf --env-file vars.env myapp

docker run -it -p 5000:5000 --volumes /etc/resolv.conf:/etc/resolv.conf --env-file vars.env myapp

None of the above resolved the mongocloud instance address. 以上都没有解决mongocloud实例地址。

This is the full trace: 这是完整的痕迹:

Traceback (most recent call last):
  File "/folder//venv/lib/python3.6/site-packages/pymongo/uri_parser.py", line 287, in _get_dns_srv_hosts
    results = resolver.query('_mongodb._tcp.' + hostname, 'SRV')
  File "/folder//venv/lib/python3.6/site-packages/dns/resolver.py", line 1102, in query
    lifetime)
  File "/folder//venv/lib/python3.6/site-packages/dns/resolver.py", line 1002, in query
    raise NXDOMAIN(qnames=qnames_to_try, responses=nxdomain_responses)
dns.resolver.NXDOMAIN: None of DNS query names exist: _mongodb._tcp.xxxxxx-shard-00-00-xxxxxx.mongodb.net., _mongodb._tcp.xxxxxx-shard-00-00-xxxxxx.mongodb.net.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "main.py", line 63, in <module>
    myclient = MongoClient(con_string)
  File "/folder//venv/lib/python3.6/site-packages/pymongo/mongo_client.py", line 494, in __init__
    res = uri_parser.parse_uri(entity, port, warn=True)
  File "/folder//venv/lib/python3.6/site-packages/pymongo/uri_parser.py", line 399, in parse_uri
    nodes = _get_dns_srv_hosts(fqdn)
  File "/folder//venv/lib/python3.6/site-packages/pymongo/uri_parser.py", line 289, in _get_dns_srv_hosts
    raise ConfigurationError(str(exc))
pymongo.errors.ConfigurationError: None of DNS query names exist: _mongodb._tcp.xxxxxx-shard-00-00-xxxxxx.mongodb.net., _mongodb._tcp.xxxxxx-shard-00-00-xxxxxx.mongodb.net.

I found the solution. 我找到了解决方案。 The connection host string should be: : 连接主机字符串应为:

xxxxxxxxxx-shard-00-00-xxxxxxxxxxxx.mongodb.net:27017,xxxxxxxx-shard-00-01-xxxxxxxx.mongodb.net:27017,xxxxxxxxx-shard-00-02-xxxxxxxxx.mongodb.net:27017/test?ssl=true&replicaSet=xxxxxxxxxxxxxxx-shard-0&authSource=admin&retryWrites=true 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM