pymongo 3.4.0连接到mongo atlas

时间:2017-08-07 11:53:49

标签: mongodb python-2.7 pymongo mongodb-atlas

我有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

1 个答案:

答案 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]