本文先记录mongodb副本集的简单搭建,以后会在其基本上进行后续实验,比如测试自动分片,引入配置节点、路由节点、仲裁节点

  1. 各节点配置参考

点击(此处)折叠或打开

  1. [root@mas data]# cat /server/mongodb-2.6.1/conf/mongo27017.conf
  2. shardsvr=true
  3. replSet=chang
  4. port=27017
  5. #bind_ip=192.168.1.120
  6. dbpath=/server/mongodb-2.6.1/data/27017
  7. oplogSize=2048
  8. logpath=/server/mongodb-2.6.1/log/27017.log
  9. directoryperdb=true
  10. logappend=true
  11. fork=true
  12. journal=true
  13. nohttpinterface=true
2.在主节点上初始化复本集

点击(此处)折叠或打开

  1. [root@mongodb01 ~]# use27017
  2. MongoDB shell version: 2.6.1
  3. connecting to: test
  4. > use admin
  5. switched to db admin
  6. #先指定复本集配置变量,然后应用,除了指定_id,host等属性,可以配置复本集相关使用策略,例如优化级,选举权,延迟复制
  7. > 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}]};
  8. {
  9. "_id" : "chang",
  10. "members" : [
  11. {
  12. "_id" : 0,
  13. "host" : "192.168.1.120",
  14. "priority" : 3
  15. },
  16. {
  17. "_id" : 1,
  18. "host" : "192.168.1.11:27017",
  19. "priority" : 2
  20. },
  21. {
  22. "_id" : 2,
  23. "host" : "192.168.1.218:27017",
  24. "priority" : 1
  25. }
  26. ]
  27. }
  28. > rs.initiate(config)  //复本集初始化
  29. {
  30. "info" : "Config now saved locally. Should come online in about a minute.",
  31. "ok" : 1
  32. }
3、验证

点击(此处)折叠或打开

  1. chang:PRIMARY> rs.status()
  2. {
  3. "set" : "chang",
  4. "date" : ISODate("2016-03-06T14:39:56Z"),
  5. "myState" : 1,
  6. "members" : [
  7. {
  8. "_id" : 0,
  9. "name" : "192.168.1.120:27017",
  10. "health" : 1,
  11. "state" : 1,
  12. "stateStr" : "PRIMARY",//节点状态,可参考本博客相关文章
  13. "uptime" : 1591,
  14. "optime" : Timestamp(1457274006, 1),
  15. "optimeDate" : ISODate("2016-03-06T14:20:06Z"),
  16. "electionTime" : Timestamp(1457274017, 1),
  17. "electionDate" : ISODate("2016-03-06T14:20:17Z"),
  18. "self" : true
  19. },
  20. {
  21. "_id" : 1,
  22. "name" : "192.168.1.11:27017",
  23. "health" : 1,
  24. "state" : 2,
  25. "stateStr" : "SECONDARY",
  26. "uptime" : 1187,
  27. "optime" : Timestamp(1457274006, 1),
  28. "optimeDate" : ISODate("2016-03-06T14:20:06Z"),
  29. "lastHeartbeat" : ISODate("2016-03-06T14:39:54Z"),//心跳检测信息
  30. "lastHeartbeatRecv" : ISODate("2016-03-06T14:39:55Z"),
  31. "pingMs" : 0,
  32. "syncingTo" : "192.168.1.120:27017"
  33. },
  34. {
  35. "_id" : 2,
  36. "name" : "192.168.1.218:27017",
  37. "health" : 1,
  38. "state" : 2,
  39. "stateStr" : "SECONDARY",
  40. "uptime" : 1187,
  41. "optime" : Timestamp(1457274006, 1),
  42. "optimeDate" : ISODate("2016-03-06T14:20:06Z"),
  43. "lastHeartbeat" : ISODate("2016-03-06T14:39:54Z"),
  44. "lastHeartbeatRecv" : ISODate("2016-03-06T14:39:55Z"),
  45. "pingMs" : 0,
  46. "syncingTo" : "192.168.1.120:27017"
  47. }
  48. ],
  49. "ok" : 1
4、相关管理命令

点击(此处)折叠或打开

  1. chang:PRIMARY> rs.printReplicationInfo() //复制信息的查看
  2. configured oplog size: 2048MB
  3. log length start to end: 0secs (0hrs)
  4. oplog first event time: Sun Mar 06 2016 06:20:06 GMT-0800 (PST)
  5. oplog last event time: Sun Mar 06 2016 06:20:06 GMT-0800 (PST)
  6. now: Sun Mar 06 2016 06:46:25 GMT-0800 (PST)
  7. chang:PRIMARY> rs.printSlaveReplicationInfo() //查看各从节点复制情况,类似MySQL的show slave status命令
  8. source: 192.168.1.11:27017
  9. syncedTo: Sun Mar 06 2016 06:20:06 GMT-0800 (PST)
  10. 0 secs (0 hrs) behind the primary
  11. source: 192.168.1.218:27017
  12. syncedTo: Sun Mar 06 2016 06:20:06 GMT-0800 (PST)
  13. 0 secs (0 hrs) behind the primary
  14. 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

  15. 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 }
  16. //添加节点日志记录
  17. 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









10-10 03:19