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:
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 MongoClient2
3 try :4 5
6 7
8 client.admin.command("ping" ) 9 print ("Connected successfully" ) 10
11 12
13 client.close() 14
15 except Exception as e:16 raise Exception( 17 "The following error occurred: " , e)
MongoClient
Connection String
client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname:<port>" , tls=True )
client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>?tls=true" )
To learn more about enabling TLS, see Enable TLS in
the TLS configuration guide.
MongoClient
Connection String
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)
To learn more about specifying a CA file, see Specify a CA File in
the TLS configuration guide.
MongoClient
Connection String
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)
To learn more about disabling OCSP checks, see OCSP in
the TLS configuration guide.
MongoClient
Connection String
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)
To learn more about specifying a CRL, see Certificate Revocation List in
the TLS configuration guide.
MongoClient
Connection String
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)
To learn more about specifying a client certificate, see Present a Client Certificate in
the TLS configuration guide.
MongoClient
Connection String
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)
To learn more about providing a key file password, see Provide a Key Password in
the TLS configuration guide.
MongoClient
Connection String
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)
To learn more about allowing insecure TLS, see Allow Insecure TLS in
the TLS configuration guide.
MongoClient
Connection String
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)
To learn more about disabling certificate validation, see Allow Insecure TLS in
the TLS configuration guide.
MongoClient
Connection String
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)
To learn more about disabling hostname verification, see Allow Insecure TLS in
the TLS configuration guide.