此验证中只开启两个mongodb节点,可以连接任意节点,以下操作不涉及读写,不涉及连接那个节点
mongodb连接:
from pymongo import MongoReplicaSetClient client = MongoReplicaSetClient('mongodb://test:caesar_123@196.168.1.112:27017/test',replicaset='spock')
rs.status
在command中输出如下:
spock:SECONDARY> rs.status() { "set" : "spock", "date" : ISODate("2018-10-07T15:27:19.877Z"), "myState" : 2, "term" : NumberLong(28), "syncingTo" : "196.168.1.112:27017", "heartbeatIntervalMillis" : NumberLong(2000), "members" : [ { "_id" : 2, "name" : "196.168.1.111:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 1457, "optime" : { "ts" : Timestamp(1538924592, 1), "t" : NumberLong(28) }, "optimeDate" : ISODate("2018-10-07T15:03:12Z"), "syncingTo" : "196.168.1.112:27017", "configVersion" : 25332, "self" : true }, { "_id" : 3, "name" : "196.168.1.112:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 1456, "optime" : { "ts" : Timestamp(1538924592, 1), "t" : NumberLong(28) }, "optimeDate" : ISODate("2018-10-07T15:03:12Z"), "lastHeartbeat" : ISODate("2018-10-07T15:27:18.350Z"), "lastHeartbeatRecv" : ISODate("2018-10-07T15:27:19.538Z"), "pingMs" : NumberLong(0), "electionTime" : Timestamp(1538924591, 1), "electionDate" : ISODate("2018-10-07T15:03:11Z"), "configVersion" : 25332 } ], "ok" : 1 }
在pymongo中,使用client.admin.command('replSetGetStatus‘)输出见下:
(Pdb) client.admin.command("replSetGetStatus") {u'term': 28L, u'set': u'spock', u'ok': 1.0, u'heartbeatIntervalMillis': 2000L, u'myState': 1, u'members': [{u'uptime': 1087, u'configVersion': 25332, u'optime': {u'ts': Timestamp(1538924592, 1), u't': 28L}, u'name': u'196.168.1.111:27017', u'pingMs': 0L, u'optimeDate': datetime.datetime(2018, 10, 7, 15, 3, 12), u'syncingTo': u'196.168.1.112:27017', u'state': 2, u'health': 1.0, u'stateStr': u'SECONDARY', u'lastHeartbeatRecv': datetime.datetime(2018, 10, 7, 15, 21, 11, 981000), u'_id': 2, u'lastHeartbeat': datetime.datetime(2018, 10, 7, 15, 21, 11, 142000)}, {u'uptime': 6416, u'configVersion': 25332, u'optime': {u'ts': Timestamp(1538924592, 1), u't': 28L}, u'name': u'196.168.1.112:27017', u'self': True, u'optimeDate': datetime.datetime(2018, 10, 7, 15, 3, 12), u'electionTime': Timestamp(1538924591, 1), u'state': 1, u'health': 1.0, u'stateStr': u'PRIMARY', u'_id': 3, u'electionDate': datetime.datetime(2018, 10, 7, 15, 3, 11)}], u'date': datetime.datetime(2018, 10, 7, 15, 21, 12, 55000)}
primary ip
在pymongo中,使用client.primary输出元组见下:
(Pdb) client.primary ('196.168.1.112', 27017)
(Pdb) client.secondaries set([(u'196.168.1.111', 27017)])
db.getCollectionInfos
spock:SECONDARY> db.getCollectionInfos() [ { "name" : "meter", "options" : { } }, { "name" : "resource", "options" : { } } ]
在pymongo中,使用collections = db.list_collections()返回pymongo.command_cursor.CommandCursor对象,遍历每个collection信息,都为字典。
{u'name': u'resource', u'options': {}} {u'name': u'meter', u'options': {}}
collection增删
(Pdb) flag = db.drop_collection('resource') (Pdb) flag {u'ns': u'test.resource', u'ok': 1.0, u'nIndexesWas': 1} (Pdb) flag = db.create_collection('resource') (Pdb) flag Collection(Database(MongoReplicaSetClient(host=['196.168.1.112:27017'], document_class=dict, tz_aware=False, connect=True, replicaset='spock'), u'test'), u'resource') (Pdb) flag = db.create_collection('resource') *** CollectionInvalid: collection resource already exists ( Pdb) db.drop_collection('meter') {u'ns': u'test.meter', u'ok': 1.0, u'nIndexesWas': 4} (Pdb) db.drop_collection('meter') {u'code': 26, u'ok': 0.0, u'errmsg': u'ns not found'} 创建capeed类型collection db.create_collection('meter', capped=True, size=5242880, max=5000)