Docs Home → Develop Applications → Python Drivers → PyMongo
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
Overview
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.
Sample Application
You can use the following sample application to test the code examples on this page. To use the sample application, perform the following steps:
Ensure you have PyMongo installed.
Copy the following code and paste it into a new
.py
file.Copy a code example from this page and paste it on the specified lines in the file.
1 from pymongo import MongoClient 2 3 try: 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 15 except Exception as e: 16 raise Exception( 17 "The following error occurred: ", e)
Connection
Local Deployment
uri = "mongodb://localhost:27017/" client = MongoClient(uri)
Atlas
uri = "<Atlas connection string>" client = MongoClient(uri, server_api=pymongo.server_api.ServerApi( version="1", strict=True, deprecation_errors=True))
Replica Set
uri = "mongodb://<replica set member>:<port>/?replicaSet=<replica set name>" client = MongoClient(uri)
Transport Layer Security (TLS)
Enable TLS
To learn more about enabling TLS, see Enable TLS in the TLS configuration guide.
Specify a Certificate Authority (CA) File
To learn more about specifying a CA file, see Specify a CA File in the TLS configuration guide.
Disable OCSP Checks
To learn more about disabling OCSP checks, see OCSP in the TLS configuration guide.
Specify a Certificate Revocation List (CRL)
To learn more about specifying a CRL, see Certificate Revocation List in the TLS configuration guide.
Present a Client Certificate
To learn more about specifying a client certificate, see Present a Client Certificate in the TLS configuration guide.
Provide a Certificate Key File Password
To learn more about providing a key file password, see Provide a Key Password in the TLS configuration guide.
Allow Insecure TLS
To learn more about allowing insecure TLS, see Allow Insecure TLS in the TLS configuration guide.
Disable Certificate Validation
To learn more about disabling certificate validation, see Allow Insecure TLS in the TLS configuration guide.
Disable Hostname Verification
To learn more about disabling hostname verification, see Allow Insecure TLS in the TLS configuration guide.
Network Compression
Compression Algorithms
To learn more about specifying compression algorithms, see Specify Compression Algorithms in the Network Compression guide.
zlib Compression Level
To learn more about setting the zlib compression level, see Specify Compression Algorithms in the Network Compression guide.
Server Selection
client = pymongo.MongoClient("mongodb://<username>:<password>@<hostname>:<port>", server_selector=<selector function>)
To learn more about customizing server selection, see Customize Server Selection.
Stable API
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.
Limit Server Execution Time
timeout Block
with pymongo.timeout(<timeout length>): # perform operations here
To learn more about client-side timeouts, see Limit Server Execution Time.
timeoutMS Connection Option
To learn more about client-side timeouts, see Limit Server Execution Time.