Last active
April 7, 2018 20:36
-
-
Save BruceZu/6054cc22ad8a298999013f3edee14330 to your computer and use it in GitHub Desktop.
Mongo replset in docker. Python access and verify Mongo replset.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| mongo replica set 3.4.4 local docker engine (bridge) |
Author
Author
=================
on sshhop prepare python environment same as the test environment : 3.5
$ sudo apt install virtualenv
$ pip install --upgrade virtualenv
$ pip install --upgrade pip
$ sudo apt install python3-venv
Reference
http://api.mongodb.com/python/current/examples/index.html
http://api.mongodb.com/python/current/api/pymongo/mongo_client.html
https://github.com/mongodb/mongo-python-driver?jmp=docs
https://stackoverflow.com/questions/22035257/django-non-rel-connecting-to-multiple-hosts-in-a-replica-set
Author
connect mongo arbiter in docker
$ docker exec -it forticloudplatform_mongo_1 mongo mongodb://forticloudplatform_mongo_arbiter_1run python from local 'worker' Docker container
$ docker exec -it $(docker ps | grep worker | cut -d" " -f 1) pythonfind out all place where the code need update: configuration and code
(.venv) bzu@bruce-laptop:~/project/FortiCloudPlatform$ grep -Rr "MONGODB" . --exclude-dir=".venv" --exclude-dir=".idea" --exclude-dir=".git"
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cases of accessing MongoDB replica set using PyMongo
As reference for upgrading existing MongoDB settings and application code to access MongoDB Replica set
Using PyMongo 3.4. It supports MongoDB 3.4
Reference case for update Settings and application code
Case1: Access AWS MongoDB replica set from EC2 'sshhop'
Access db 'foo'
Operation on db 'foo'
Check replica set nodes and the one the client is connecting
Check read preference and change the db 'foo' read preference to be ReadPreference.SECONDARY
Case2: Access local docker dev environment MongoDB replica set from local 'worker' Docker container
Check 'worker' Docker container Python and Pymongo version
Access Mongo replica set
Common operations
Application code need care
1> ConnectionFailure
Check if the server is available like this:
2> AutoReconnect exception
"When this exception is raised our application code needs to decide whether to retry the operation or to simply continue, accepting the fact that the operation might have failed.
On subsequent attempts to run the query we might continue to see this exception. Eventually, however, the replica set will failover and elect a new primary (this should take no more than a couple of seconds in general). At that point the driver will connect to the new primary and the operation will succeed:"
3> Secondary Reads
options of MongoClient parameters: Read Preference:
Defaults to primary. To use secondaries for queries we have to change the read preference:
Secondary Reads
Reference
http://api.mongodb.com/python/current/api/pymongo/mongo_client.html