- MongoDB Cookbook(Second Edition)
- Cyrus Dasadia Amol Nayak
- 557字
- 2021-07-23 14:38:57
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…
- 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)
- Connect to any of the nodes in the replica set, say to
localhost:27000
, and executers.status()
from the shell. Take a note of the primary instance in the replica set and connect to it from the shell, iflocalhost:27000
is not a primary. Here, switch to the administrator database as follows:> repSetTest:PRIMARY>use admin
- We now execute the preceding script from the operating system shell as follows:
$ python replicaset_client.py
- Shut down the primary instance by executing the following on the mongo shell that is connected to the primary:
> repSetTest:PRIMARY> db.shutdownServer()
- 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.
- 軟件項(xiàng)目估算
- Python機(jī)器學(xué)習(xí):數(shù)據(jù)分析與評分卡建模(微課版)
- PHP 從入門到項(xiàng)目實(shí)踐(超值版)
- Microsoft Application Virtualization Cookbook
- Cocos2d-x游戲開發(fā):手把手教你Lua語言的編程方法
- 從程序員到架構(gòu)師:大數(shù)據(jù)量、緩存、高并發(fā)、微服務(wù)、多團(tuán)隊(duì)協(xié)同等核心場景實(shí)戰(zhàn)
- C語言程序設(shè)計(jì)立體化案例教程
- 大模型RAG實(shí)戰(zhàn):RAG原理、應(yīng)用與系統(tǒng)構(gòu)建
- Oracle GoldenGate 12c Implementer's Guide
- C語言程序設(shè)計(jì)習(xí)題與實(shí)驗(yàn)指導(dǎo)
- MyBatis 3源碼深度解析
- 深入分析GCC
- Building Microservices with Go
- Continuous Integration,Delivery,and Deployment
- 程序員的算法趣題2