【Mongodb】 Replica set的主从切换测试

Replica set 为我们提供了自动故障切换功能,这个机制是由mongodb自己来操作的,它根据从库的优先级或者数据新鲜度(也就是最新的从主库同步数据的那个节点)来选择primary,而当以前的primary起来之后,会成为secondary ,接受新的primary 的日志。

                                                              完整的replica sets

                                                             primary 当机

                   mongodb 会根据数据的新鲜度来选择下一个主库


接上一篇文章,搭建好了replica set,查看端口为 27018 27020两个服务的状态:

[mongodb@rac4 bin]$ ./mongo 127.0.0.1:27018

MongoDB shell version: 2.0.1

connecting to: 127.0.0.1:27018/test

PRIMARY> db.isMaster();

{

        "setName" : "myset",

        "ismaster" : true,  --为主库

        "secondary" : false,

        "hosts" : [

                "10.250.7.220:27018",

                "10.250.7.220:27020",

                "10.250.7.220:27019"

        ],

        "primary" : "10.250.7.220:27018",

        "me" : "10.250.7.220:27018",

        "maxBsonObjectSize" : 16777216,

        "ok" : 1

}

PRIMARY> exit

bye

[mongodb@rac4 bin]$ ./mongo 127.0.0.1:27020

MongoDB shell version: 2.0.1

connecting to: 127.0.0.1:27020/test

SECONDARY> 

SECONDARY> db.isMaster();

{

        "setName" : "myset",

        "ismaster" : false,

        "secondary" : true, --为从库

        "hosts" : [

                "10.250.7.220:27020",

                "10.250.7.220:27019",

                "10.250.7.220:27018"

        ],

        "primary" : "10.250.7.220:27018",

        "me" : "10.250.7.220:27020",

        "maxBsonObjectSize" : 16777216,

        "ok" : 1

}

PRIMARY> 手工杀掉primary 

[root@rac4 ~]# ps -ef | grep 27018 

mongodb  14826 14794  1 20:24 pts/4    00:00:05 ./mongod --dbpath /opt/mongodata/r1 --port 27018 --replSet myset --rest

mongodb  14999 14430  0 20:28 pts/2    00:00:00 ./mongo 127.0.0.1:27018

[root@rac4 ~]# kill -9 14826 14794

[root@rac4 ~]# ps -ef | grep mongodb |grep -v root

mongodb  14883 14853  1 20:26 pts/7    00:00:05 ./mongod --dbpath /opt/mongodata/r2 --port 27019 --replSet myset --rest

mongodb  14901 14548  1 20:27 pts/6    00:00:07 ./mongod --dbpath /opt/mongodata/r3 --port 27020 --replSet myset --rest

mongodb  14999 14430  0 20:28 pts/2    00:00:00 ./mongo 127.0.0.1:27018

mongodb  15102 15072  0 20:30 pts/5    00:00:00 ./mongo 127.0.0.1:27019

mongodb  15136 15106  0 20:30 pts/8    00:00:00 ./mongo 127.0.0.1:27020

[root@rac4 ~]# 

27019 端口的mongodb 输出日志显示的选择10.250.7.220 作为主库的日志记录

Mon Oct 31 20:27:59 [FileAllocator] allocating new datafile /opt/mongodata/r2/local.2, filling with zeroes...

Mon Oct 31 20:27:59 [rsHealthPoll] replSet info member 10.250.7.220:27018 is up

Mon Oct 31 20:27:59 [rsHealthPoll] replSet member 10.250.7.220:27018 is now in state SECONDARY

Mon Oct 31 20:27:59 [rsHealthPoll] replSet info 10.250.7.220:27020 is down (or slow to respond): still initializing

Mon Oct 31 20:27:59 [rsHealthPoll] replSet member 10.250.7.220:27020 is now in state DOWN

Mon Oct 31 20:28:01 [initandlisten] connection accepted from 10.250.7.220:10857 #3

Mon Oct 31 20:28:05 [conn2] replSet RECOVERING

Mon Oct 31 20:28:05 [conn2] replSet info voting yea for 10.250.7.220:27018 (0)

Mon Oct 31 20:28:07 [rsHealthPoll] replSet member 10.250.7.220:27018 is now in state PRIMARY

Mon Oct 31 20:28:09 [FileAllocator] done allocating datafile /opt/mongodata/r2/local.2, size: 1024MB,  took 10.89 secs

Mon Oct 31 20:28:10 [rsSync] ******

Mon Oct 31 20:28:10 [rsSync] replSet initial sync pending

Mon Oct 31 20:28:10 [rsSync] replSet syncing to: 10.250.7.220:27018

Mon Oct 31 20:28:10 [rsSync] build index local.me { _id: 1 }

Mon Oct 31 20:28:10 [rsSync] build index done 0 records 0.001 secs

Mon Oct 31 20:28:10 [rsSync] replSet initial sync drop all databases

Mon Oct 31 20:28:10 [rsSync] dropAllDatabasesExceptLocal 1

Mon Oct 31 20:28:10 [rsSync] replSet initial sync clone all databases

Mon Oct 31 20:28:10 [rsSync] replSet initial sync query minValid

Mon Oct 31 20:28:10 [rsSync] replSet initial oplog application from 10.250.7.220:27018 starting at Oct 31 20:27:53:1 to Oct 31 20:27:53:1

Mon Oct 31 20:28:13 [rsHealthPoll] replSet info member 10.250.7.220:27020 is up

Mon Oct 31 20:28:13 [rsHealthPoll] replSet member 10.250.7.220:27020 is now in state STARTUP2

Mon Oct 31 20:28:14 [rsSync] replSet initial sync finishing up

Mon Oct 31 20:28:14 [rsSync] replSet set minValid=4eae9449:1

Mon Oct 31 20:28:14 [rsSync] build index local.replset.minvalid { _id: 1 }

Mon Oct 31 20:28:14 [rsSync] build index done 0 records 0.005 secs

Mon Oct 31 20:28:14 [rsSync] replSet initial sync done

Mon Oct 31 20:28:15 [rsSync] replSet syncing to: 10.250.7.220:27018

Mon Oct 31 20:28:15 [rsSync] replSet SECONDARY

Mon Oct 31 20:28:15 [rsHealthPoll] replSet member 10.250.7.220:27020 is now in state RECOVERING

Mon Oct 31 20:28:26 [clientcursormon] mem (MB) res:16 virt:2677 mapped:1232

Mon Oct 31 20:28:52 [initandlisten] connection accepted from 10.250.7.220:10872 #4

Mon Oct 31 20:28:52 [initandlisten] connection accepted from 10.250.7.220:10873 #5

Mon Oct 31 20:28:52 [rsGhostSync] handshake between 2 and 10.250.7.220:27018

Mon Oct 31 20:28:53 [slaveTracking] build index local.slaves { _id: 1 }

Mon Oct 31 20:28:53 [slaveTracking] build index done 0 records 0.003 secs

Mon Oct 31 20:28:55 [conn5] end connection 10.250.7.220:10873

Mon Oct 31 20:28:55 [conn4] end connection 10.250.7.220:10872

Mon Oct 31 20:28:57 [rsHealthPoll] replSet member 10.250.7.220:27020 is now in state SECONDARY

Mon Oct 31 20:29:27 [clientcursormon] mem (MB) res:19 virt:2693 mapped:1232

Mon Oct 31 20:30:21 [initandlisten] connection accepted from 127.0.0.1:44672 #6

Mon Oct 31 20:33:35 [conn2] end connection 10.250.7.220:42493

Mon Oct 31 20:33:35 [rsSync] replSet syncThread: 10278 dbclient error communicating with server: 10.250.7.220:27018

Mon Oct 31 20:33:35 [rsHealthPoll] DBClientCursor::init call() failed

Mon Oct 31 20:33:35 [rsHealthPoll] replSet info 10.250.7.220:27018 is down (or slow to respond): DBClientBase::findN: transport error: 10.250.7.220:27018 query: { replSetHeartbeat: "myset", v: 1, pv: 1, checkEmpty: false, from: "10.250.7.220:27019" }

Mon Oct 31 20:33:35 [rsHealthPoll] replSet member 10.250.7.220:27018 is now in state DOWN

Mon Oct 31 20:33:35 [rsMgr] not electing self, 10.250.7.220:27020 would veto

Mon Oct 31 20:33:36 [conn3] replSet info voting yea for 10.250.7.220:27020 (2)

Mon Oct 31 20:33:37 [rsHealthPoll] replSet member 10.250.7.220:27020 is now in state PRIMARY

Mon Oct 31 20:33:46 [rsSync] replSet syncing to: 10.250.7.220:27020

Mon Oct 31 20:34:27 [clientcursormon] mem (MB) res:19 virt:2693 mapped:1232

27020 端口的mongodb 输出日志显示的选择10.250.7.220 作为主库的日志记录

Mon Oct 31 20:33:35 [rsSync] replSet syncThread: 10278 dbclient error communicating with server: 10.250.7.220:27018

Mon Oct 31 20:33:36 [rsHealthPoll] DBClientCursor::init call() failed

Mon Oct 31 20:33:36 [rsHealthPoll] replSet info 10.250.7.220:27018 is down (or slow to respond): DBClientBase::findN: transport error: 10.250.7.220:27018 query: { replSetHeartbeat: "myset", v: 1, pv: 1, checkEmpty: false, from: "10.250.7.220:27020" }

Mon Oct 31 20:33:36 [rsHealthPoll] replSet member 10.250.7.220:27018 is now in state DOWN

Mon Oct 31 20:33:36 [rsMgr] replSet info electSelf 2

Mon Oct 31 20:33:36 [rsMgr] replSet PRIMARY

Mon Oct 31 20:33:46 [initandlisten] connection accepted from 10.250.7.220:37261 #5

Mon Oct 31 20:33:47 [slaveTracking] build index local.slaves { _id: 1 }

Mon Oct 31 20:33:47 [slaveTracking] build index done 0 records 0.001 secs

Mon Oct 31 20:33:48 [clientcursormon] mem (MB) res:19 virt:2692 mapped:1232

Mon Oct 31 20:34:35 [conn4] end connection 127.0.0.1:17500

Mon Oct 31 20:34:37 [initandlisten] connection accepted from 127.0.0.1:36525 #6

进入数据库查看:

[mongodb@rac4 bin]$ ./mongo 127.0.0.1:27020

MongoDB shell version: 2.0.1

connecting to: 127.0.0.1:27020/test

PRIMARY> 

PRIMARY> 

PRIMARY> db.isMaster();

{

        "setName" : "myset",

        "ismaster" : true,--成为主库master

        "secondary" : false,

        "hosts" : [

                "10.250.7.220:27020",

                "10.250.7.220:27019",

                "10.250.7.220:27018"

        ],

        "primary" : "10.250.7.220:27020",

        "me" : "10.250.7.220:27020",

        "maxBsonObjectSize" : 16777216,

        "ok" : 1

}

PRIMARY> 

重新启动端口为27018的mongodb的数据库服务:从日志中可以看出其进行恢复的操作记录

[mongodb@rac4 bin]$ ./mongod --dbpath /opt/mongodata/r1 --port 27018  --rest --replSet myset &

[1] 16290

[mongodb@rac4 bin]$ Mon Oct 31 20:48:32 [initandlisten] MongoDB starting : pid=16290 port=27018 dbpath=/opt/mongodata/r1 64-bit host=rac4

Mon Oct 31 20:48:32 [initandlisten] db version v2.0.1, pdfile version 4.5

Mon Oct 31 20:48:32 [initandlisten] git version: 3a5cf0e2134a830d38d2d1aae7e88cac31bdd684

Mon Oct 31 20:48:32 [initandlisten] build info: Linux bs-linux64.10gen.cc 2.6.21.7-2.ec2.v1.2.fc8xen #1 SMP Fri Nov 20 17:48:28 EST 2009 x86_64 BOOST_LIB_VERSION=1_41

Mon Oct 31 20:48:32 [initandlisten] options: { dbpath: "/opt/mongodata/r1", port: 27018, replSet: "myset", rest: true }

Mon Oct 31 20:48:32 [initandlisten] journal dir=/opt/mongodata/r1/journal

Mon Oct 31 20:48:32 [initandlisten] recover begin

Mon Oct 31 20:48:32 [initandlisten] recover lsn: 231055

Mon Oct 31 20:48:32 [initandlisten] recover /opt/mongodata/r1/journal/j._0

Mon Oct 31 20:48:32 [initandlisten] recover skipping application of section seq:198962 < lsn:231055

Mon Oct 31 20:48:32 [initandlisten] recover cleaning up

Mon Oct 31 20:48:32 [initandlisten] removeJournalFiles

Mon Oct 31 20:48:32 [initandlisten] recover done

Mon Oct 31 20:48:32 [initandlisten] waiting for connections on port 27018

Mon Oct 31 20:48:32 [websvr] admin web console waiting for connections on port 28018

Mon Oct 31 20:48:32 [initandlisten] connection accepted from 127.0.0.1:11930 #1

Mon Oct 31 20:48:32 [rsStart] replSet STARTUP2

Mon Oct 31 20:48:32 [rsHealthPoll] replSet info member 10.250.7.220:27019 is up

Mon Oct 31 20:48:32 [rsHealthPoll] replSet member 10.250.7.220:27019 is now in state SECONDARY

Mon Oct 31 20:48:32 [rsHealthPoll] replSet info member 10.250.7.220:27020 is up

Mon Oct 31 20:48:32 [rsHealthPoll] replSet member 10.250.7.220:27020 is now in state PRIMARY

Mon Oct 31 20:48:32 [rsSync] replSet SECONDARY

Mon Oct 31 20:48:33 [initandlisten] connection accepted from 10.250.7.220:35971 #2

Mon Oct 31 20:48:34 [initandlisten] connection accepted from 10.250.7.220:35972 #3

Mon Oct 31 20:48:36 [rsSync] replSet syncing to: 10.250.7.220:27020

Mon Oct 31 20:48:36 [rsSync] build index local.me { _id: 1 }

Mon Oct 31 20:48:36 [rsSync] build index done 0 records 0 secs

[mongodb@rac4 bin]$ ./mongo 127.0.0.1:27018

MongoDB shell version: 2.0.1

connecting to: 127.0.0.1:27018/test

SECONDARY> 

SECONDARY> db.isMaster();

{

        "setName" : "myset",

        "ismaster" : false,   --端口为 27018的数据库服务变为从库

        "secondary" : true,

        "hosts" : [

                "10.250.7.220:27018",

                "10.250.7.220:27020",

                "10.250.7.220:27019"

        ],

        "primary" : "10.250.7.220:27020",

        "me" : "10.250.7.220:27018",

        "maxBsonObjectSize" : 16777216,

        "ok" : 1

}

SECONDARY> 

时间: 2024-09-22 23:58:29

【Mongodb】 Replica set的主从切换测试的相关文章

mongodb replica set 配置高性能多服务器详解_MongoDB

mongodb的多服务器配置,以前写过一篇文章,是master-slave模式的,请参考:详解mongodb 主从配置.master-slave模式,不能自动实现故障转移和恢复.所以推荐大家使用mongodb的replica set,来实现多服务器的高可用.给我的感觉是replica set好像自带了heartbeat功能,挺强大的. 一,三台服务器,1主,2从 服务器1:127.0.0.1:27017 服务器2:127.0.0.1:27018 服务器3:127.0.0.1:27019 1,创建

从MongoDB Replica Set HA 看分布式系统读写一致性问题

副本集基础 Replica Set是mongodb提供的一个去中心化的备份模式(同时mongodb还提供了主从部署和分片模式),每个mongod都可以是master,且副本集内会自动选举出一个primary,其他都暂时为seconary,primary挂掉后会自动选举出新的primary.副本集内所有mongod存储的都是数据全集,secondary节点会从primary同步数据操作以保证自己的数据up-to-date.副本集有自己的选举机制,相对是一种比较简单的选举,根据心跳.权重等因素选取一

master_pos_wait函数与MySQL主从切换

背景   主从切换是高可用MySQL架构的必要步骤(即使用不发生,也要有备无患).一般设置为双M(M1.M2),假设当前状态为写M1,而M2只读,切换的大致流程如下: 1.  停止应用写M1,将M1设置为只读 2.  检查M2的slave status直到赶上M1 3.  将M1设置为可写      其中在第2步细化为 a)       在M1上show master status;得到binlog位置P,因为已经设为只读,不会变化 b)       循环检测M2上的执行位置,若未到P,则过几秒

MHA实现mysql主从切换之主故障

本文主要模拟实现主Master故障后,手动指定Master到从节点.   #masterha_master_switch --master_state=dead --conf=/etc/masterha/app1.cnf  --dead_master_host=10.1.1.231 --dead_master_ip=10.1.1.231 --dead_master_port=63306  --new_master_host=10.1.1.234 --new_master_ip=10.1.1.23

诺西完成全球首个LTE切换测试

北京时间10月29日消息,最近,诺基亚西门子通信使用商用基站和标准软件进行了全球首个 LTE 切换测试.该测试表明诺基亚西门子通信 Flexi Multiradio 基站和 LTE 测试终端均支持并正确处理 LTE 切换程序. 切换是移动无线技术的一项重要特性.诺基亚西门子通信进行的这项 LTE测试是全球首例完全符合 3GPP(LTE 标准机构) 2009 年 3 月版本LTE 标准的切换测试.在这一初步测试中,切换在诺基亚西门子通信屡获殊荣的 Flexi Multiradio 基站LTE小区(

mongodb replica set 添加删除节点的2种方法_MongoDB

一,利用rs.reconfig,来添加,删除节点 1,添加节点 repmore:PRIMARY> config = {_id:"repmore",members:[{_id:0,host:'127.0.0.1:27017',priority :2},{_id:1,host:'127.0.0.1:27018',priority:1}]}; //添加节点 repmore:PRIMARY> rs.reconfig(config); //使配置生效 repmore:PRIMARY&

Android编程之页面切换测试实例_Android

本文实例讲述了Android编程之页面切换测试.分享给大家供大家参考.具体分析如下: 一.软件平台: win7 + eclipse + sdk 二.设计思路: 两个页面:mian和ok,每个页面上有一个按键,点击则可以互相切换 三.源代码: main.xml源代码: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.andr

MongoDB支持的java数据类型和测试例子_java

mongodb的java数据类型有: Object Ids 自动IDRegular Expressions 正则表达式搜索记录Dates/Times 时间Database References 数据基础结构Binary Data 二进制流Timestamp Data 时间标记戳Code Data 代码数据Embedded Documents 嵌入式文档Arrays 数组类型 详细参考原来英文 http://docs.mongodb.org/ecosystem/drivers/java-types

诺基亚西门子完成全球首个LTE切换测试

北京时间10月28日晚间消息,最近,诺基亚西门子通信使用商用基站和标准软件进行了全球首个 LTE 切换测试.该测试表明诺基亚西门子通信 Flexi Multiradio 基站和 LTE 测试终端均支持并正确处理 LTE 切换程序. 切换是移动无线技术的一项重要特性.诺基亚西门子通信进行的这项 LTE测试是全球首例完全符合 3GPP(LTE 标准机构) 2009 年 3 月版本LTE 标准的切换测试.在这一初步测试中,切换在诺基亚西门子通信屡获殊荣的 Flexi Multiradio 基站LTE小