全部博文(389)
分类: NOSQL
2015-05-06 21:51:25
Mongo DB复制配置
1, 在主库上,启replSet模式启动一个mongod实例.
[root@localhost bin]# ./mongod --replSet "rs0" -dbpath /data
2,在主库上通过rs.initiate方法初始化replicat set
[root@localhost bin]# ./mongo
MongoDB shell version: 2.4.9
connecting to: test
> rs.initiate()
{
"info2" : "no configuration explicitly specified -- making one",
"me" : "localhost.localdomain:27017",
"info" : "Config now saved locally. Should come online in about a minute.",
"ok" : 1
}
查看当前复制状态
rs0:PRIMARY> rs.status()
{
"set" : "rs0",
"date" : ISODate("2015-05-06T13:37:43Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "localhost.localdomain:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 225,
"optime" : Timestamp(1430919283, 1),
"optimeDate" : ISODate("2015-05-06T13:34:43Z"),
"self" : true
}
],
"ok" : 1
}
当前只有一个成员,且状态为primary
3,在从库上以相同方式启动mongod实例
[root@localhost bin] mongod --port 27018 --replSet "rs0" --dbpath=/b
4,在主库上通过rs.add方法增加备库的成员
rs0:PRIMARY> rs.add("172.28.10.145:27018")
{ "ok" : 1 }
5, 查看复制的状态
rs0:PRIMARY> rs.status()rs.status()
{
"set" : "rs0",
"date" : ISODate("2015-05-06T13:48:00Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "localhost.localdomain:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 842,
"optime" : Timestamp(1430919658, 1),
"optimeDate" : ISODate("2015-05-06T13:40:58Z"),
"self" : true
},
{
"_id" : 1,
"name" : "172.28.10.145:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 422,
"optime" : Timestamp(1430919658, 1),
"optimeDate" : ISODate("2015-05-06T13:40:58Z"),
"lastHeartbeat" : ISODate("2015-05-06T13:47:58Z"),
"lastHeartbeatRecv" : ISODate("2015-05-06T13:47:58Z"),
"pingMs" : 0,
"syncingTo" : "localhost.localdomain:27017"
}
],
"ok" : 1
}
当前一个primary和一个secondary成员.