Is pymongo replica installed to support client connection automatically?

I created the following Mongo replicas using mongo cli:

> config = { _id:"repset", members:[{_id:0,host:"192.168.0.1:27017"},{_id:1,host:"192.168.0.2:27017"},{_id:2,host:"192.168.0.3:27017"}]} > rs.initiate(config); 

All mongo servers are working correctly.

 >>> import pymongo >>> from pymongo import MongoClient >>> servers = ["192.168.0.1:27017", "192.168.0.2:27017", "192.168.0.3:27017"] >>> MongoClient(servers) >>> xc = MongoClient() >>> print xc MongoClient('localhost', 27017) >>> print xc.database_names() [u'test_repsets', u'local', u'admin', u'test'] 

After I kill the local mongodb server, it shows me a connection timeout error:

 pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused 

There seems to be no automatic failure, although I defined the mongodb server. I am wondering if pymongo handles the failure automatically, or how is this situation handled properly?

Thanks in advance.

+5
source share
2 answers

in Pymongo 3.x, you can explicitly indicate which replica you are connecting to. I know that Pymongo 3.x has switched to some of the ways to manage server arrays. I got this from the Pymongo API about replica connectivity and fault tolerance

0
source

In your code:

MongoClient (servers) No variable is assigned above the line. It should assign to the variable (in your case, you again created an instance that causes an error.)

Please add the following line

 >>> #MongoClient(servers) # remove this line >>> #xc = MongoClient() # remove this line >>> xc = MongoClient(servers) # add this line 
0
source

All Articles