官方文檔:http://api.mongodb.com/python/current/index.htmlhtml
MongoReplicaSetClient:http://api.mongodb.com/python/current/api/pymongo/mongo_replica_set_client.htmlpython
mongo文檔:https://docs.mongodb.com/manual/reference/command/mongodb
pymongo能夠使用mongo command方法調用mongo方法api
from pymongo import MongoClient c = MongoClient("10.207.0.128:4000") admin = c["admin"] #獲取實例狀態 admin.command('serverStatus') #副本集角色降級 admin.command("replSetStepDown",10) from pymongo import MongoReplicaSetClient #鏈接副本集 r= MongoReplicaSetClient(host = , port = , replicaSet = )
connection_admin=r.admin
connection_admin.authenticate('name','#') #獲取副本集主節點從節點 r.primary r.secondaries
得到庫對象和集合對象分別有兩種方法:負載均衡
db = client.test_database # 或者 db = client['test-database'] 獲得一個數據集合: collection = db.test_collection # 或者 collection = db['test-collection']
鏈接認證 auth failed解決方法spa
注意MongoDB3.0版本之後採用的是'SCRAM-SHA-1'認證方式,pymongo須要更新2.8以上版本code
http://api.mongodb.com/python/current/examples/authentication.htmlserver
mongo副本集rs.status()中htm
state:https://docs.mongodb.com/manual/reference/replica-states/對象
其餘:https://docs.mongodb.com/manual/reference/command/replSetGetStatus/#dbcmd.replSetGetStatus
pymongo連接副本集讀寫分離和負載均衡的使用:
http://api.mongodb.com/python/current/examples/high_availability.html
from pymongo import MongoClient,ReadPreference client = MongoClient(['host1:port','host2:port','host3:port'], replicaset='replset_name', readPreference='secondaryPreferred') db_name = client.get_database('db_name',read_preference=ReadPreference.NEAREST) db_name.authenticate('username','passwd',mechanism='SCRAM-SHA-1')
查詢MongoDB有哪些庫,庫大小:
https://docs.mongodb.com/manual/reference/command/listDatabases/#dbcmd.listDatabases
admin.command("listDatabases",1)
command是庫級別的,庫、集合、副本集等操做要按照語法:
db_object.command({'command-name':'command-value'}) 例如 db_test.command({ 'collStats' : 'collection-test})