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> 

【Mongodb】 Replica set的主从切换测试相关推荐

  1. mycat读写分离与主从切换

    1,分库分表的优缺点.以及为什么分表方式无法成为主流? 分表:在台服务器上,优点是易维护,类似表分区,缺点是在一台db服务器上,无法分担IO.负载集中. 分库:在多台服务器上,优点是分担IO.负载均衡 ...

  2. Centos7 deploy mongoDB Replica set

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

  3. Redis(持久化、主从复制、主从切换、twemproxy、redis集群)

    文章目录 Redis Redis应用场景 下载及安装 Redis常用指令 Redis持久化 Redis主从复制 Redis的Sentinel分布式系统(主从切换) sentnel(Redis的高可用方 ...

  4. Mycat 读写分离、主从切换、分库分表的操作记录

    Mycat 读写分离.主从切换.分库分表的操作记录 系统开发中,数据库是非常重要的一个点.除了程序的本身的优化,如:SQL语句优化.代码优化,数据库的处理本身优化也是非常重要的.主从.热备.分表分库等 ...

  5. Mycat读写分离、主从切换、分库分表的操作记录 https://www.cnblogs.com/kevingrace/p/9365840.html

    [此篇文章写得不错] 系统开发中,数据库是非常重要的一个点.除了程序的本身的优化,如:SQL语句优化.代码优化,数据库的处理本身优化也是非常重要的.主从.热备.分表分库等都是系统发展迟早会遇到的技术问 ...

  6. REDIS哨兵【Sentinel】模式+哨兵的核心知识点+redis哨兵主从切换的数据丢失问题+上一章铺垫的【异步复制数据丢失问题】+【集群脑裂】

    1.redis哨兵模式的前言: 一年一度的问题来了,为啥子要用redis的哨兵模式的呢? 简单粗暴的理解下子,顺带开个玩笑,没有理解好,还望不要见笑: 其实redis的哨兵模式,个人理解:只是说法搞大 ...

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

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

  8. Redis集群的主从切换研究

    目录 目录 1 1. 前言 1 2. slave发起选举 2 3. master响应选举 5 4. 选举示例 5 5. 哈希槽传播方式 6 6. 一次主从切换记录1 6 6.1. 相关参数 6 6.2 ...

  9. PostgreSQL一主两从实现及主从切换

    HA 实现方式: 热备份.冷备份 冷备份:物理备份,也就是文件复制.主机写完一个WAL日志文件后,才复制到standby服务器上去应用. 热备份:流复制,也就是同步复制或异步复制.主备机实时同步. 一 ...

最新文章

  1. Ehcarts 与 百度地图结合时,如何获取bmap的实例对象?
  2. 【ERP】如何根据系统币种(扩展)精度来动态产生数字的格式(FORMAT MASK)
  3. mysql 日期查询今天_Mysql 日期查询今天、昨天、近7天、近30天、本月、上一月、本季...
  4. mysql 创建函数_MySQL函数,存储过程,用户管理
  5. Dijkstra求最短路径例题
  6. 浙江大学_包家立计算生物学_神经网络
  7. 宏杉科技助力西安交大二附院:突破存储旧瓶颈、实现业务新成长
  8. sm是什么职位_职位或职级ED MD SM M 是什么意思?
  9. Catagory用法
  10. PCL之kd-tree详解
  11. XILINX DDR3 VIVADO(二)写模块
  12. 查询一段时间内的具体时间
  13. 转 TensorFlow Object Detection API 多GPU 卡平行计算,加速模型训练速度教学
  14. 【表白神器】Python超火隐藏表白图 你能看出来吗?【附源码】
  15. YGG SEA 通证经济模型概览
  16. P4169 [Violet]天使玩偶/SJY摆棋子 [CDQ分治]
  17. sim300的一些知识点
  18. 闪压压缩解压软件官方版
  19. Android 8.0目录介绍
  20. C#项目实战练习:SunTalk——做你自己的QQ

热门文章

  1. 北京20-35万Sharepoint Senior Consultant
  2. latex中插入visio矢量图的方法
  3. 数据库持久 项目重启_Aerospike + ScaleFlux 提供的超高性能方案助力同盾超大规模核心数据库系统...
  4. Xamarin SQLite教程Xamarin.iOS项目添加引用
  5. 分享Kali Linux 2017年第17周镜像文件
  6. python如何启动app_Python手机app爬虫|Airtest连接多个手机的时候,如何启动APP-start_app...
  7. html选择和按钮间距,html - 想要在按钮之间添加间距
  8. pyinstaller打包py文件生成的exe出现闪退问题
  9. python读取.locs文件
  10. 英特尔:80%的边缘数据都是视频数据!新成立物联网视频事业部,总部base中国...