Connecting to the replica set to query and insert data using a Python client

In this recipe, we will demonstrate how to connect to a replica set using a Python client and how the client would automatically failover to another node in the replica set, should a primary node fail.

Getting ready

Refer to the Connecting to the single node using a Python client recipe as it describes how to set up and install PyMongo, the Python driver for MongoDB. Additionally, a replica set must be up and running. Refer to the Starting multiple instances as part of a replica set recipe for details on how to start the replica set.

How to do it…

  1. Write/copy the following piece of code to replicaset_client.py: (This script is also available for download from the Packt website.)
    from __future__ import print_function
    import pymongo
    import time
    
    # Instantiate MongoClient with a list of server addresses
    client = pymongo.MongoClient(['localhost:27002', 'localhost:27001', 'localhost:27000'], replicaSet='repSetTest')
    
    # Select the collection and drop it before using
    collection = client.test.repTest
    collection.drop()
    
    #insert a record in
    collection.insert_one(dict(name='Foo', age='30'))
    
    for x in range(5):
        try:
            print('Fetching record: %s' % collection.find_one())
        except Exception as e:
            print('Could not connect to primary')
        time.sleep(3)
  2. Connect to any of the nodes in the replica set, say to localhost:27000, and execute rs.status() from the shell. Take a note of the primary instance in the replica set and connect to it from the shell, if localhost:27000 is not a primary. Here, switch to the administrator database as follows:
    > repSetTest:PRIMARY>use admin
    
  3. We now execute the preceding script from the operating system shell as follows:
    $ python replicaset_client.py
    
  4. Shut down the primary instance by executing the following on the mongo shell that is connected to the primary:
    > repSetTest:PRIMARY> db.shutdownServer()
    
  5. Watch the output on the console where the Python script is executed.

How it works…

You will notice that, in this script, we instantiated the mongo client by giving a list of hosts instead of a single host. As of version 3.0, the pymongo driver's MongoClient() class can accept either a list of hosts or a single host during initialization and deprecate MongoReplicaSetClient(). The client will attempt to connect to the first host in the list, and if successful, will be able to determine the other nodes in the replica set. We are also passing the replicaSet='repSetTest' parameter exclusively, ensuring that the client checks whether the connected node is a part of this replica set.

Once connected, we perform normal database operations such as selecting the test database, dropping the repTest collection, and inserting a single document into the collection.

Following this, we enter a conditional for loop, iterating five times. Each time, we fetch the record, display it, and sleep for three seconds. While the script is in this loop, we shut down the primary node in the replica set as mentioned in step 4. We should see an output similar to this:

Fetching record: {u'age': u'30', u'_id': ObjectId('5558bfaa0640fd1923fce1a1'), u'name': u'Foo'}
Fetching record: {u'age': u'30', u'_id': ObjectId('5558bfaa0640fd1923fce1a1'), u'name': u'Foo'}
Fetching record: {u'age': u'30', u'_id': ObjectId('5558bfaa0640fd1923fce1a1'), u'name': u'Foo'}
Could not connect to primary
Fetching record: {u'age': u'30', u'_id': ObjectId('5558bfaa0640fd1923fce1a1'), u'name': u'Foo'}

In the preceding output, the client gets disconnected from the primary node midway. However, very soon, a new primary node is selected by the remaining nodes and the mongo client is able to resume the connection.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset