mongoDB复制集搭建,演示

一、概念简介

1、主从复制的作用

	我们常见的主从复制的架构,有redis,mysql,zookeeper,mongodb等等,一说到这种主从复制的架构,
其主要的作用有两方面:
**a、高可用(最主要的功能)**
	如果只有一个节点,假如它挂了,那么服务就不可用了;实现主从复制,当主节点挂了,从节点可以
被选举为主节点,继续提供服务。而且,mongodb的主从选举是自动进行的,系统自带的,并不需要借助第三方软件。
**b、读写分离,提高性能**

2、mongoDb复制集群机构

PRIMARY 节点: 可以查询和新增数据
SECONDARY 节点:只能查询 不能新增  基于priority 权重可以被选为主节点
RBITER 节点: 不能查询数据 和新增数据 ,不能变成主节点(图中未画出)

在这里插入图片描述

3、mongoDb复制集节点类型

   a、主动节点:参与选举和工作,priority>0
   b、被动节点:不参与选举,只工作,prority=0
   c、仲裁节点:不参与选举和工作,只是用来执行选举工作的,提高系统的高可用性

二、mongDb复制集搭建演示

1. 主节点搭建

(服务器环境:这里以阿里云centos7服务器为环境,演示一主两从的伪集群)
a、目录结构:
在这里插入图片描述
/data : 数据目录
/conf : 配置文件目录
先在/data 目录下新建3个目录:

#主节点数据目录(primary)
mkdir repl-27017
#从节点数据目录(secondary1)
mkdir repl-27018
#从节点数据目录(secondary2)
mkdir repl-27019

在/conf目录下编写3个节点的配置
在这里插入图片描述
以主节点配置“repl-27017.conf”为例: vim repl-27017.conf

#数据目录
dbpath=/root/Hugo/tools/mongodb/mongodb-linux-x86_64-4.0.5/data/repl-27017
#启动端口
port=27017
#日志目录
logpath=/root/Hugo/tools/mongodb/mongodb-linux-x86_64-4.0.5/data/repl-27017/27017.log
#后台启动
fork=true
#复制集的名称
replSet=hugoCluster

b、启动主节点

#指定配置文件启动主节点
./bin/mongod -f conf/repl-27017.conf

在这里插入图片描述

2. 从节点搭建

参照注解点配置编写从节点的配置文件:

#直接复制一份repl-27017.conf为repl-27018.conf
cp repl-27018.conf repl-27018.conf
#编辑repl-27018.conf
vim repl-27018.conf
#批量替换27017 --》 27018
:%s/27017/27018/g

repl-27018.conf的结果如下:

#数据目录
dbpath=/root/Hugo/tools/mongodb/mongodb-linux-x86_64-4.0.5/data/repl-27018
#启动端口
port=27018
#日志目录
logpath=/root/Hugo/tools/mongodb/mongodb-linux-x86_64-4.0.5/data/repl-27018/27018.log
#后台启动
fork=true
#复制集的名称
replSet=hugoCluster

同理,repl-27019.conf的配置如下:

#数据目录
dbpath=/root/Hugo/tools/mongodb/mongodb-linux-x86_64-4.0.5/data/repl-27019
#启动端口
port=27019
#日志目录
logpath=/root/Hugo/tools/mongodb/mongodb-linux-x86_64-4.0.5/data/repl-27019/27019.log
#后台启动
fork=true
#复制集的名称
replSet=hugoCluster

启动从节点

./bin/mongod -f conf/repl-27018.conf
./bin/mongod -f conf/repl-27019.conf

在这里插入图片描述

3. 启动复制集

1. 进入任意一个节点,查看当前集群的状态:

./bin/mongo --port=27017

在这里插入图片描述

rs.status();

在这里插入图片描述
提示:集群尚未配置,其实我们刚刚启动的三个节点只是游离的节点,并没有加入到集群中

2. 初始化集群配置

a、编写配置脚本
var cfg = {
	"_id":"hugoCluster",
	"protocolVersion":1,
	"members":[
		{
			"_id":1,"host":"127.0.0.1:27017"
		},{
			"_id":2,"host":"127.0.0.1:27018"
		},{
			"_id":3,"host":"127.0.0.1:27019"
		}
	]
}

在这里插入图片描述

使用rs.initiate(cfg)初始化集群配置:

在这里插入图片描述
查看当前集群状态:rs.status 或者 rs.config() 或者 rs.isMaster()
在这里插入图片描述

往集群写数据,验证是否可用

a、先在主节点中写数据:

#新建一个test的数据库
use test
#新建emp表,往里面写数据
db.emp.insert({"name":"张三""sex":"男"})
#查看数据状态
db.emp.find()

在这里插入图片描述
在这里插入图片描述
b、进入27018的客户端,查看从节点的数据是否从主节点同步过来了

./bin/mongo --port 27018

在这里插入图片描述
在这里插入图片描述
发现提示:“not master and slaveOk=false”,从节点尚未准备好,执行rs.slaveOk()即可
在这里插入图片描述

4. 模拟自动选举

a、kill掉主节点
在这里插入图片描述
b、进入27018节点查看是否已经完成主从选举
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
mongoDB的主从选举是自动进行的,根据每个节点配置时给定的优先级priority来选举,优先级越大(数字越大)就当选为主节点,最开始的时候我们没有明确在配置cfg中指定priority的值,大家默认priority都是1,随机选一个

hugoCluster:SECONDARY> rs.config()
{
	"_id" : "hugoCluster",
	"version" : 1,
	"protocolVersion" : NumberLong(1),
	"writeConcernMajorityJournalDefault" : true,
	"members" : [
		{
			"_id" : 1,
			"host" : "127.0.0.1:27017",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 1,
			"tags" : {
				
			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		},
		{
			"_id" : 2,
			"host" : "127.0.0.1:27018",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 1,
			"tags" : {
				
			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		},
		{
			"_id" : 3,
			"host" : "127.0.0.1:27019",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 1,
			"tags" : {
				
			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		}
	],
	"settings" : {
		"chainingAllowed" : true,
		"heartbeatIntervalMillis" : 2000,
		"heartbeatTimeoutSecs" : 10,
		"electionTimeoutMillis" : 10000,
		"catchUpTimeoutMillis" : -1,
		"catchUpTakeoverDelayMillis" : 30000,
		"getLastErrorModes" : {
			
		},
		"getLastErrorDefaults" : {
			"w" : 1,
			"wtimeout" : 0
		},
		"replicaSetId" : ObjectId("5dccf89531265d40292a4892")
	}
}

现在,我们从新指定集群的配置文件cfg,为每个节点指定priority:
(注意:重新加载配置cfg,调用rs.reconfig(cfg)都要在当前的primary节点上执行,在secondary上执行会报错)
先把之前停掉的27017节点启动起来:
在这里插入图片描述

#注意:由于重新加载配置只能在当前的primary节点执行rs.reconfig(cfg),新的配置里面的分配给当前主节点的priority不能为0(前面说过,priority=0的节点是被动几点,priority>0的节点是主动节点)
#我们试错一下,当前的主节点是27019,如果priority分配为0,如下
var cfg = {
	"_id":"hugoCluster",
	"protocolVersion":1,
	"members":[
		{
			"_id":1,"host":"127.0.0.1:27017","priority":10
		},{
			"_id":2,"host":"127.0.0.1:27018","priority":5
		},{
			"_id":3,"host":"127.0.0.1:27019","priority":0
		}
	]
}

报错的场景如下:

hugoCluster:PRIMARY> var cfg = {
... "_id":"hugoCluster",
... "protocolVersion":1,
... "members":[
... {
... "_id":1,"host":"127.0.0.1:27017","priority":10
... },{
... "_id":2,"host":"127.0.0.1:27018","priority":5
... },{
... "_id":3,"host":"127.0.0.1:27019","priority":0
... }
... ]
... }
hugoCluster:PRIMARY> rs.reconfig(cfg)
{
	"operationTime" : Timestamp(1573717050, 1),
	"ok" : 0,
	"errmsg" : "This node, 127.0.0.1:27019, with _id 3 is not electable under the new configuration version 2 for replica set hugoCluster",
	"code" : 103,
	"codeName" : "NewReplicaSetConfigurationIncompatible",
	"$clusterTime" : {
		"clusterTime" : Timestamp(1573717050, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}

正确的配置如下:

hugoCluster:PRIMARY> var cfg = {
... "_id":"hugoCluster",
... "protocolVersion":1,
... "members":[
... {
... "_id":1,"host":"127.0.0.1:27017","priority":10
... },{
... "_id":2,"host":"127.0.0.1:27018","priority":0
... },{
... "_id":3,"host":"127.0.0.1:27019","priority":5
... }
... ]
... }
hugoCluster:PRIMARY> rs.reconfig(cfg)
{
	"ok" : 1,
	"operationTime" : Timestamp(1573717096, 1),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1573717096, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}

然后查看重新加载配置后的集群的主从节点的分布情况:

hugoCluster:SECONDARY> rs.isMaster()
{
	"hosts" : [
		"127.0.0.1:27017",
		"127.0.0.1:27019"
	],
	"passives" : [
		"127.0.0.1:27018"
	],
	"setName" : "hugoCluster",
	"setVersion" : 2,
	"ismaster" : false,
	"secondary" : true,
	"primary" : "127.0.0.1:27017",
	"me" : "127.0.0.1:27019",
	"lastWrite" : {
		"opTime" : {
			"ts" : Timestamp(1573717138, 1),
			"t" : NumberLong(3)
		},
		"lastWriteDate" : ISODate("2019-11-14T07:38:58Z"),
		"majorityOpTime" : {
			"ts" : Timestamp(1573717138, 1),
			"t" : NumberLong(3)
		},
		"majorityWriteDate" : ISODate("2019-11-14T07:38:58Z")
	},
	"maxBsonObjectSize" : 16777216,
	"maxMessageSizeBytes" : 48000000,
	"maxWriteBatchSize" : 100000,
	"localTime" : ISODate("2019-11-14T07:39:02.498Z"),
	"logicalSessionTimeoutMinutes" : 30,
	"minWireVersion" : 0,
	"maxWireVersion" : 7,
	"readOnly" : false,
	"ok" : 1,
	"operationTime" : Timestamp(1573717138, 1),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1573717138, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}

如图上所示,priority=10的27017节点重新单选为主节点了。
由于现在的27019变成了从节点了,也是需要重新确认从节点的状态:

hugoCluster:SECONDARY> show dbs;
2019-11-14T15:53:24.665+0800 E QUERY    [js] Error: listDatabases failed:{
	"operationTime" : Timestamp(1573717998, 1),
	"ok" : 0,
	"errmsg" : "not master and slaveOk=false",
	"code" : 13435,
	"codeName" : "NotMasterNoSlaveOk",
	"$clusterTime" : {
		"clusterTime" : Timestamp(1573717998, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:124:1
shellHelper.show@src/mongo/shell/utils.js:876:19
shellHelper@src/mongo/shell/utils.js:766:15
@(shellhelp2):1:1
hugoCluster:SECONDARY> rs.slaveOk()
hugoCluster:SECONDARY> show dbs;
admin   0.000GB
config  0.000GB
local   0.000GB
test    0.000GB
hugoCluster:SECONDARY> use test;
switched to db test
hugoCluster:SECONDARY> show tables;
emp
hugoCluster:SECONDARY> db.emp.find()
{ "_id" : ObjectId("5dccfadbb54d7680cf572050"), "name" : "张三", "sex" : "男" }

c、往当前集群中加入仲裁节点
比如:新增27020作为仲裁节点

[root@hugo conf]# ls
mongo.conf  repl-27017.conf  repl-27018.conf  repl-27019.conf  repl-27020.conf
#repl-27020.conf的配置
dbpath=/root/Hugo/tools/mongodb/mongodb-linux-x86_64-4.0.5/data/repl-27020
port=27020
logpath=/root/Hugo/tools/mongodb/mongodb-linux-x86_64-4.0.5/data/repl-27020/27020.log
fork=true
replSet=hugoCluster
#启动27020节点
[root@hugo mongodb-linux-x86_64-4.0.5]# ls
bin  conf  data  LICENSE-Community.txt  log  MPL-2  README  THIRD-PARTY-NOTICES
[root@hugo mongodb-linux-x86_64-4.0.5]# ./bin/mongod -f conf/repl-27020.conf 
about to fork child process, waiting until server is ready for connections.
forked process: 2448
child process started successfully, parent exiting
[root@hugo mongodb-linux-x86_64-4.0.5]# ps -ef|grep mongo
root      1201     1  0 15:32 ?        00:00:12 ./bin/mongod -f conf/repl-27017.conf
root      1318 31680  0 15:34 pts/0    00:00:00 ./bin/mongo --port 27019
root      2145   344  0 16:09 pts/1    00:00:00 ./bin/mongo --port 27017
root      2448     1  8 16:18 ?        00:00:00 ./bin/mongod -f conf/repl-27020.conf
root      2476  2202  0 16:18 pts/3    00:00:00 grep --color=auto mongo
root     27287     1  0 11:00 ?        00:01:06 ./bin/mongod -f conf/repl-27018.conf
root     27384     1  0 11:02 ?        00:01:04 ./bin/mongod -f conf/repl-27019.conf

从新加载集群的配置文件(新增27020为仲裁节点):

hugoCluster:PRIMARY> var cfg = {
... "_id":"hugoCluster",
... "protocolVersion":1,
... "members":[
... {
... "_id":1,"host":"127.0.0.1:27017","priority":10
... },{
... "_id":2,"host":"127.0.0.1:27018","priority":0
... },{
... "_id":3,"host":"127.0.0.1:27019","priority":5
... },{
... "_id":4,"host":"127.0.0.1:27020","arbiterOnly":true
... }
... ]
... }
hugoCluster:PRIMARY> rs.reconfig(cfg)
{
	"ok" : 1,
	"operationTime" : Timestamp(1573719620, 1),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1573719620, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}
hugoCluster:PRIMARY> rs.isMaster()
{
	"hosts" : [
		"127.0.0.1:27017",
		"127.0.0.1:27019"
	],
	"passives" : [
		"127.0.0.1:27018"
	],
	"arbiters" : [
		"127.0.0.1:27020"
	],
	"setName" : "hugoCluster",
	"setVersion" : 3,
	"ismaster" : true,
	"secondary" : false,
	"primary" : "127.0.0.1:27017",
	"me" : "127.0.0.1:27017",
	"electionId" : ObjectId("7fffffff0000000000000003"),
	"lastWrite" : {
		"opTime" : {
			"ts" : Timestamp(1573719638, 1),
			"t" : NumberLong(3)
		},
		"lastWriteDate" : ISODate("2019-11-14T08:20:38Z"),
		"majorityOpTime" : {
			"ts" : Timestamp(1573719638, 1),
			"t" : NumberLong(3)
		},
		"majorityWriteDate" : ISODate("2019-11-14T08:20:38Z")
	},
	"maxBsonObjectSize" : 16777216,
	"maxMessageSizeBytes" : 48000000,
	"maxWriteBatchSize" : 100000,
	"localTime" : ISODate("2019-11-14T08:20:48.375Z"),
	"logicalSessionTimeoutMinutes" : 30,
	"minWireVersion" : 0,
	"maxWireVersion" : 7,
	"readOnly" : false,
	"ok" : 1,
	"operationTime" : Timestamp(1573719638, 1),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1573719638, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}

仲裁节点不存储数据,也对外提供任何读写服务,只是用来进行选举工作的,生产上提高 系统的稳定性:

#进入到27020节点
[root@hugo mongodb-linux-x86_64-4.0.5]# ./bin/mongo --port 27020
#查看当前27020的状态
hugoCluster:ARBITER> show dbs;
2019-11-14T16:23:08.769+0800 E QUERY    [js] Error: listDatabases failed:{
	"ok" : 0,
	"errmsg" : "not master and slaveOk=false",
	"code" : 13435,
	"codeName" : "NotMasterNoSlaveOk"
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:124:1
shellHelper.show@src/mongo/shell/utils.js:876:19
shellHelper@src/mongo/shell/utils.js:766:15
@(shellhelp2):1:1
hugoCluster:ARBITER> rs.slaveOk()
#可以看到,仲裁节点是没有存储数据的
hugoCluster:ARBITER> show dbs;
local  0.000GB
hugoCluster:ARBITER> use local;
switched to db local
#startup_log存储的是启动信息
hugoCluster:ARBITER> show tables;
replset.minvalid
replset.oplogTruncateAfterPoint
startup_log

进入到primary节点查看local数据库里面的表,可以看到用于主从同步数据的oplog.rs表:

hugoCluster:PRIMARY> use local;
switched to db local
hugoCluster:PRIMARY> show tables;
oplog.rs
replset.election
replset.minvalid
replset.oplogTruncateAfterPoint
startup_log
hugoCluster:PRIMARY> db.oplog.rs.find()
{ "ts" : Timestamp(1573714069, 1), "h" : NumberLong("9218491020065173464"), "v" : 2, "op" : "n", "ns" : "", "wall" : ISODate("2019-11-14T06:47:49.743Z"), "o" : { "msg" : "initiating set" } }
{ "ts" : Timestamp(1573714081, 1), "t" : NumberLong(1), "h" : NumberLong("3917468659492125070"), "v" : 2, "op" : "n", "ns" : "", "wall" : ISODate("2019-11-14T06:48:01.764Z"), "o" : { "msg" : "new primary" } }
{ "ts" : Timestamp(1573714081, 2), "t" : NumberLong(1), "h" : NumberLong("8169843132886441680"), "v" : 2, "op" : "c", "ns" : "config.$cmd", "ui" : UUID("c2eeac3d-6d4a-42e4-b717-ed97c9d71463"), "wall" : ISODate("2019-11-14T06:48:01.774Z"), "o" : { "create" : "transactions", "idIndex" : { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "config.transactions" } } }
{ "ts" : Timestamp(1573714081, 3), "t" : NumberLong(1), "h" : NumberLong("-721299786089761262"), "v" : 2, "op" : "c", "ns" : "admin.$cmd", "ui" : UUID("2a88dbce-e73b-4996-aada-7bb994d2d2b2"), "wall" : ISODate("2019-11-14T06:48:01.796Z"), "o" : { "create" : "system.keys", "idIndex" : { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "admin.system.keys" } } }
{ "ts" : Timestamp(1573714081, 4), "t" : NumberLong(1), "h" : NumberLong("-1117689384358166164"), "v" : 2, "op" : "i", "ns" : "admin.system.keys", "ui" : UUID("2a88dbce-e73b-4996-aada-7bb994d2d2b2"), "wall" : ISODate("2019-11-14T06:48:01.796Z"), "o" : { "_id" : NumberLong("6759050511149694978"), "purpose" : "HMAC", "key" : BinData(0,"HJ8g0mFoMmccUtHZT5H2TeS0YH0="), "expiresAt" : Timestamp(1581490081, 0) } }
{ "ts" : Timestamp(1573714082, 1), "t" : NumberLong(1), "h" : NumberLong("2492156872741532766"), "v" : 2, "op" : "i", "ns" : "admin.system.keys", "ui" : UUID("2a88dbce-e73b-4996-aada-7bb994d2d2b2"), "wall" : ISODate("2019-11-14T06:48:02.916Z"), "o" : { "_id" : NumberLong("6759050511149694979"), "purpose" : "HMAC", "key" : BinData(0,"Rx1U/O8WhW9Z2g6sboGA84kZ/5Y="), "expiresAt" : Timestamp(1589266081, 0) } }
{ "ts" : Timestamp(1573714100, 1), "t" : NumberLong(1), "h" : NumberLong("-2489987046676372710"), "v" : 2, "op" : "c", "ns" : "config.$cmd", "ui" : UUID("3f2654f4-b328-47d6-b1b6-d22ca20df2a5"), "wall" : ISODate("2019-11-14T06:48:20.793Z"), "o" : { "create" : "system.sessions", "idIndex" : { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "config.system.sessions" } } }
{ "ts" : Timestamp(1573714100, 2), "t" : NumberLong(1), "h" : NumberLong("-5277091518919098373"), "v" : 2, "op" : "n", "ns" : "", "wall" : ISODate("2019-11-14T06:48:20.813Z"), "o" : { "msg" : "Creating indexes. Coll: config.system.sessions" } }
{ "ts" : Timestamp(1573714100, 3), "t" : NumberLong(1), "h" : NumberLong("6121659563133314631"), "v" : 2, "op" : "c", "ns" : "config.$cmd", "ui" : UUID("3f2654f4-b328-47d6-b1b6-d22ca20df2a5"), "wall" : ISODate("2019-11-14T06:48:20.813Z"), "o" : { "createIndexes" : "system.sessions", "v" : 2, "key" : { "lastUse" : 1 }, "name" : "lsidTTLIndex", "expireAfterSeconds" : 1800 } }
{ "ts" : Timestamp(1573714100, 4), "t" : NumberLong(1), "h" : NumberLong("-4164658975870537248"), "v" : 2, "op" : "i", "ns" : "config.system.sessions", "ui" : UUID("3f2654f4-b328-47d6-b1b6-d22ca20df2a5"), "wall" : ISODate("2019-11-14T06:48:20.813Z"), "o" : { "_id" : { "id" : UUID("a7eb3312-0052-4b17-a65a-3f04127f9f10"), "uid" : BinData(0,"47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=") }, "lastUse" : ISODate("2019-11-14T06:48:20.822Z") } }
{ "ts" : Timestamp(1573714100, 5), "t" : NumberLong(1), "h" : NumberLong("-244386783764177079"), "v" : 2, "op" : "i", "ns" : "config.system.sessions", "ui" : UUID("3f2654f4-b328-47d6-b1b6-d22ca20df2a5"), "wall" : ISODate("2019-11-14T06:48:20.813Z"), "o" : { "_id" : { "id" : UUID("9997edac-e7ba-4217-9eee-8ef5dad859e6"), "uid" : BinData(0,"47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=") }, "lastUse" : ISODate("2019-11-14T06:48:20.823Z") } }
{ "ts" : Timestamp(1573714111, 1), "t" : NumberLong(1), "h" : NumberLong("-5550391087800828872"), "v" : 2, "op" : "n", "ns" : "", "wall" : ISODate("2019-11-14T06:48:31.780Z"), "o" : { "msg" : "periodic noop" } }
{ "ts" : Timestamp(1573714121, 1), "t" : NumberLong(1), "h" : NumberLong("6739196509230376618"), "v" : 2, "op" : "n", "ns" : "", "wall" : ISODate("2019-11-14T06:48:41.781Z"), "o" : { "msg" : "periodic noop" } }
{ "ts" : Timestamp(1573714131, 1), "t" : NumberLong(1), "h" : NumberLong("-8152473928276068552"), "v" : 2, "op" : "n", "ns" : "", "wall" : ISODate("2019-11-14T06:48:51.781Z"), "o" : { "msg" : "periodic noop" } }
{ "ts" : Timestamp(1573714141, 1), "t" : NumberLong(1), "h" : NumberLong("-8724563208116701571"), "v" : 2, "op" : "n", "ns" : "", "wall" : ISODate("2019-11-14T06:49:01.781Z"), "o" : { "msg" : "periodic noop" } }
{ "ts" : Timestamp(1573714151, 1), "t" : NumberLong(1), "h" : NumberLong("-3623885859191131909"), "v" : 2, "op" : "n", "ns" : "", "wall" : ISODate("2019-11-14T06:49:11.781Z"), "o" : { "msg" : "periodic noop" } }
{ "ts" : Timestamp(1573714161, 1), "t" : NumberLong(1), "h" : NumberLong("480760306959478597"), "v" : 2, "op" : "n", "ns" : "", "wall" : ISODate("2019-11-14T06:49:21.781Z"), "o" : { "msg" : "periodic noop" } }
{ "ts" : Timestamp(1573714171, 1), "t" : NumberLong(1), "h" : NumberLong("-3366586106124003522"), "v" : 2, "op" : "n", "ns" : "", "wall" : ISODate("2019-11-14T06:49:31.782Z"), "o" : { "msg" : "periodic noop" } }
{ "ts" : Timestamp(1573714181, 1), "t" : NumberLong(1), "h" : NumberLong("-7459162185324693362"), "v" : 2, "op" : "n", "ns" : "", "wall" : ISODate("2019-11-14T06:49:41.782Z"), "o" : { "msg" : "periodic noop" } }
{ "ts" : Timestamp(1573714191, 1), "t" : NumberLong(1), "h" : NumberLong("-5781994448740727025"), "v" : 2, "op" : "n", "ns" : "", "wall" : ISODate("2019-11-14T06:49:51.782Z"), "o" : { "msg" : "periodic noop" } }
Type "it" for more

可以动态的修改集群节点,比如,我先把27019的节点从集群中移除,然后再添加进来:

hugoCluster:PRIMARY> rs.isMaster()
{
	"hosts" : [
		"127.0.0.1:27017",
		"127.0.0.1:27019"
	],
	"passives" : [
		"127.0.0.1:27018"
	],
	"arbiters" : [
		"127.0.0.1:27020"
	],
	"setName" : "hugoCluster",
	"setVersion" : 3,
	"ismaster" : true,
	"secondary" : false,
	"primary" : "127.0.0.1:27017",
	"me" : "127.0.0.1:27017",
	"electionId" : ObjectId("7fffffff0000000000000003"),
	"lastWrite" : {
		"opTime" : {
			"ts" : Timestamp(1573720528, 1),
			"t" : NumberLong(3)
		},
		"lastWriteDate" : ISODate("2019-11-14T08:35:28Z"),
		"majorityOpTime" : {
			"ts" : Timestamp(1573720528, 1),
			"t" : NumberLong(3)
		},
		"majorityWriteDate" : ISODate("2019-11-14T08:35:28Z")
	},
	"maxBsonObjectSize" : 16777216,
	"maxMessageSizeBytes" : 48000000,
	"maxWriteBatchSize" : 100000,
	"localTime" : ISODate("2019-11-14T08:35:36.519Z"),
	"logicalSessionTimeoutMinutes" : 30,
	"minWireVersion" : 0,
	"maxWireVersion" : 7,
	"readOnly" : false,
	"ok" : 1,
	"operationTime" : Timestamp(1573720528, 1),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1573720528, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}
hugoCluster:PRIMARY> rs.remove("127.0.0.1:27019")
{
	"ok" : 1,
	"operationTime" : Timestamp(1573720561, 1),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1573720561, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}
hugoCluster:PRIMARY> rs.isMaster()
{
	"hosts" : [
		"127.0.0.1:27017"
	],
	"passives" : [
		"127.0.0.1:27018"
	],
	"arbiters" : [
		"127.0.0.1:27020"
	],
	"setName" : "hugoCluster",
	"setVersion" : 4,
	"ismaster" : true,
	"secondary" : false,
	"primary" : "127.0.0.1:27017",
	"me" : "127.0.0.1:27017",
	"electionId" : ObjectId("7fffffff0000000000000003"),
	"lastWrite" : {
		"opTime" : {
			"ts" : Timestamp(1573720561, 1),
			"t" : NumberLong(3)
		},
		"lastWriteDate" : ISODate("2019-11-14T08:36:01Z"),
		"majorityOpTime" : {
			"ts" : Timestamp(1573720561, 1),
			"t" : NumberLong(3)
		},
		"majorityWriteDate" : ISODate("2019-11-14T08:36:01Z")
	},
	"maxBsonObjectSize" : 16777216,
	"maxMessageSizeBytes" : 48000000,
	"maxWriteBatchSize" : 100000,
	"localTime" : ISODate("2019-11-14T08:36:09.693Z"),
	"logicalSessionTimeoutMinutes" : 30,
	"minWireVersion" : 0,
	"maxWireVersion" : 7,
	"readOnly" : false,
	"ok" : 1,
	"operationTime" : Timestamp(1573720561, 1),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1573720561, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}
hugoCluster:PRIMARY> rs.add("127.0.0.1:27019")
{
	"ok" : 1,
	"operationTime" : Timestamp(1573720589, 1),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1573720589, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}
hugoCluster:PRIMARY> rs.isMaster()
{
	"hosts" : [
		"127.0.0.1:27017",
		"127.0.0.1:27019"
	],
	"passives" : [
		"127.0.0.1:27018"
	],
	"arbiters" : [
		"127.0.0.1:27020"
	],
	"setName" : "hugoCluster",
	"setVersion" : 5,
	"ismaster" : true,
	"secondary" : false,
	"primary" : "127.0.0.1:27017",
	"me" : "127.0.0.1:27017",
	"electionId" : ObjectId("7fffffff0000000000000003"),
	"lastWrite" : {
		"opTime" : {
			"ts" : Timestamp(1573720589, 1),
			"t" : NumberLong(3)
		},
		"lastWriteDate" : ISODate("2019-11-14T08:36:29Z"),
		"majorityOpTime" : {
			"ts" : Timestamp(1573720589, 1),
			"t" : NumberLong(3)
		},
		"majorityWriteDate" : ISODate("2019-11-14T08:36:29Z")
	},
	"maxBsonObjectSize" : 16777216,
	"maxMessageSizeBytes" : 48000000,
	"maxWriteBatchSize" : 100000,
	"localTime" : ISODate("2019-11-14T08:36:31.955Z"),
	"logicalSessionTimeoutMinutes" : 30,
	"minWireVersion" : 0,
	"maxWireVersion" : 7,
	"readOnly" : false,
	"ok" : 1,
	"operationTime" : Timestamp(1573720589, 1),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1573720589, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值