通过pymongo测试MongoDB的高可用性

1 实验环境搭建
三台Ubuntu 14.04 64bit Server。

Mongodb version db version v2.6.7.
Pymongo version 2.7

1.1 Install mongodb
在三台机器上安装mongodb

1.2 Configure mongodb
我们要配置一个三个节点的Replication,如下所示:

1.2.1 三个节点能相互通信,配置DNS。
三台机器的hostname分别为:mongodb1 mongodb2 mongodb3
配置host:

保证之间能相互ping通:

1.2.2. 配置mongodb
尽量使用MongoDB默认端口27017. 使用bind_ip配置选项配置绑定的IP地址,能让三个节点相互访问。

1) 启动三个mongodb进程
修改三台host的mongodb1的配置: 添加一个相同的replicate set的名字:rs1。

启动三台host的mongod进程

2) 然后进到mongodb1的mongo shell里初始化配置。

可以看到此时因为只有一个member,并且本身就是PRIMARY.

2) 添加member
把另外两台host添加进来。

结果可以看到mongodb1是PRIMARY,其它两个host是SECONDARY。

3) 删除member
http://docs.mongodb.org/manual/tutorial/remove-replica-set-member/

  1. Shut down the mongod instance for the member you wish to remove. To shut down the instance, connect using the mongo shell and the db.shutdownServer() method.
  2. Connect to the replica set’s current primary. To determine the current primary, use db.isMaster() while connected to any member of the replica set.
  3. Use rs.remove() in either of the following forms to remove the member:

MongoDB disconnects the shell briefly as the replica set elects a new primary. The shell then automatically reconnects. The shell displays a DBClientCursor::init call() failed error even though the command succeeds.
至此mongodb的配置完成了。
2 测试内容

2.1 Test case 1: Basic operations
测试对Replica Set的基本连接,以及基本写操作,和Failover后的恢复操作,
使用pymongo

Connecting to a Replica Set

对数据库进行基本的写操作:

看到目前对于数据库的操作是PRIMARY,也就是host mongodb1。 然后对数据库写入一条record:

此时,把mongodb1的mongod stop掉。

发现有个pymongo.errors.AutoReconnect的异常,不过马上恢复了,而且此时的操作数据库变成了mongodb3,也就是现在的PRIMARY.
如果需要从Secondary读取数据,可以设置ReadPreference.

2.2 Test case 2: PRIMARY lost connection with all SECONDARY
假如PRIMARY和其它所有的SECONDARY失去联系了,那么PRIMARY就无法进行读写操作了。

直到有至少两个Replica Set的host连接,然后选出新的PRIMARY。

2.3 Test case 3: Write Concern
根据Write Concern for Replica Sets的介绍:

pymongo write concern

默认的write concern是空的配置。write concern有四个参数:w,wtimeout,j, fsync。
其中比较重要的是w和wtimeout。
w: (integer or string)If this is a replica set, write operations will block until they have been replicated to the specified
number or tagged set of servers. w= always includes the replica set primary (e.g. w=3 means write to
the primary and wait until replicated to two secondaries). Setting w=0 disables write acknowledgement and
all other write concern options.
wtimeout: (integer) Used in conjunction with w. Specify a value in milliseconds to control how long to wait for write
propagation to complete. If replication does not complete in the given timeframe, a timeout exception is raised.

Reference

Three Member Replica Sets from mongodb.org

http://docs.mongodb.org/manual/replication/

How to Setup MongoDB Replication Using Replica Set and Arbiters

pymongo documentation

1 2 收藏 评论

相关文章

可能感兴趣的话题



直接登录
跳到底部
返回顶部