Start United States USA — software PyMongo Tutorial: Testing MongoDB Failover in Your Python App

PyMongo Tutorial: Testing MongoDB Failover in Your Python App

299
0
TEILEN

Learn about Python distributions and drivers for MongoDB that allow you to work with databases and perform common database operations.
Let’s be friends:
Comment (0)
Join the DZone community and get the full member experience.
Python is a powerful and flexible programming language used by millions of developers around the world to build their applications. It comes as no surprise that Python developers commonly leverage MongoDB hosting, the most popular NoSQL database, for their deployments due to its flexible nature and lack of schema requirements.
So, what’s the best way to use MongoDB with Python? PyMongo is a Python distribution containing tools for working with MongoDB, and the recommended Python MongoDB driver. It is a fairly mature driver that supports most of the common operations with the database, and you can check out this tutorial for an introduction to the PyMongo driver.
When deploying in production, it’s highly recommended to setup in a MongoDB replica set configuration so your data is geographically distributed for high availability. It is also recommended that SSL connections be enabled to encrypt the client-database traffic. We often undertake the testing of failover characteristics of various MongoDB drivers to qualify them for production use cases, or when our customers ask us for advice. In this post, we show you how to connect to an SSL-enabled MongoDB replica set configured with self-signed certificates using PyMongo and how to test MongoDB failover behavior in your code.
The first step is to ensure that the right versions of PyMongo and its dependencies are installed. This guide helps you in sorting out the dependencies, and the driver compatibility matrix can be found here.
The mongo_client. MongoClient parameters that are of interest to us are ssl and ss_ca_cert. In order to connect to an SSL-enabled MongoDB endpoint that uses a self-signed certificate, ssl must be set to True and ss_ca_cert must point to the CA certificate file.
If you are a ScaleGrid customer, you can download the CA certificate file for your MongoDB clusters from the ScaleGrid console as shown here:
So, a connection snippet would look like:
If you are using our own self-signed certificates where hostname verification might fail, you will also have to set the ssl_match_hostname parameter to False.

Continue reading...