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
1条答案
按热度按时间rsaldnfx1#
连接字符串中的url
cluster0.zwa0u.mongodb.net
不使用dns解析;您需要更正url。