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

}

本文转自yzy121403725 51CTO博客,原文链接:http://blog.51cto.com/lookingdream/1795019,如需转载请自行联系原作者

【Mongodb】 Replica set的自动故障切换相关推荐

  1. (1)解锁MongoDB replica set核心姿势

    本文倒腾目前大热的MongoDB Replica Set集群,在倒腾的同时串讲一些 MongoDB特性. 副本集Replica Set是一个术语,定义具有多节点的数据库集群,这些节点具有主从复制(ma ...

  2. Mongodb Replica Configure

    Mongodb Replica Configure      我在配置replica的时候,文档中也把官网的中一些重要解释放在里面了但是并没有用中文做必要的解释,不过都是很容易理解的.说一下环境,这里 ...

  3. replica文件服务器,mongodb replica set 配置高性能多服务器详解

    mongodb的多服务器配置,以前写过一篇文章,是master-slave模式的,请参考:详解mongodb 主从配置.master-slave模式,不能自动实现故障转移和恢复.所以推荐大家使用mon ...

  4. mysql replication 监控_MySQL之-Replication监控及自动故障切换的详细分析

    1.服务器规划 Master:192.168.0.152 Slave: 192.168.0.153 192.168.0.154 监控服务器: 192.168.0.154 注意:真实生产环境最好有一台单 ...

  5. 通过Keepalived实现Redis Failover自动故障切换功能(整理中) .

    通过Keepalived实现Redis Failover自动故障切换功能 分类: Linux 2013-06-21 16:44 1049人阅读 评论(1) 收藏 举报 详细可参考http://heyl ...

  6. Centos7 deploy mongoDB Replica set

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.     概述: 主节点(Primary) 在复制集中,主节点是唯一能够接收写请求的节点.Mon ...

  7. 通过Keepalived实现Redis Failover自动故障切换功能

    参考资料: http://patrick-tang.blogspot.com/2012/06/redis-keepalived-failover-system.html http://deidara. ...

  8. mongodb和mysql中自动增长id_MongoDB 自动增长

    MongoDB 自动增长 MongoDB 没有像 SQL 一样有自动增长的功能, MongoDB 的 _id 是系统自动生成的12字节唯一标识. 但在某些情况下,我们可能需要实现 ObjectId 自 ...

  9. mongodb和mysql中自动增长id_如何在java中维护MongoDB的_id字段的自动增长

    _id字段是MongoDB中一个特殊的字段,它的类型为BSON ObjectId.它是每条记录的第一个属性,并且该属性值必须唯一. BSON ObjectId长12个字节,包含4字节的时间戳.3字节的 ...

最新文章

  1. Python中if__name__==__main__:该如何理解
  2. 语音信号短时域分析之预处理(三)
  3. 使用geopandas 制作中国疫情地图
  4. 对easyUI中课堂源码编辑改进建议
  5. layui select框代码实现自动下拉
  6. Java杂记3—流程控制之条件 1
  7. sqlserver 时间递增循环
  8. 浅析MySQL中exists,in ,=的使用
  9. 工字型钢弹性截面模量计算公式_弹性截面模量与塑性截面模量的例析
  10. 物联网的体系结构和关键技术
  11. Centos7上搭建迅雷远程下载服务器
  12. 【程序包uti1不存在怎么办?】求解答
  13. 教你快速制作一个简单的网页
  14. pyecharts官网
  15. redis的消息队列(面试题)
  16. acm计算机教育汇刊,ACM 全文数据库
  17. android hook api
  18. 兼容 Spring Boot 1.x 和 2.x 配置类参数绑定的工具类 SpringBootBindUtil
  19. 传奇GOM引擎补丁安装教程图解,传奇pak补丁介绍
  20. Appium在Android无法显示界面处理方式

热门文章

  1. Valve CEO:脑机接口技术可加强游戏体验远超“肉体外设“
  2. 英伟达吞并Arm失败!英美欧盟一齐干涉,老黄还要给孙正义12.5亿美元分手费
  3. 仅需2张图,AI便可生成完整运动过程
  4. 是时候给2020年做个总结了
  5. 疫情之下第一个倒闭航空公司:百年老店Avianca申请破产保护,国内民航也不乐观...
  6. 李飞飞团队加入AI抗疫:家用监控系统,可以远程反馈新冠症状
  7. 华为VC首次出手:投资2家半导体公司,带火5G建材“碳化硅”
  8. 高通5G旗舰升级!骁龙855Plus发布,主打游戏和AI;网友:牙膏厂
  9. 阿里云企业邮箱收费吗
  10. 华为交换机SSH登录失败原因