PS:因为条件有限,只能使用单机器多实例集群搭建,多机器配置一样
搭建一个三节点的集群,一个主副本节点一个辅助副本节点,还有一个仲裁节点,仲裁节点作用主要是在当一台机器DOWN掉时,投票机制还能正常运行,所以使用仲裁节点满足过半票数的要求。
预先准备配置:
三个节点使用不同的数据目录,所以预先创建三个数据目录:
/data/db1
/data/db2
/data/db3
还有三个日志目录
/var/log/mongod1
/var/log/mongod2
/var/log/mongod3
准备配置文件mongod1.conf如下:
systemLog:
destination: file
path: "/var/log/mongodb1/mongod.log"
logAppend: true
storage:
dbPath: /data/db1
journal:
enabled: true
processManagement:
fork: true
security:
authorization: disabled
net:
bindIp: 127.0.0.1,192.168.70.128
port: 27017
setParameter:
enableLocalhostAuthBypass: false
replication:
replSetName: test
并复制两份分别为mongod2.conf和mongod3.conf 内容如下:
mongod2.conf :
systemLog:
destination: file
path: "/var/log/mongodb2/mongod.log"
logAppend: true
storage:
dbPath: /data/db2
journal:
enabled: true
processManagement:
fork: true
security:
authorization: disabled
net:
bindIp: 127.0.0.1,192.168.70.128
port: 27018
setParameter:
enableLocalhostAuthBypass: false
replication:
replSetName: test
mongod3.conf :
systemLog:
destination: file
path: "/var/log/mongodb3/mongod.log"
logAppend: true
storage:
dbPath: /data/db3
journal:
enabled: true
processManagement:
fork: true
security:
authorization: disabled
net:
bindIp: 127.0.0.1,192.168.70.128
port: 27019
setParameter:
enableLocalhostAuthBypass: false
replication:
replSetName: test
然后根据配置文件分别启动三个实例 :
/mongod --config /conf/mongod1.conf
/mongod --config /conf/mongod2.conf
/mongod --config /conf/mongod3.conf
目前配置集群的前期准备工作已经完成。下面开始初始化副本集:
我们选择第一台机连接执行以下命令:
cfg={
_id:'test',
members:[
{_id:0,host:'192.168.70.128:27017'},
{_id:1,host:'192.168.70.128:27018'},
{_id:2,host:'192.168.70.128:27019',arbiterOnly:true}
]
}
rs.initiate(cfg)
服务器返回表示成功:
{
"ok" : 1,
"operationTime" : Timestamp(1531351517, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1531351517, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
执行rs.status()查看集群信息:
{
"set" : "test",
"date" : ISODate("2018-07-11T23:25:32.808Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1531351531, 1),
"t" : NumberLong(1)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1531351531, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1531351531, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1531351531, 1),
"t" : NumberLong(1)
}
},
"members" : [
{
"_id" : 0,
"name" : "192.168.70.128:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 194,
"optime" : {
"ts" : Timestamp(1531351531, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-07-11T23:25:31Z"),
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1531351528, 1),
"electionDate" : ISODate("2018-07-11T23:25:28Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 1,
"name" : "192.168.70.128:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 15,
"optime" : {
"ts" : Timestamp(1531351517, 1),
"t" : NumberLong(-1)
},
"optimeDurable" : {
"ts" : Timestamp(1531351517, 1),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("2018-07-11T23:25:17Z"),
"optimeDurableDate" : ISODate("2018-07-11T23:25:17Z"),
"lastHeartbeat" : ISODate("2018-07-11T23:25:30.816Z"),
"lastHeartbeatRecv" : ISODate("2018-07-11T23:25:30.121Z"),
"pingMs" : NumberLong(0),
"configVersion" : 1
},
{
"_id" : 2,
"name" : "192.168.70.128:27019",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 15,
"lastHeartbeat" : ISODate("2018-07-11T23:25:30.816Z"),
"lastHeartbeatRecv" : ISODate("2018-07-11T23:25:29.592Z"),
"pingMs" : NumberLong(0),
"configVersion" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1531351531, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1531351531, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
信息中显示27017为主节点,27018为辅助节点,27019为仲裁节点,
试着把当前主节点关闭验证集群状态,在27017执行db.shutdownServer();
关闭成功后,登录27018执行rs.status()查看集群状态:
test:PRIMARY> rs.status()rs.status()
{
"set" : "test",
"date" : ISODate("2018-07-11T23:35:05.728Z"),
"myState" : 1,
"term" : NumberLong(2),
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1531352009, 1),
"t" : NumberLong(1)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1531352009, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1531352101, 1),
"t" : NumberLong(2)
},
"durableOpTime" : {
"ts" : Timestamp(1531352101, 1),
"t" : NumberLong(2)
}
},
"members" : [
{
"_id" : 0,
"name" : "192.168.70.128:27017",
"health" : 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:00Z"),
"optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2018-07-11T23:35:04.928Z"),
"lastHeartbeatRecv" : ISODate("2018-07-11T23:33:29.998Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "Connection refused",
"configVersion" : -1
},
{
"_id" : 1,
"name" : "192.168.70.128:27018",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 660,
"optime" : {
"ts" : Timestamp(1531352101, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2018-07-11T23:35:01Z"),
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1531352020, 1),
"electionDate" : ISODate("2018-07-11T23:33:40Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 2,
"name" : "192.168.70.128:27019",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 586,
"lastHeartbeat" : ISODate("2018-07-11T23:35:04.877Z"),
"lastHeartbeatRecv" : ISODate("2018-07-11T23:35:04.854Z"),
"pingMs" : NumberLong(0),
"configVersion" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1531352101, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1531352101, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
可以看到27018节点已经提升为PRIMARY节点 ,自动提升主节点正常,
重新启动27017节点,执行rs.status()可以看到节点启动后自动成为SECONDARY节点
到此集群配置成功
后面可以各自扩展配置信息,比如身份验证,oplog默认大小