我有python 2.7,有pymongo 3.4
我试图使用以下内容连接到mongo地图集:
import certifi
import ssl
import os
from pymongo import MongoClient
MongoClient(
"mongodb://Admin:<PASSWORD>@ABC-shard-00-00-XYZ:27017",
"ABC-shard-00-01-XYZ:27017",
"ABC-shard-00-02-XYZ:27017/dev?ssl=true&replicaSet=ABC-0&authSource=admin",
ssl_cert_reqs=ssl.CERT_REQUIRED,
ssl_ca_certs=certifi.where()
)
它一直失败,身份验证失败。代码18
答案 0 :(得分:3)
如Using PyMongo with MongoDB Atlas中所述:
从PyMongo v3.4.0开始,您可以通过将MongoDB Atlas提供的MongoDB Atlas传递给Connection String URI来连接到MongoClient,例如:
from pymongo import MongoClient
client = MongoClient("mongodb://USER:PASSWORD@ABC-cluster-shard-00-00-XYZ.mongodb.net:27017,ABC-cluster-shard-00-01-XYZ.mongodb.net:27017,ABC-cluster-shard-00-02-XYZ.mongodb.net:27017/DATABASE?ssl=true&replicaSet=ABC-cluster-shard-0&authSource=admin")
与MongoDB Atlas的连接需要TLS/SSL。使用PyMongo 3.3+,您可以使用以下Python pip命令安装PyMongo 3.3+和任何与TLS / SSL相关的依赖项:
python -m pip install pymongo[tls]