mongo学习- 副本集 大多数原则
副本集中有一个重要的概念“大多数”,意思是说,选择主节点需要大多数决定(本人亲自做了实验)
步骤:
1.开启副本集(如果没有配置好 副本集的 亲参考我的上篇文章 https://www.cnblogs.com/anxbb/p/9482304.html)
2.运行 rs.status(),查看状态
{
"set" : "haibin",
"date" : ISODate("2018-08-16T03:18:05.461Z"),
"myState" : 1,
"term" : NumberLong(9),
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1534389483, 1),
"t" : NumberLong(9)
},
"appliedOpTime" : {
"ts" : Timestamp(1534389483, 1),
"t" : NumberLong(9)
},
"durableOpTime" : {
"ts" : Timestamp(1534389483, 1),
"t" : NumberLong(9)
}
},
"members" : [
{
"_id" : 0,
"name" : "127.0.0.1:27017",
"health" : 1.0,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 788,
"optime" : {
"ts" : Timestamp(1534389483, 1),
"t" : NumberLong(9)
},
"optimeDate" : ISODate("2018-08-16T03:18:03.000Z"),
"electionTime" : Timestamp(1534389270, 1),
"electionDate" : ISODate("2018-08-16T03:14:30.000Z"),
"configVersion" : 2,
"self" : true
},
{
"_id" : 1,
"name" : "127.0.0.1:27018",
"health" : 1.0,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 6,
"optime" : {
"ts" : Timestamp(1534389483, 1),
"t" : NumberLong(9)
},
"optimeDurable" : {
"ts" : Timestamp(1534389483, 1),
"t" : NumberLong(9)
},
"optimeDate" : ISODate("2018-08-16T03:18:03.000Z"),
"optimeDurableDate" : ISODate("2018-08-16T03:18:03.000Z"),
"lastHeartbeat" : ISODate("2018-08-16T03:18:04.903Z"),
"lastHeartbeatRecv" : ISODate("2018-08-16T03:18:03.874Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "127.0.0.1:27017",
"configVersion" : 2
},
{
"_id" : 2,
"name" : "127.0.0.1:27019",
"health" : 1.0,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 221,
"lastHeartbeat" : ISODate("2018-08-16T03:18:04.603Z"),
"lastHeartbeatRecv" : ISODate("2018-08-16T03:18:04.103Z"),
"pingMs" : NumberLong(0),
"configVersion" : 2
}
],
"ok" : 1.0
}
3.试着关闭 27017这个端口 然后在运行 rs.status(),结果如下
{
"set" : "haibin",
"date" : ISODate("2018-08-16T03:20:21.708Z"),
"myState" : 1,
"term" : NumberLong(10),
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1534389603, 1),
"t" : NumberLong(9)
},
"appliedOpTime" : {
"ts" : Timestamp(1534389616, 1),
"t" : NumberLong(10)
},
"durableOpTime" : {
"ts" : Timestamp(1534389616, 1),
"t" : NumberLong(10)
}
},
"members" : [
{
"_id" : 0,
"name" : "127.0.0.1:27017",
"health" : 0.0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDurable" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("1970-01-01T00:00:00.000Z"),
"optimeDurableDate" : ISODate("1970-01-01T00:00:00.000Z"),
"lastHeartbeat" : ISODate("2018-08-16T03:20:21.009Z"),
"lastHeartbeatRecv" : ISODate("2018-08-16T03:20:02.982Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "����Ŀ�������������ܾ��������ӡ�",
"configVersion" : -1
},
{
"_id" : 1,
"name" : "127.0.0.1:27018",
"health" : 1.0,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 144,
"optime" : {
"ts" : Timestamp(1534389616, 1),
"t" : NumberLong(10)
},
"optimeDate" : ISODate("2018-08-16T03:20:16.000Z"),
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1534389614, 1),
"electionDate" : ISODate("2018-08-16T03:20:14.000Z"),
"configVersion" : 2,
"self" : true
},
{
"_id" : 2,
"name" : "127.0.0.1:27019",
"health" : 1.0,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 142,
"lastHeartbeat" : ISODate("2018-08-16T03:20:20.912Z"),
"lastHeartbeatRecv" : ISODate("2018-08-16T03:20:20.345Z"),
"pingMs" : NumberLong(0),
"configVersion" : 2
}
],
"ok" : 1.0
}
我们发现现在端口 27018 变成了主节点,27017 为断开状态
4.试着关闭 27019 这个端口 然后在运行 rs.status(),结果如下
/* 1 */
{
"set" : "haibin",
"date" : ISODate("2018-08-16T03:21:34.102Z"),
"myState" : 1,
"term" : NumberLong(10),
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1534389603, 1),
"t" : NumberLong(9)
},
"appliedOpTime" : {
"ts" : Timestamp(1534389686, 1),
"t" : NumberLong(10)
},
"durableOpTime" : {
"ts" : Timestamp(1534389686, 1),
"t" : NumberLong(10)
}
},
"members" : [
{
"_id" : 0,
"name" : "127.0.0.1:27017",
"health" : 0.0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDurable" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("1970-01-01T00:00:00.000Z"),
"optimeDurableDate" : ISODate("1970-01-01T00:00:00.000Z"),
"lastHeartbeat" : ISODate("2018-08-16T03:21:31.166Z"),
"lastHeartbeatRecv" : ISODate("2018-08-16T03:20:02.982Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "����Ŀ�������������ܾ��������ӡ�",
"configVersion" : -1
},
{
"_id" : 1,
"name" : "127.0.0.1:27018",
"health" : 1.0,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 217,
"optime" : {
"ts" : Timestamp(1534389686, 1),
"t" : NumberLong(10)
},
"optimeDate" : ISODate("2018-08-16T03:21:26.000Z"),
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1534389614, 1),
"electionDate" : ISODate("2018-08-16T03:20:14.000Z"),
"configVersion" : 2,
"self" : true
},
{
"_id" : 2,
"name" : "127.0.0.1:27019",
"health" : 0.0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"lastHeartbeat" : ISODate("2018-08-16T03:21:31.966Z"),
"lastHeartbeatRecv" : ISODate("2018-08-16T03:21:25.358Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "����Ŀ�������������ܾ��������ӡ�",
"configVersion" : -1
}
],
"ok" : 1.0
}
发现现在 27018这个端口还是主节点,那么我们过一会在运行 rs.tatus(),在看下结果
/* 1 */
{
"set" : "haibin",
"date" : ISODate("2018-08-16T03:23:20.966Z"),
"myState" : 2,
"term" : NumberLong(10),
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1534389603, 1),
"t" : NumberLong(9)
},
"appliedOpTime" : {
"ts" : Timestamp(1534389686, 1),
"t" : NumberLong(10)
},
"durableOpTime" : {
"ts" : Timestamp(1534389686, 1),
"t" : NumberLong(10)
}
},
"members" : [
{
"_id" : 0,
"name" : "127.0.0.1:27017",
"health" : 0.0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDurable" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("1970-01-01T00:00:00.000Z"),
"optimeDurableDate" : ISODate("1970-01-01T00:00:00.000Z"),
"lastHeartbeat" : ISODate("2018-08-16T03:23:20.305Z"),
"lastHeartbeatRecv" : ISODate("2018-08-16T03:20:02.982Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "����Ŀ�������������ܾ��������ӡ�",
"configVersion" : -1
},
{
"_id" : 1,
"name" : "127.0.0.1:27018",
"health" : 1.0,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 323,
"optime" : {
"ts" : Timestamp(1534389686, 1),
"t" : NumberLong(10)
},
"optimeDate" : ISODate("2018-08-16T03:21:26.000Z"),
"infoMessage" : "could not find member to sync from",
"configVersion" : 2,
"self" : true
},
{
"_id" : 2,
"name" : "127.0.0.1:27019",
"health" : 0.0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"lastHeartbeat" : ISODate("2018-08-16T03:23:13.088Z"),
"lastHeartbeatRecv" : ISODate("2018-08-16T03:21:25.358Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "����Ŀ�������������ܾ��������ӡ�",
"configVersion" : -1
}
],
"ok" : 1.0
}
发现此时的27018 已经不是主节点了,变成了备份节点。
经过上面的实验可以让大家知道,副本集的大多数概念,一定要记住。
最后我们再次重启一个节点(27019)过一段时间按发现 27018又变回了主节点
/* 1 */
{
"set" : "haibin",
"date" : ISODate("2018-08-16T03:25:34.676Z"),
"myState" : 1,
"term" : NumberLong(11),
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1534389603, 1),
"t" : NumberLong(9)
},
"appliedOpTime" : {
"ts" : Timestamp(1534389931, 1),
"t" : NumberLong(11)
},
"durableOpTime" : {
"ts" : Timestamp(1534389931, 1),
"t" : NumberLong(11)
}
},
"members" : [
{
"_id" : 0,
"name" : "127.0.0.1:27017",
"health" : 0.0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDurable" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("1970-01-01T00:00:00.000Z"),
"optimeDurableDate" : ISODate("1970-01-01T00:00:00.000Z"),
"lastHeartbeat" : ISODate("2018-08-16T03:25:31.632Z"),
"lastHeartbeatRecv" : ISODate("2018-08-16T03:20:02.982Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "����Ŀ�������������ܾ��������ӡ�",
"configVersion" : -1
},
{
"_id" : 1,
"name" : "127.0.0.1:27018",
"health" : 1.0,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 457,
"optime" : {
"ts" : Timestamp(1534389931, 1),
"t" : NumberLong(11)
},
"optimeDate" : ISODate("2018-08-16T03:25:31.000Z"),
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1534389928, 1),
"electionDate" : ISODate("2018-08-16T03:25:28.000Z"),
"configVersion" : 2,
"self" : true
},
{
"_id" : 2,
"name" : "127.0.0.1:27019",
"health" : 1.0,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 7,
"lastHeartbeat" : ISODate("2018-08-16T03:25:34.640Z"),
"lastHeartbeatRecv" : ISODate("2018-08-16T03:25:32.362Z"),
"pingMs" : NumberLong(0),
"configVersion" : 2
}
],
"ok" : 1.0
}
截取书上的文字说明,说明下为什么要使用这样的规则:
mongo学习- 副本集 大多数原则的更多相关文章
- mongo学习- 副本集配置(事故自动转移)
一.安装mongodb 二.复制2份 三.启动 (1)mongod --config C:\mongofuben\zhu\conf\mongodb.config --replSet haibin/12 ...
- mongodb 系列 ~ mongo的副本集(3)
一 简介:今天咱们来聊聊mongodb复制的具体一些案例 二 副本集 1 当mongodb采用全量复制时,如何观察全量复制的进度 对比文件本身和primary大小 2 mongodb全量复制的过程 旧 ...
- mongodb 系列 ~ mongo的副本集(2)
一 简介:来试试更改副本集的oplog问题二 背景: oplog的作用类似于mysql的binlog,传递增量操作到从节点 三 oplog介绍 1 oplog在local库: 1 mas ...
- mongo转换副本集
本文介绍如何把独立的mongo实例转换成包含3个成员的副本集.开发和测试使用独立实例,生产使用副本集.如何安装独立的mongo实例本文不再赘述. 如果在部署副本集时还没有安装mongo实例,可以查看部 ...
- Mongo分片+副本集集群搭建
一. 概念简单描述 1. MongoDB分片集群包含组件: mongos,configserver,shardding分片 2. Mongos:路由服务是Sharded cluster的访问入口,本身 ...
- mongodb(副本集)
副本集是mongo下的一种集群配置方式: 1.通过oplog的方式将主节点数据同步到副本节点,oplog不记录查询语句(因为不改变数据): 2.mongo的副本集可以有一个主节点,多个副本节点,主节点 ...
- MongoDB副本集的搭建
副本集是mongodb提供的一种高可用解决方案.相对于原来的主从复制,副本集能自动感知primary节点的下线,并提升其中一个Secondary作为Primary. 整个过程对业务透明,同时也大大降低 ...
- mongodb副本集的从库永久性设置setSlaveOk
今天在生产环境下面搭了一个mongo的副本集,但开发那边要求副本集读写分离. 坑爹的是每次上副本集的时候都要设置db.getMongo().setSlaveOk()才能访问数据.感觉很是苦逼. 后来开 ...
- MongoDB学习笔记~Mongo集群和副本集
回到目录 一些概念 对于Mongo在数据容灾上,推荐的模式是使用副本集模式,它有一个对外的主服务器Primary,还有N个副本服务器Secondary(N>=1,当N=1时,需要有一台仲裁服务器 ...
随机推荐
- Linux驱动调试学习笔记
1. struct task_struct current->comm[16]; /*此进程可执行文件的名字!!可只对此进程执行打印*/
- web应用中Filter过滤器之开发应用
1 过滤器的简单开发应用部署 首先讲解过滤器的开发部署运行基本流程,在这里先通过一个简单的示例: 1)编写过滤器类 编写一个简单的过滤器类:SimpleFilter,实现Filter接口,完整的代码为 ...
- redis的maxmemory与maxmemory-policy关系
如果redis配置了maxmemory和maxmemory-policy策略,则当redis内存数据达到maxmemory时,会根据maxmemory-policy配置来淘汰内存数据,以避免OOM.r ...
- 显示Deprecated: Assigning the return value of new by reference is deprecated in解决办法
很多朋友的php程序当php的版本升级到5.3以后,会出现”Deprecated: Assigning the return value of new by reference is deprecat ...
- hdu 5909 Tree Cutting——点分治(树形DP转为序列DP)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5909 点分治的话,每次要做一次树形DP:但时间应该是 siz*m2 的.可以用 FWT 变成 siz*ml ...
- 【白银组】codevs_1160 蛇形矩阵
#include <iostream> using namespace std; #define M 100 int a[M][M]; void pt( int n ) { for ( i ...
- 黄聪:wordpress如何防止发布文章时候自动清除<P>、<br>换行标签
1.安装[TinyMCE Advanced]插件 2.进入[后台]--[设置]--[TinyMCE Advanced]把这个选项勾上保存即可.
- Android网络请求
HTTP请求与响应 HTTP请求包结构 例: POST /meme.php/home/user/login HTTP/1.1 Host: 114.215.86.90 Cache-Control: no ...
- bzoj4331: JSOI2012 越狱老虎桥
Description 这里,是美丽的南京:这里,是秀美的进香河:这里是安逸的老虎桥. 如果说进香河的美,美在其秀美的风光,倒不如说是美在了那惬意的南京古典小巷式生活.如果说进香河的迷人,在其淳朴的 ...
- Synergy使用(安装及配置)
最近在看一篇文章,找到了一款符合我需求的软件:Synergy. Synergy可以在多台电脑上进行鼠标与键盘及剪贴板(只能共享文本)的共享.不用每台电脑都插上这些外设了... 共享文件可以参看微软出品 ...