本文先记录mongodb副本集的简单搭建,以后会在其基本上进行后续实验,比如测试自动分片,引入配置节点、路由节点、仲裁节点
-
各节点配置参考
-
[root@mas data]# cat /server/mongodb-2.6.1/conf/mongo27017.conf
-
shardsvr=true
-
replSet=chang
-
port=27017
-
#bind_ip=192.168.1.120
-
dbpath=/server/mongodb-2.6.1/data/27017
-
oplogSize=2048
-
logpath=/server/mongodb-2.6.1/log/27017.log
-
directoryperdb=true
-
logappend=true
-
fork=true
-
journal=true
-
nohttpinterface=true
2.在主节点上初始化复本集
-
[root@mongodb01 ~]# use27017
-
MongoDB shell version: 2.6.1
-
connecting to: test
-
> use admin
-
switched to db admin
-
#先指定复本集配置变量,然后应用,除了指定_id,host等属性,可以配置复本集相关使用策略,例如优化级,选举权,延迟复制
-
> config={_id:'chang',members:[{_id:0,host:'192.168.1.120',priority:3},{_id:1,host:'192.168.1.11:27017',priority:2},{_id:2,host:'192.168.1.218:27017',priority:1}]};
-
{
-
"_id" : "chang",
-
"members" : [
-
{
-
"_id" : 0,
-
"host" : "192.168.1.120",
-
"priority" : 3
-
},
-
{
-
"_id" : 1,
-
"host" : "192.168.1.11:27017",
-
"priority" : 2
-
},
-
{
-
"_id" : 2,
-
"host" : "192.168.1.218:27017",
-
"priority" : 1
-
}
-
]
-
}
-
> rs.initiate(config) //复本集初始化
-
{
-
"info" : "Config now saved locally. Should come online in about a minute.",
-
"ok" : 1
-
}
3、验证
-
chang:PRIMARY> rs.status()
-
{
-
"set" : "chang",
-
"date" : ISODate("2016-03-06T14:39:56Z"),
-
"myState" : 1,
-
"members" : [
-
{
-
"_id" : 0,
-
"name" : "192.168.1.120:27017",
-
"health" : 1,
-
"state" : 1,
-
"stateStr" : "PRIMARY",//节点状态,可参考本博客相关文章
-
"uptime" : 1591,
-
"optime" : Timestamp(1457274006, 1),
-
"optimeDate" : ISODate("2016-03-06T14:20:06Z"),
-
"electionTime" : Timestamp(1457274017, 1),
-
"electionDate" : ISODate("2016-03-06T14:20:17Z"),
-
"self" : true
-
},
-
{
-
"_id" : 1,
-
"name" : "192.168.1.11:27017",
-
"health" : 1,
-
"state" : 2,
-
"stateStr" : "SECONDARY",
-
"uptime" : 1187,
-
"optime" : Timestamp(1457274006, 1),
-
"optimeDate" : ISODate("2016-03-06T14:20:06Z"),
-
"lastHeartbeat" : ISODate("2016-03-06T14:39:54Z"),//心跳检测信息
-
"lastHeartbeatRecv" : ISODate("2016-03-06T14:39:55Z"),
-
"pingMs" : 0,
-
"syncingTo" : "192.168.1.120:27017"
-
},
-
{
-
"_id" : 2,
-
"name" : "192.168.1.218:27017",
-
"health" : 1,
-
"state" : 2,
-
"stateStr" : "SECONDARY",
-
"uptime" : 1187,
-
"optime" : Timestamp(1457274006, 1),
-
"optimeDate" : ISODate("2016-03-06T14:20:06Z"),
-
"lastHeartbeat" : ISODate("2016-03-06T14:39:54Z"),
-
"lastHeartbeatRecv" : ISODate("2016-03-06T14:39:55Z"),
-
"pingMs" : 0,
-
"syncingTo" : "192.168.1.120:27017"
-
}
-
],
-
"ok" : 1
4、相关管理命令
-
chang:PRIMARY> rs.printReplicationInfo() //复制信息的查看
-
configured oplog size: 2048MB
-
log length start to end: 0secs (0hrs)
-
oplog first event time: Sun Mar 06 2016 06:20:06 GMT-0800 (PST)
-
oplog last event time: Sun Mar 06 2016 06:20:06 GMT-0800 (PST)
-
now: Sun Mar 06 2016 06:46:25 GMT-0800 (PST)
-
chang:PRIMARY> rs.printSlaveReplicationInfo() //查看各从节点复制情况,类似MySQL的show slave status命令
-
source: 192.168.1.11:27017
-
syncedTo: Sun Mar 06 2016 06:20:06 GMT-0800 (PST)
-
0 secs (0 hrs) behind the primary
-
source: 192.168.1.218:27017
-
syncedTo: Sun Mar 06 2016 06:20:06 GMT-0800 (PST)
-
0 secs (0 hrs) behind the primary
-
chang:PRIMARY> rs.stepDown() //在当点主节点上执行,使主节点降级成为从节点,重新选举new primary,影响选择主节的因素很多,可以参考本博客其他文章
2016-03-06T06:51:34.730-0800 DBClientCursor::init call() failed
2016-03-06T06:51:34.734-0800 Error: error doing query: failed at src/mongo/shell/query.js:81
2016-03-06T06:51:34.738-0800 trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed
2016-03-06T06:51:34.740-0800 reconnect 127.0.0.1:27017 (127.0.0.1) ok
-
-
chang:PRIMARY> rs.remove('192.168.1.218:27017') //移除某个从节点
2016-03-06T07:02:19.878-0800 DBClientCursor::init call() failed
2016-03-06T07:02:19.879-0800 Error: error doing query: failed at src/mongo/shell/query.js:81
2016-03-06T07:02:19.885-0800 trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed
2016-03-06T07:02:19.890-0800 reconnect 127.0.0.1:27017 (127.0.0.1) ok
chang:PRIMARY> rs.status()
{
"set" : "chang",
"date" : ISODate("2016-03-06T15:02:24Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "192.168.1.120:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 2939,
"optime" : Timestamp(1457276539, 1),
"optimeDate" : ISODate("2016-03-06T15:02:19Z"),
"electionTime" : Timestamp(1457275971, 1),
"electionDate" : ISODate("2016-03-06T14:52:51Z"),
"self" : true
},
{
"_id" : 1,
"name" : "192.168.1.11:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 5,
"optime" : Timestamp(1457276539, 1),
"optimeDate" : ISODate("2016-03-06T15:02:19Z"),
"lastHeartbeat" : ISODate("2016-03-06T15:02:23Z"),
"lastHeartbeatRecv" : ISODate("2016-03-06T15:02:23Z"),
"pingMs" : 8,
"lastHeartbeatMessage" : "syncing to: 192.168.1.120:27017",
"syncingTo" : "192.168.1.120:27017"
}
],
"ok" : 1
}
chang:PRIMARY> rs.add({id_:2,host:'192.168.1.218:27017',priority:1,hidden:true}) //添加节点,顺例提一下hidden属性,表明该节点为隐藏节点,与之对应优先级的问题,都隐藏了当然就不能有被选择举咯,此外可以查看添加节点相应的日志,查看同步过程
{
"errmsg" : "exception: bad config for member[2] unexpected field 'id_' in object",
"code" : 13135,
"ok" : 0
}
chang:PRIMARY> rs.add({_id:2,host:'192.168.1.218:27017',priority:1,hidden:true})
{
"errmsg" : "exception: bad config for member[2] priority must be 0 when hidden=true",
"code" : 13135,
"ok" : 0
}
chang:PRIMARY> rs.add({_id:2,host:'192.168.1.218:27017',priority:0,hidden:true})
{ "down" : [ "192.168.1.218:27017" ], "ok" : 1 }
-
//添加节点日志记录
-
2016-03-06T07:08:29.236+0800 [initandlisten] connection accepted from 192.168.1.120:30494 #14 (3 connections now open)
2016-03-06T07:08:29.553+0800 [SyncSourceFeedbackThread] replset setting syncSourceFeedback to 192.168.1.120:27017
2016-03-06T07:08:30.465+0800 [rsMgr] trying to contact 192.168.1.11:27017
2016-03-06T07:08:30.468+0800 [rsMgr] trying to contact 192.168.1.120:27017
2016-03-06T07:08:30.468+0800 [rsMgr] replSet I am 192.168.1.218:27017
2016-03-06T07:08:30.468+0800 [rsMgr] replSet got config version 3 from a remote, saving locally
2016-03-06T07:08:30.468+0800 [rsMgr] replSet info saving a newer config version to local.system.replset: { _id: "chang", version: 3, members: [ { _id: 0, host: "192.168.1.120:27017", priority: 3.0 }, { _id: 1, host: "192.168.1.11:27017", priority: 2.0 }, { _id: 2, host: "192.168.1.218:27017", priority: 0.0, hidden: true } ] }
2016-03-06T07:08:30.468+0800 [rsMgr] replSet saveConfigLocally done
2016-03-06T07:08:30.468+0800 [rsBackgroundSync] replSet syncing to: 192.168.1.120:27017
2016-03-06T07:08:30.468+0800 [rsSync] replSet SECONDARY
2016-03-06T07:08:30.469+0800 [conn11] command admin.$cmd command: replSetSyncFrom { replSetSyncFrom: undefined } keyUpdates:0 numYields:0 reslen:110 308680ms
2016-03-06T07:08:30.469+0800 [conn11] end connection 127.0.0.1:26786 (2 connections now open)
2016-03-06T07:08:30.471+0800 [rsHealthPoll] replSet member 192.168.1.11:27017 is up
2016-03-06T07:08:30.471+0800 [rsHealthPoll] replSet member 192.168.1.11:27017 is now in state SECONDARY
2016-03-06T07:08:30.472+0800 [rsBackgroundSync] replset setting syncSourceFeedback to 192.168.1.120:27017
2016-03-06T07:08:30.473+0800 [rsHealthPoll] replSet member 192.168.1.120:27017 is up
阅读(2892) | 评论(1) | 转发(0) |