文档菜单
文档首页
/ / /
PyMongo

连接到 MongoDB

本页内容

  • 概述
  • 示例应用
  • 连接
  • 本地部署
  • Atlas
  • 副本集
  • 传输层安全性(TLS)
  • 启用 TLS
  • 指定证书颁发机构(CA)文件
  • 禁用 OCSP 检查
  • 指定证书吊销列表(CRL)
  • 提供客户端证书
  • 提供证书密钥文件密码
  • 允许不安全的 TLS
  • 禁用证书验证
  • 禁用主机名验证
  • 网络压缩
  • 压缩算法
  • zlib 压缩级别
  • 服务器选择
  • 稳定 API
  • 限制服务器执行时间
  • 超时块
  • timeoutMS 连接选项

本页面包含代码示例,展示如何使用各种设置将您的 Python 应用程序连接到 MongoDB。

提示

要了解更多关于本页面上的连接选项,请参阅每个部分提供的链接。

要使用本页面上的连接示例,请将代码示例复制到示例应用或您自己的应用程序中。请确保替换代码示例中的所有占位符,例如<hostname>,以适用于您的 MongoDB 部署的相关值。

您可以使用以下示例应用来测试本页面上的代码示例。要使用示例应用,请执行以下步骤

  1. 确保您已安装 PyMongo。

  2. 复制以下代码,并将其粘贴到一个新的 .py 文件中。

  3. 从本页面复制一个代码示例,并将其粘贴到文件中的指定行。

1from pymongo import MongoClient
2
3try:
4 # start example code here
5
6 # end example code here
7
8 client.admin.command("ping")
9 print("Connected successfully")
10
11 # other application code
12
13 client.close()
14
15except Exception as e:
16 raise Exception(
17 "The following error occurred: ", e)
uri = "mongodb://localhost:27017/"
client = MongoClient(uri)
uri = "<Atlas connection string>"
client = MongoClient(uri, server_api=pymongo.server_api.ServerApi(
version="1", strict=True, deprecation_errors=True))
uri = "mongodb://<replica set member>:<port>/?replicaSet=<replica set name>"
client = MongoClient(uri)
client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname:<port>", tls=True)
client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>?tls=true")

要了解更多关于启用 TLS 的信息,请参阅TLS 配置指南中的启用 TLS

client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
tls=True,
tlsCAFile="/path/to/ca.pem")
uri = "mongodb://<db_username>:<db_password>@<hostname>:<port>/?tls=true&tlsCAFile=/path/to/ca.pem"
client = pymongo.MongoClient(uri)

要了解更多关于指定 CA 文件的信息,请参阅 TLS 配置指南中的指定 CA 文件

client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
tls=True,
tlsDisableOCSPEndpointCheck=True)
uri = "mongodb://example.com/?tls=true&tlsDisableOCSPEndpointCheck=true"
client = pymongo.MongoClient(uri)

要了解如何禁用OCSP检查,请参阅TLS配置指南中的OCSP

client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
tls=True,
tlsCRLFile="/path/to/crl.pem")
uri = "mongodb://example.com/?tls=true&tlsCRLFile=/path/to/crl.pem"
client = pymongo.MongoClient(uri)

要了解如何指定CRL,请参阅TLS配置指南中的证书吊销列表

client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
tls=True,
tlsCertificateKeyFile='/path/to/client.pem')
uri = ("mongodb://<db_username>:<db_password>@<hostname:<port>/?"
"tls=true"
"&tlsCertificateKeyFile=path/to/client.pem")
client = pymongo.MongoClient(uri)

要了解如何指定客户端证书,请参阅TLS配置指南中的呈现客户端证书

client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname:<port>",
tls=True,
tlsCertificateKeyFile='/path/to/client.pem',
tlsCertificateKeyFilePassword=<passphrase>)
uri = ("mongodb://<db_username>:<db_password>@<hostname:<port>/?"
"tls=true"
"&tlsCertificateKeyFile=path/to/client.pem"
"&tlsCertificateKeyFilePassword=<passphrase>")
client = pymongo.MongoClient(uri)

要了解如何提供密钥文件密码,请参阅TLS配置指南中的提供密钥密码

client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname:<port>",
tls=True,
tlsInsecure=True)
uri = ("mongodb://<db_username>:<db_password>@<hostname>:<port>/?"
"tls=true"
"&tlsInsecure=true")
client = pymongo.MongoClient(uri)

要了解更多关于允许不安全TLS的信息,请参阅TLS配置指南中的允许不安全的TLS

client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
tls=True,
tlsAllowInvalidCertificates=True)
uri = ("mongodb://<db_username>:<db_password>@<hostname>:<port>/?"
"tls=true"
"&tlsAllowInvalidCertificates=true")
client = pymongo.MongoClient(uri)

要了解更多关于禁用证书验证的信息,请参阅TLS配置指南中的允许不安全的TLS

client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
tls=True,
tlsAllowInvalidHostnames=True)
uri = ("mongodb://<db_username>:<db_password>@<hostname>:<port>/?"
"tls=true"
"&tlsAllowInvalidHostnames=true")
client = pymongo.MongoClient(uri)

要了解更多关于禁用主机名验证的信息,请参阅TLS配置指南中的允许不安全的TLS

client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
compressors = "snappy,zstd,zlib")
uri = ("mongodb://<db_username>:<db_password>@<hostname>:<port>/?"
"compressors=snappy,zstd,zlib")
client = pymongo.MongoClient(uri)

要了解更多关于指定压缩算法的信息,请参阅网络压缩指南中的指定压缩算法

client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
compressors = "zlib",
zlibCompressionLevel=<zlib compression level>)
uri = ("mongodb://<db_username>:<db_password>@<hostname>:<port>/?"
"compressors=zlib"
"zlibCompressionLevel=<zlib compression level>")
client = pymongo.MongoClient(uri)

要了解更多关于设置 zlib 压缩级别的信息,请参阅网络压缩指南中的指定压缩算法

client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
server_selector=<selector function>)

要了解更多关于自定义服务器选择的信息,请参阅自定义服务器选择

from pymongo.server_api import ServerApi
client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname:<port>",
server_api=ServerApi("<Stable API version>"))

要了解更多关于稳定API的信息,请参阅稳定API。

with pymongo.timeout(<timeout length>):
# perform operations here

要了解更多关于客户端超时信息,请参阅限制服务器执行时间。

client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname@:<port>",
timeoutMS=<timeout length>)
uri = "mongodb://<db_username>:<db_password>@<hostname:<port>/?timeoutMS=<timeout length>"
client = pymongo.MongoClient(uri)

要了解更多关于客户端超时信息,请参阅限制服务器执行时间。

返回

下一步