Python连接MongoDB分片集群

Python连接MongoDB分片集群

首先需要说明的是:首先mongDB的分片集群是我自己建的,没有使用认证,所以人人都能连接,其次使用pymongo来连接集群。
我们看一下官方的连接string:
mongodb://[username:password@]host1[:port1][,host2[:port2],…[,hostN[:portN]]][/[database][?options]]
所以我们写了一个测试文件:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import time
from pymongo import MongoReplicaSetClient
from pymongo import MongoClient
# 连接单机
# single mongo
# c = MongoClient(host="192.168.89.151", port=27017) # okay
# 连接集群
# mongo cluster
c = MongoClient('mongodb://192.168.89.151,192.168.89.152,192.168.89.153')#okay
print c.database_names()

结果都能连接,开心一波

你可能感兴趣的:(mongodb)