从mongodb dns错误访问数据

gt0wga4j  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(469)
import pymongo 
from pymongo import MongoClient
cluster = MongoClient("mongodb+srv://user1:user11@cluster0.zwa0u.mongodb.net/test?retryWrites=true&w=majority")
db = cluster["test"]
collection = db["test"]
post = {"_id" : 0, "name" : "Jon", "score" : 5}
collection.insert_one(post)

运行这段代码时,我得到的dns错误和这么多。但是数据没有上传到数据库。下面的错误是在我运行上述代码时发生的

> Traceback (most recent call last):   File
> "C:\CompanyWork\ProgramsPython-2\lib\site-packages\pymongo\srv_resolver.py",
> line 72, in _resolve_uri
>     results = resolver.query('_mongodb._tcp.' + self.__fqdn, 'SRV',   File
> "C:\CompanyWork\ProgramsPython-2\lib\site-packages\dns\resolver.py",
> line 1321, in query
>     return resolve(qname, rdtype, rdclass, tcp, source,   File "C:\CompanyWork\ProgramsPython-2\lib\site-packages\dns\resolver.py",
> line 1305, in resolve
>     return get_default_resolver().resolve(qname, rdtype, rdclass, tcp, source,   File
> "C:\CompanyWork\ProgramsPython-2\lib\site-packages\dns\resolver.py",
> line 1176, in resolve
>     timeout = self._compute_timeout(start, lifetime)   File "C:\CompanyWork\ProgramsPython-2\lib\site-packages\dns\resolver.py",
> line 997, in _compute_timeout
>     raise Timeout(timeout=duration) dns.exception.Timeout: The DNS operation timed out after 21.213911056518555 seconds
> 
> During handling of the above exception, another exception occurred:
> 
> Traceback (most recent call last):   File
> "C:\CompanyWork\ProgramsPython-2\mongodbAccess.py", line 23, in
> <module>
>     cluster = MongoClient("mongodb+srv://user1:user11@cluster0.zwa0u.mongodb.net/test?retryWrites=true&w=majority")
> File
> "C:\CompanyWork\ProgramsPython-2\lib\site-packages\pymongo\mongo_client.py",
> line 639, in __init__
>     res = uri_parser.parse_uri(   File "C:\CompanyWork\ProgramsPython-2\lib\site-packages\pymongo\uri_parser.py",
> line 500, in parse_uri
>     nodes = dns_resolver.get_hosts()   File "C:\CompanyWork\ProgramsPython-2\lib\site-packages\pymongo\srv_resolver.py",
> line 102, in get_hosts
>     _, nodes = self._get_srv_response_and_hosts(True)   File "C:\CompanyWork\ProgramsPython-2\lib\site-packages\pymongo\srv_resolver.py",
> line 83, in _get_srv_response_and_hosts
>     results = self._resolve_uri(encapsulate_errors)   File "C:\CompanyWork\ProgramsPython-2\lib\site-packages\pymongo\srv_resolver.py",
> line 79, in _resolve_uri
>     raise ConfigurationError(str(exc)) pymongo.errors.ConfigurationError: The DNS operation timed out after
> 21.213911056518555 seconds
rsaldnfx

rsaldnfx1#

连接字符串中的url cluster0.zwa0u.mongodb.net 不使用dns解析;您需要更正url。

$ dig zwa0u.mongodb.net

; <<>> DiG 9.16.1-Ubuntu <<>> zwa0u.mongodb.net
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 46955
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;zwa0u.mongodb.net.             IN      A

;; AUTHORITY SECTION:
mongodb.net.            49      IN      SOA     ns-761.awsdns-31.net. awsdns-hostmaster.amazon.com. 1 7200 900 1209600 60

;; Query time: 40 msec
;; SERVER: 192.168.176.1#53(192.168.176.1)
;; WHEN: Tue Apr 27 17:36:14 BST 2021
;; MSG SIZE  rcvd: 127

相关问题