Docs Menu

Docs HomeDevelop ApplicationsPython DriversPyMongo

Connect to MongoDB

On this page

  • Overview
  • Sample Application
  • Connection
  • Local Deployment
  • Atlas
  • Replica Set
  • Transport Layer Security (TLS)
  • Enable TLS
  • Specify a Certificate Authority (CA) File
  • Disable OCSP Checks
  • Specify a Certificate Revocation List (CRL)
  • Present a Client Certificate
  • Provide a Certificate Key File Password
  • Allow Insecure TLS
  • Disable Certificate Validation
  • Disable Hostname Verification
  • Network Compression
  • Compression Algorithms
  • zlib Compression Level
  • Server Selection
  • Stable API
  • Limit Server Execution Time
  • timeout Block
  • timeoutMS Connection Option

This page contains code examples that show how to connect your Python application to MongoDB with various settings.

Tip

To learn more about the connection options on this page, see the link provided in each section.

To use a connection example from this page, copy the code example into the sample application or your own application. Be sure to replace all placeholders in the code examples, such as <hostname>, with the relevant values for your MongoDB deployment.

You can use the following sample application to test the code examples on this page. To use the sample application, perform the following steps:

  1. Ensure you have PyMongo installed.

  2. Copy the following code and paste it into a new .py file.

  3. Copy a code example from this page and paste it on the specified lines in the file.

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)

To learn more about enabling TLS, see Enable TLS in the TLS configuration guide.

To learn more about specifying a CA file, see Specify a CA File in the TLS configuration guide.

To learn more about disabling OCSP checks, see OCSP in the TLS configuration guide.

To learn more about specifying a CRL, see Certificate Revocation List in the TLS configuration guide.

To learn more about specifying a client certificate, see Present a Client Certificate in the TLS configuration guide.

To learn more about providing a key file password, see Provide a Key Password in the TLS configuration guide.

To learn more about allowing insecure TLS, see Allow Insecure TLS in the TLS configuration guide.

To learn more about disabling certificate validation, see Allow Insecure TLS in the TLS configuration guide.

To learn more about disabling hostname verification, see Allow Insecure TLS in the TLS configuration guide.

To learn more about specifying compression algorithms, see Specify Compression Algorithms in the Network Compression guide.

To learn more about setting the zlib compression level, see Specify Compression Algorithms in the Network Compression guide.

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

To learn more about customizing server selection, see Customize Server Selection.

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

To learn more about the Stable API, see Stable API.

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

To learn more about client-side timeouts, see Limit Server Execution Time.

To learn more about client-side timeouts, see Limit Server Execution Time.

← Next Steps