创建必要的目录和认证文件:
[root@mongo mongo]# mkdir keyfile
[root@mongo mongo]# echo "mongo cluster one" > keyfile/m1
[root@mongo mongo]# echo "mongo cluster one" > keyfile/m2
[root@mongo mongo]# echo "mongo cluster one" > keyfile/m3
启动不同端口的服务
mongod --replSet mco --keyFile /usr/local/mongo/keyfile/m1 --fork --port 28010 --dbpath=/usr/local/mongo/data0/ --logpath=/usr/local/mongo/log/m1.log --logappend
mongod --replSet mco --keyFile /usr/local/mongo/keyfile/m2 --fork --port 28011 --dbpath=/usr/local/mongo/data1/ --logpath=/usr/local/mongo/log/m2.log --logappend
mongod --replSet mco --keyFile /usr/local/mongo/keyfile/m3 --fork --port 28012 --dbpath=/usr/local/mongo/data2/ --logpath=/usr/local/mongo/log/m3.log --logappend
mongod --replSet mco --keyFile /usr/local/mongo/keyfile/m4 --fork --port 28013 --dbpath=/usr/local/mongo/data3/ --logpath=/usr/local/mongo/log/m4.log --logappend
mongod --replSet mco --keyFile /usr/local/mongo/keyfile/m5 --fork --port 28014 --dbpath=/usr/local/mongo/data4/ --logpath=/usr/local/mongo/log/m5.log --logappend --fastsync
配置主从
> config_mco={_id:'mco',members:[
... {_id:0,host:'localhost:28010',priority:1},
... {_id:1,host:'localhost:28011'},
... {_id:2,host:'localhost:28012'}]
... }
{
"_id" : "mco",
"members" : [
{
"_id" : 0,
"host" : "localhost:28010",
"priority" : 1
},
{
"_id" : 1,
"host" : "localhost:28011"
},
{
"_id" : 2,
"host" : "localhost:28012"
}
]
}
初始化主从
> rs.initiate(config_mco)
{
"info" : "Config now saved locally. Should come online in about a minute.",
"ok" : 1
}
查看集群状态
> rs.status()rs.status()
{
"set" : "mco",
"date" : ISODate("2012-12-01T03:31:14Z"),
"myState" : 2,
"members" : [
{
"_id" : 0,
"name" : "localhost:28010",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1047,
"optime" : Timestamp(1354332643000, 1),
"optimeDate" : ISODate("2012-12-01T03:30:43Z"),
"self" : true
},
{
"_id" : 1,
"name" : "localhost:28011",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : Timestamp(0, 0),
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2012-12-01T03:30:57Z"),
"pingMs" : 0,
"errmsg" : "DBClientBase::findN: transport error: localhost:28011 ns: admin.$cmd query: { replSetHeartbeat: \"mco\", v: 1, pv: 1, checkEmpty: false, from: \"localhost:28010\", $auth: { local: { __system: 2 } } }"
},
{
"_id" : 2,
"name" : "localhost:28012",
"health" : 1,
"state" : 6,
"stateStr" : "UNKNOWN",
"uptime" : 21,
"optime" : Timestamp(0, 0),
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2012-12-01T03:30:59Z"),
"pingMs" : 0,
"errmsg" : "still initializing"
}
],
"ok" : 1
}
mco:SECONDARY> rs.status()
{
"set" : "mco",
"date" : ISODate("2012-12-01T03:33:13Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "localhost:28010",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1166,
"optime" : Timestamp(1354332643000, 1),
"optimeDate" : ISODate("2012-12-01T03:30:43Z"),
"self" : true
},
{
"_id" : 1,
"name" : "localhost:28011",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 113,
"optime" : Timestamp(1354332643000, 1),
"optimeDate" : ISODate("2012-12-01T03:30:43Z"),
"lastHeartbeat" : ISODate("2012-12-01T03:33:13Z"),
"pingMs" : 0,
"errmsg" : "syncing to: localhost:28010"
},
{
"_id" : 2,
"name" : "localhost:28012",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 87,
"optime" : Timestamp(1354332643000, 1),
"optimeDate" : ISODate("2012-12-01T03:30:43Z"),
"lastHeartbeat" : ISODate("2012-12-01T03:33:13Z"),
"pingMs" : 0
}
],
"ok" : 1
}
mco:PRIMARY>
打印从库复制信息:
mco:PRIMARY> db.printSlaveReplicationInfo()
source: localhost:28011
syncedTo: Sat Dec 01 2012 11:30:43 GMT+0800 (CDT)
= 842 secs ago (0.23hrs)
source: localhost:28012
syncedTo: Sat Dec 01 2012 11:30:43 GMT+0800 (CDT)
= 842 secs ago (0.23hrs)
mco:PRIMARY>
打印主从状态:
mco:PRIMARY> rs.status()
{
"set" : "mco",
"date" : ISODate("2012-12-01T04:04:01Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "localhost:28010",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 395,
"optime" : Timestamp(1354334590000, 1),
"optimeDate" : ISODate("2012-12-01T04:03:10Z"),
"lastHeartbeat" : ISODate("2012-12-01T04:04:00Z"),
"pingMs" : 0
},
{
"_id" : 1,
"name" : "localhost:28011",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1958,
"optime" : Timestamp(1354334590000, 1),
"optimeDate" : ISODate("2012-12-01T04:03:10Z"),
"lastHeartbeat" : ISODate("2012-12-01T04:04:00Z"),
"pingMs" : 0
},
{
"_id" : 2,
"name" : "localhost:28012",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 2900,
"optime" : Timestamp(1354334590000, 1),
"optimeDate" : ISODate("2012-12-01T04:03:10Z"),
"self" : true
},
{
"_id" : 3,
"name" : "localhost:28013",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 23,
"optime" : Timestamp(1354334590000, 1),
"optimeDate" : ISODate("2012-12-01T04:03:10Z"),
"lastHeartbeat" : ISODate("2012-12-01T04:03:59Z"),
"pingMs" : 1058
}
],
"ok" : 1
}
本文完!