1.创建目录
mkdir -p /mongodb/data/r1
mkdir -p /mongodb/data/r2
mkdir -p /mongodb/data/r3
mkdir -p /mongodb/mlog
2.启动三个实例,并声明实例属于复制集
./mongod --dbpath /mongodb/data/r1 --logpath /mongodb/mlog/r1.log --fork --port 27017 --replSet ybb --smallfiles
./mongod --dbpath /mongodb/data/r2 --logpath /mongodb/mlog/r2.log --fork --port 27018 --replSet ybb --smallfiles
./mongod --dbpath /mongodb/data/r3 --logpath /mongodb/mlog/r3.log --fork --port 27019 --replSet ybb --smallfiles
3.复制集初始化
这一步初始化的复制集只是包含当前节点
# ./mongo --port=27017
> rs.initiate()
{
"info2" : "no configuration specified. Using a default configuration for the set",
"me" : "cc.example.com:27017",
"ok" : 1
}
4.验证复制集初始化配置
这一步初始化的复制集只是包含当前节点
ybb:OTHER> rs.conf()
{
"_id" : "ybb",
"version" : 1,
"protocolVersion" : NumberLong(1),
"members" : [
{
"_id" : 0,
"host" : "cc.example.com:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : { },
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"getLastErrorModes" : { },
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
}
}
}
ybb:PRIMARY>
5.添加其它节点
ybb:PRIMARY> rs.add("cc.example.com:27018")rs.add("cc.example.com:27018")
{ "ok" : 1 }
ybb:PRIMARY> rs.add("cc.example.com:27019")rs.add("cc.example.com:27019")
{ "ok" : 1 }
ybb:PRIMARY>
6.检查复制集的状态
ybb:PRIMARY> rs.status()rs.status()
{
"set" : "ybb",
"date" : ISODate("2016-03-15T05:26:06.682Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"members" : [
{
"_id" : 0,
"name" : "cc.example.com:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 482,
"optime" : {
"ts" : Timestamp(1458019522, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2016-03-15T05:25:22Z"),
"electionTime" : Timestamp(1458019202, 2),
"electionDate" : ISODate("2016-03-15T05:20:02Z"),
"configVersion" : 3,
"self" : true
},
{
"_id" : 1,
"name" : "cc.example.com:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 47,
"optime" : {
"ts" : Timestamp(1458019522, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2016-03-15T05:25:22Z"),
"lastHeartbeat" : ISODate("2016-03-15T05:26:04.822Z"),
"lastHeartbeatRecv" : ISODate("2016-03-15T05:26:05.826Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "cc.example.com:27017",
"configVersion" : 3
},
{
"_id" : 2,
"name" : "cc.example.com:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 43,
"optime" : {
"ts" : Timestamp(1458019522, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2016-03-15T05:25:22Z"),
"lastHeartbeat" : ISODate("2016-03-15T05:26:04.822Z"),
"lastHeartbeatRecv" : ISODate("2016-03-15T05:26:04.821Z"),
"pingMs" : NumberLong(0),
"configVersion" : 3
}
],
"ok" : 1
}
ybb:PRIMARY>
7.查看复制集的最新配置
ybb:PRIMARY> rs.config();rs.config();
{
"_id" : "ybb",
"version" : 3,
"protocolVersion" : NumberLong(1),
"members" : [
{
"_id" : 0,
"host" : "cc.example.com:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : { },
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "cc.example.com:27018",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : { },
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "cc.example.com:27019",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : { },
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"getLastErrorModes" : { },
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
}
}
}
ybb:PRIMARY>