本文主要介绍python如何连接mongodb副本集及读写分离配置,mongodb副本集数据同步延迟测试。
一、python连接mongodb副本集
1.连接副本集
在新版的驱动中直接使用MongoClient连接
如下:
from pymongo import MongoClient
conn = MongoClient(['192.168.3.11:27017', '192.168.3.12:27017', '192.168.3.13:27017'])
2.读写分离配置
from pymongo import ReadPreference
db = conn.get_database('hnrtest', read_preference=ReadPreference.SECONDARY_PREFERRED)
副本集ReadPreference有5个选项:
PRIMARY:默认选项,从primary节点读取数据
PRIMARY_PREFERRED:优先从primary节点读取,如果没有primary节点,则从集群中可用的secondary节点读取
SECONDARY:从secondary节点读取数据
SECONDARY_PREFERRED:优先从secondary节点读取,如果没有可用的secondary节点,则从primary节点读取
NEAREST:从集群中可用的节点读取数据
二、测试mongodb延迟脚本
1.原理
往集群中插入一批数据,然后从secondary节点读取,直到读到数据总数与插入的总数相等。
2.脚本内容
#!/usr/bin/python
# -*- coding: utf-8 -*-
import pymongo
from pymongo import MongoClient
from pymongo import ReadPreference
import time
import argparse
# 定义执行时长装饰器函数
def warps(*args):
def deco(func):
def _deco(*args, **kwargs):
# 记录开始时间
start = time.clock()
# 回调原函数
func(*args, **kwargs)
# 记录结束时间
end = time.clock()
# 计算执行时长
delat = end - start
# 转换成ms输出
print("delay:%sms" % (int(delat*1000)))
return _deco
return deco
# 连接副本集
conn = MongoClient(['192.168.3.11:27017', '192.168.3.12:27017', '192.168.3.13:27017'])
# 读写分离
db = conn.get_database('hnrtest', read_preference=ReadPreference.SECONDARY_PREFERRED)
# 定义连接的集合
collection = db.student
# 创建插入数据函数
def data_insert(num):
try:
for i in range(1, num):
collection.insert({"name": "student"+str(i), "age": (i % 100), "city": "FuZhou"})
except Exception as e:
print("insert data:", e)
# 创建查询数据函数,引用装饰器函数
@warps()
def data_select(num):
try:
count = collection.find().count()
while count != num - 1:
count = collection.find().count()
except Exception as e:
print("select data:", e)
# 创建删除数据函数
def data_delete():
try:
collection.remove({})
except Exception as e:
print("delete data:", e)
# 创建计算延迟时长函数
def data_delay(num):
data_insert(num)
data_select(num)
if __name__ == '__main__':
# 定义脚本需要传入插入的数据量,默认值为1000,通过-n传入参数
parser = argparse.ArgumentParser(description='insert data to mongodb number')
parser.add_argument('-n', action='store', dest='num', type=int, required=False, default=1000)
given_args = parser.parse_args()
num = given_args.num
data_delete()
data_delay(num)