DB 정보
Replica Name: shard01
192.168.100.37:27017
192.168.100.37:27018
192.168.100.37:27019
Python 접속
- https://www.mongodb.com/community/forums/t/python-connection-to-replicaset/151808
- https://charsyam.wordpress.com/2011/12/07/pymongo에서의-mongodb-replica-set에서의-failover-과정/
- While로 돌리면서 Primary DB를 Kill하였을떄 기존 Secondary가 Primary로 넘어가면서 Connection이 되는것을 확인함
- db = client['test'] = test Database 접속
- collection = db['testdb'] = testdb Collection 접속
-
[root@mongo ~]# yum -y install python2-pip
pip2 install --upgrade pip==20.3
pip2 install pymongo
[root@mongo ~]# cat mongo.py
#!/bin/python2
from pymongo import MongoClient
import pprint
## Client connect - Replica
client = MongoClient(host=["192.168.100.37:27017","192.168.100.37:27018","192.168.100.37:27019"])
## Connect Test Database
db = client['test']
print(client.list_database_names())
## Connect testdb collection
collection=db['testdb']
pprint.pprint(collection.find_one())
[root@mongo ~]# python2 mongo.py
[u'admin', u'config', u'local', u'sample_airbnb', u'sample_analytics', u'sample_geospatial', u'sample_guides', u'sample_mflix', u'sample_restaurants', u'sample_supplies', u'sample_training', u'sample_weatherdata', u'test']
{u'_id': ObjectId('639ad7497ced46cad15d88a2'), u'x': 1.0}