MySQL DBA
分类: NOSQL
2013-03-07 14:03:43
mongodb replica set config
参考:
1. rs.conf() replica set 相关的配置类,具有的属性:
a) _id, replica set 名称。
b) members。各节点相关属性(id,host,priority,tag,slaveDelay…)。
c) settings。配置信息。
2. rs 操作类:
rs.status() 返回当前replica set 的状态信息。每个member的host,health,role,optime等信息
rs.initate() 初始化配置一个复制集,加入参数则按照参数设定配置。
rs.conf() 返回当前复制集的配置信息。
rs.reconfig() 在线更新复制集配置信息,如对member 的host等属性的更改。
rs.add() 添加节点。参数可以是host:port,也可是完整的结构体。
rs.stepDown() primary 主动降级为slave。
rs.freeze() 设置节点一定时间内不能被选为primary,在切换的时候人工干预。
rs.remove() 移除节点。
db.isMaster() 返回整个复制集hosts,以及角色的分配情况。在rs.status()也能看到。
搭建步骤:
1. 启动实例
mongod --port 27018 --dbpath /data/mongodb/rs-1 --replSet rs0 &
mongod --port 27019 --dbpath /data/mongodb/rs-2 --replSet rs0 &
mongod --port 27020 --dbpath /data/mongodb/rs-3 --replSet rs0 &
2. 配置节点
ubuntu@os-cloud-12:~$ mongo localhost:27018
MongoDB shell version: 2.0.4
connecting to: localhost:27018/test
> rs.status()
{
"startupStatus" : 3,
"info" : "run rs.initiate(...) if not yet done for the set",
"errmsg" : "can't get local.system.replset config from self or any seed (EMPTYCONFIG)",
"ok" : 0
}
#使用默认配置初始化。
> rs.initiate()
{
"info2" : "no configuration explicitly specified -- making one",
"me" : "os-cloud-12:27018",
"info" : "Config now saved locally. Should come online in about a minute.",
"ok" : 1
}
> rs.conf()
{
"_id" : "rs0",
"version" : 1,
"members" : [
{
"_id" : 0,
"host" : "os-cloud-12:27018"
}
]
}
#初始化后,默认当前host为primary,继续添加节点
PRIMARY> rs.add("os-cloud-12:27019")
{ "ok" : 1 }
PRIMARY> rs.add("os-cloud-12:27020")
{ "ok" : 1 }
#查看配置情况和当前状态
PRIMARY> rs.conf()
{
"_id" : "rs0",
"version" : 3,
"members" : [
{
"_id" : 0,
"host" : "os-cloud-12:27018"
},
{
"_id" : 1,
"host" : "os-cloud-12:27019"
},
{
"_id" : 2,
"host" : "os-cloud-12:27020"
}
]
}
PRIMARY> rs.status()
{
"set" : "rs0",
"date" : ISODate("2012-11-24T03:01:54Z"),
"myState" : 1,
"syncingTo" : "os-cloud-12:27018",
"members" : [
{
"_id" : 0,
"name" : "os-cloud-12:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 60854,
"optime" : {
"t" : 1353665255000,
"i" : 1
},
"optimeDate" : ISODate("2012-11-23T10:07:35Z"),
"lastHeartbeat" : ISODate("2012-11-24T03:01:53Z"),
"pingMs" : 0
},
{
"_id" : 1,
"name" : "os-cloud-12:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 60854,
"optime" : {
"t" : 1353665255000,
"i" : 1
},
"optimeDate" : ISODate("2012-11-23T10:07:35Z"),
"lastHeartbeat" : ISODate("2012-11-24T03:01:53Z"),
"pingMs" : 0
},
{
"_id" : 2,
"name" : "os-cloud-12:27020",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"optime" : {
"t" : 1353665255000,
"i" : 1
},
"optimeDate" : ISODate("2012-11-23T10:07:35Z"),
"self" : true
}
],
"ok" : 1
}
3. primary 切换。
PRIMARY> rs.stepDown()
Fri Nov 23 18:12:31 DBClientCursor::init call() failed
Fri Nov 23 18:12:31 query failed : admin.$cmd { replSetStepDown: 60.0 } to: localhost:27018
Fri Nov 23 18:12:31 Error: error doing query: failed shell/collection.js:151
Fri Nov 23 18:12:31 trying reconnect to localhost:27018
Fri Nov 23 18:12:31 reconnect localhost:27018 ok
SECONDARY> rs.status()
4. member 属性的修改。
a) 重启需要修改的mongodb 节点,以新的配置加入复制集
b) 在primary中修改其属性值(rs.reconfig())
选举策略:
1 get maxLocalOpOrdinal from each server.
2 if a majority of servers are not up (from this server's POV), remain in Secondary mode and stop.
3 if the last op time seems very old, stop and await human intervention.
4 else, using a consensus protocol, pick the server with the highest maxLocalOpOrdinal as the Primary.
http://blog.nosqlfan.com/html/2523.html