在自己的本机创建有三个节点的副本集 (一个主节点,两个从节点),是基于MongoDB 3.6 版本
-
创建数据存储路径
mkdir r0 mkdir r1 mkdir r2
-
创建log存储路径
mkdir log
-
启动第一个节点
mongod --replSet rs0 --port 27017 --bind_ip 127.0.0.1 --dbpath .\r0\ --logpath log\r0.log --logappend
-
启动第二个节点
mongod --replSet rs0 --port 27018 --bind_ip 127.0.0.1 --dbpath .\r1\ --logpath log\r1.log --logappend
-
启动第三个节点
mongod --replSet rs0 --port 27017 --bind_ip 127.0.0.1 --dbpath .\r2\ --logpath log\r2.log --logappend
-
连接到
mongod
实例mongo --port 27017
在
mongo
shell中,使用rs.initiate()
初始化副本集
rsconf = {
_id: "rs0",
members: [
{
_id: 0,
host: "127.0.1:27017"
},
{
_id: 1,
host: "127.0.1:27018"
},
{
_id: 2,
host: "127.0.1:27019"
}
]
}
rs.initiate(rsconf)
初始化成功
{
"ok" : 1,
"operationTime" : Timestamp(1524728214, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1524728214, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
- 查看副本集的状态
27017:是主节点
27018 27019是从节点rs.status() { "set" : "rs0", "date" : ISODate("2018-04-26T08:15:11.241Z"), "myState" : 1, ... "members" : [ { "_id" : 0, "name" : "127.0.0.1:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", ... }, { "_id" : 1, "name" : "127.0.0.1:27018", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", ... }, { "_id" : 2, "name" : "127.0.0.1:27019", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", ... } ], "ok" : 1, ... }
- 查看节点是否是主节点
rs.isMaster()
{
"hosts" : [
"127.0.0.1:27017",
"127.0.0.1:27018",
"127.0.0.1:27019"
],
"setName" : "rs0",
"setVersion" : 1,
"ismaster" : true,
"secondary" : false,
"primary" : "127.0.0.1:27017",
"me" : "127.0.0.1:27017",
"electionId" : ObjectId("7fffffff0000000000000003"),
...
}
-
读写操作
先在主节点创建一个数据库并写入数据use TestDB db.createCollection("TestModel") db.getCollection("TestModel").insert({"first_name":"Lorry","second_name":"kevin"})
使用从节点进行查询
mongo --port 27019
无法进行查询的操作 ,从节点无法进行查询操作
rs0:SECONDARY> show dbs
2018-04-26T16:20:35.769+0800 E QUERY [thread1] Error: listDatabases failed:{
"operationTime" : Timestamp(1524730833, 1),
"ok" : 0,
"errmsg" : "not master and slaveOk=false",
"code" : 13435,
"codeName" : "NotMasterNoSlaveOk",
...
} :
配置从节点可以进行查询
db.getMongo().setSlaveOk()
rs0:SECONDARY> show dbs
TestDB 0.000GB
admin 0.000GB
config 0.000GB
local 0.000GB
-
故障转移
把主节点端口停掉,再查看对应的副本集的状态
rs.status()
27017 : "stateStr" : "(not reachable/healthy)"
27018 : "stateStr" : "PRIMARY",
27019 : "stateStr" : "SECONDARY",
27018 变成了主节点
{
"set" : "rs0",
"date" : ISODate("2018-04-26T08:24:24.740Z"),
"myState" : 2,
"term" : NumberLong(4),
...
"members" : [
{
"_id" : 0,
"name" : "127.0.0.1:27017",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
...
},
{
"_id" : 1,
"name" : "127.0.0.1:27018",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
...
},
{
"_id" : 2,
"name" : "127.0.0.1:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
...
}
],
"ok" : 1,
...
}
-
增加节点
mkdir r3
-
启动新节点
mongod --relpSet rs0 --port 27020 --bind_ip 127.0.0.1 --dbpath .\r3\
增加节点
rs.add("127.0.0.1:27020") { "ok" : 1, "operationTime" : Timestamp(1524731467, 1), "$clusterTime" : { "clusterTime" : Timestamp(1524731467, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } }
在查看副本集的状态,可以看到 27020
的新节点, 连接到新的节点查看,数据已经同步了
- 删除节点
rs.remove("127.0.0.1:27020")
{
"ok" : 1,
"operationTime" : Timestamp(1524731753, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1524731753, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
再查看一下当前副本集的状态,可以看到 27020的节点已经被删除了