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时,需要有一台仲裁服务器 ...
随机推荐
- 什么是HBase(六)性能调优
硬件层面 内存要大,最好是ECC(Error Checking and Correcting),实现内存的动态纠错:CPU要多路(每个路彼此隔离)每个路一个CPU,每个cpu上面一般都是2~12核. ...
- 使用ntp从时间同步服务器更新centos系统时间的方法
CentOS系统时间同步的步骤如下: 复制代码 代码如下: cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtimentpdate us.pool.ntp ...
- 【python】smtp邮件发送
纯文本: #!/usr/bin/env python3 #coding: utf-8 import smtplib from email.mime.text import MIMEText from ...
- WebHttpBinding.ReaderQuotas 无法设置或者无法点出来
项目需要引用System.Runtime.Serialization.dll 才能设置各项值: binding.ReaderQuotas.MaxDepth = ; binding.ReaderQuot ...
- storyboard貌似不错
冷静下来看了下,貌似聽简单,蛋疼,忙完才发现,弄的时候咋没发现,靠 push,present等可以全部用下面这个api搞定 - (void)performSegueWithIdentifier:(NS ...
- [转]FluentData
本文来自:http://fluentdata.codeplex.com/wikipage?title=Fluency&referringTitle=Home Documentation Fl ...
- java 查询solr时间格式
solr时间格式是2015-07-06T00:00:00.0Z,所以下面是把当前时间做转换 SimpleDateFormat format = new SimpleDateFormat("y ...
- 浏览器禁用Cookie
做JavaWeb的都知道Session的底层是使用Cookie来实现的,服务器端会在本地文件中保存session信息,并将sessionID发给客户端(浏览器),浏览器就会把这个sessionID(准 ...
- Oracle的操作系统身份认证(转)
oraclelogin数据库远程登录authenticationossqlnet.authentication_services=(NTS),在$ORACLE_HOME/network/admin/s ...
- Linux下安装MATLAB
Why do I see "Preparing installation files ... Installing ... Finished..." in the terminal ...