我正在尝试使用SRV方案连接到我的MongoDB集群,并且在尝试连接时遇到问题。 下面是我尝试过的代码
from motor import motor_asyncio
motor_client = motor_asyncio.AsyncIOMotorClient('mongodb+srv://<user>:<password>@examplecluster-ece7n.azure.mongodb.net/test?retryWrites=true&w=majority')
_db = self.motor_client.translations
_collection = _db.translations_collections
我已经安装了dnspython模块,但这是仍然弹出的错误:
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/libneko/extras/superuser.py", line 176, in execute_in_session
result = await locals()["aexec"](ctx, ctx.bot)
File "<string>", line 4, in aexec
File "/usr/local/lib/python3.7/site-packages/motor/core.py", line 141, in __init__
delegate = self.__delegate_class__(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/pymongo/mongo_client.py", line 524, in __init__
res = uri_parser.parse_uri(entity, port, warn=True)
File "/usr/local/lib/python3.7/site-packages/pymongo/uri_parser.py", line 318, in parse_uri
raise ConfigurationError('The "dnspython" module must be '
pymongo.errors.ConfigurationError: The "dnspython" module must be installed to use mongodb+srv:// URIs
它应该可以按照我的预期成功连接到集群,并且只需指定两点即可
答案 0 :(得分:0)
我遇到了完全相同的问题,结果证明我在项目中的一个文件名为dns.py
。因此,Python将使用我的文件而不是dns库,因此出现此错误。
重命名可以解决该问题。
希望这会有所帮助。
答案 1 :(得分:0)
在MongoClient()URI中,只需指定mongodb而不是mongodb + srv即可: 只需将占位符字段(即password和dbname)替换为您的集群密码和数据库名称 或者,如果上述方法不起作用,请尝试安装python -m pip install pymongo [srv]此命令,然后重新启动您使用的任何IDE 希望对您有帮助