Replica Set 架构图
当主节点挂掉之后
复制集的搭建过程(写的比较简陋)
1、在MongoDB的安装目录下新建replicSet文件夹,以及三个以端口号命名的文件夹
2、在replicSet目录下的27000准备几个文件以及文件夹
1)db文件夹
2)27000.conf 启动服务时用到的配置文件
3)mongod启动进程
27001、27003也需要准备相同的文件以及文件夹
3、修改配置文件
port=27000
bind_ip=192.168.1.217
dbpath=./db
replSet=cat 复制集的名称
27001、27002的配置文件也需要相同的配置选项不同的配置值
4、mongod启动服务
[root@mongodb mongodb-linux-x86_64-3.4.2]# cd replicSet/27000
[root@mongodb 27000]# ./mongod -f 27000.conf
同时也需要启动27001、27002
5、初始化复制集
链接登录到192.168.1.217:27000,
在副本集中的一个且只有一个成员上使用rs.initiate():
rs.initiate()
6、验证初始副本集配置
使用rs.conf()显示副本集配置对象:
rs.conf()
/* 1 */
{
"_id" : "cat",
"version" : 1,
"protocolVersion" : NumberLong(1),
"members" : [
{
"_id" : 0,
"host" : "192.168.1.217:27000",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1.0,
"tags" : {},
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"catchUpTimeoutMillis" : 2000,
"getLastErrorModes" : {},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("58ed0068d070c232c0da4b42")
}
}
7、将剩余的成员添加到副本集
使用rs.add()方法添加剩余的成员。
rs.add("192.168.1.217:27001")
rs.add("192.168.1.217:27002")
查询状态:rs.conf
/* 1 */
{
"_id" : "cat",
"version" : 3,
"protocolVersion" : NumberLong(1),
"members" : [
{
"_id" : 0,
"host" : "192.168.1.217:27000",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1.0,
"tags" : {},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "192.168.1.217:27001",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1.0,
"tags" : {},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "192.168.1.217:27002",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1.0,
"tags" : {},
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"catchUpTimeoutMillis" : 2000,
"getLastErrorModes" : {},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("58ed0068d070c232c0da4b42")
}
}
8、检查副本集的状态
使用rs.status()操作:
rs.status()
/* 1 */
{
"set" : "cat",
"date" : ISODate("2017-04-11T16:22:27.728Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(6407780872770027, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(6407780872770027, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(6407780872770027, 1),
"t" : NumberLong(1)
}
},
"members" : [
{
"_id" : 0,
"name" : "192.168.1.217:27000",
"health" : 1.0,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1249,
"optime" : {
"ts" : Timestamp(6407780872770027, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2017-04-11T16:22:25.000Z"),
"electionTime" : Timestamp(6407778291494682, 2),
"electionDate" : ISODate("2017-04-11T16:12:24.000Z"),
"configVersion" : 3,
"self" : true
},
{
"_id" : 1,
"name" : "192.168.1.217:27001",
"health" : 1.0,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 177,
"optime" : {
"ts" : Timestamp(6407780872770027, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(6407780872770027, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2017-04-11T16:22:25.000Z"),
"optimeDurableDate" : ISODate("2017-04-11T16:22:25.000Z"),
"lastHeartbeat" : ISODate("2017-04-11T16:22:26.864Z"),
"lastHeartbeatRecv" : ISODate("2017-04-11T16:22:25.892Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "192.168.1.217:27000",
"configVersion" : 3
},
{
"_id" : 2,
"name" : "192.168.1.217:27002",
"health" : 1.0,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 177,
"optime" : {
"ts" : Timestamp(6407780872770027, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(6407780872770027, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2017-04-11T16:22:25.000Z"),
"optimeDurableDate" : ISODate("2017-04-11T16:22:25.000Z"),
"lastHeartbeat" : ISODate("2017-04-11T16:22:26.864Z"),
"lastHeartbeatRecv" : ISODate("2017-04-11T16:22:25.947Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "192.168.1.217:27001",
"configVersion" : 3
}
],
"ok" : 1.0
}