Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1183542
  • 博文数量: 177
  • 博客积分: 1857
  • 博客等级: 上尉
  • 技术积分: 2751
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-14 22:55
  • 认证徽章:
个人简介

吾生有涯,而知无涯,适当止学.循序渐进,步步提升 Talk is cheap, show me the code.

文章分类

全部博文(177)

文章存档

2018年(12)

2017年(24)

2016年(31)

2015年(15)

2014年(5)

2013年(10)

2012年(80)

分类: NOSQL

2017-12-22 15:09:54

这篇文档主要介绍MongoDB副本集的日常查看和管理的命令.

1)命令行的查看命令:db.serverCmdLineOpts()

点击(此处)折叠或打开

  1. arps:SECONDARY> db.serverCmdLineOpts()
  2. {
  3.         "argv" : [
  4.                 "/opt/software/mongodb-linux-x86_64-3.0.1/bin/mongod",//启动参数mongod
  5.                 "-f",
  6.                 "/opt/software/mongodb-linux-x86_64-3.0.1/bin/mongodb.conf"  
  7.         ],//参数配置文件
  8.         "parsed" : {
  9.                 "config" : "/opt/software/mongodb-linux-x86_64-3.0.1/bin/mongodb.conf",
  10.                 "net" : {
  11.                         "port" : 27017   //端口
  12.                 },
  13.                 "processManagement" : {
  14.                         "fork" : true,
  15.                         "pidFilePath" : "/opt/data/mongodb/mongodb.pid"  //进程文件
  16.                 },
  17.                 "replication" : {
  18.                         "oplogSizeMB" : 10000,       //oplog日志大小
  19.                         "replSet" : "arps"           //副本集的名称arps
  20.                 },
  21.                 "storage" : {
  22.                         "dbPath" : "/opt/data/mongodb",   //数据存储的位置
  23.                         "directoryPerDB" : true,
  24.                         "mmapv1" : {
  25.                                 "preallocDataFiles" : false
  26.                         }
  27.                 },
  28.                 "systemLog" : {
  29.                         "destination" : "file",
  30.                         "logAppend" : true,
  31.                         "path" : "/opt/data/mongodb/log/mongodb.log"  //系统日志的位置
  32.                 }
  33.         },
  34.         "ok" : 1
  35. }
2)查看副本集状态的命令rs.status()

点击(此处)折叠或打开

  1. arps:SECONDARY> rs.status()
  2. {
  3.         "set" : "arps",                                //副本集名称
  4.         "date" : ISODate("2017-12-22T06:12:21.904Z"),  //执行时间
  5.         "myState" : 2,                                
  6.         "syncingTo" : "172.17.4.37:27017",             //同步源:172.17.4.37:27017
  7.         "members" : [                                  //成员
  8.                 {
  9.                         "_id" : 0,                    
  10.                         "name" : "172.17.4.37:27017",
  11.                         "health" : 1,                 //健康状态值,1为UP,0为down
  12.                         "state" : 2,                  //成员状态值,2为secondary
  13.                         "stateStr" : "SECONDARY",
  14.                         "uptime" : 12354,
  15.                         "optime" : Timestamp(1513923135, 3),  //最近一次更改数据库的时间。
  16.                         "optimeDate" : ISODate("2017-12-22T06:12:15Z"),
  17.                                                   //最后一个操作发生的时间
  18.                         "lastHeartbeat" : ISODate("2017-12-22T06:12:20.999Z"),
  19.                                                   //最后一次心跳接收的时间值
  20.                         "lastHeartbeatRecv" : ISODate("2017-12-22T06:12:20.999Z"),
  21.                                                   //最近心跳的发送到达的时间值
  22.                         "pingMs" : 0,
  23.                         "syncingTo" : "172.17.4.38:27017",
  24.                         "configVersion" : 1
  25.                 },
  26.                 {
  27.                         "_id" : 1,
  28.                         "name" : "172.17.4.38:27017",
  29.                         "health" : 1,
  30.                         "state" : 1,                  //成员状态值,1位primary
  31.                         "stateStr" : "PRIMARY",
  32.                         "uptime" : 12354,
  33.                         "optime" : Timestamp(1513923135, 3),
  34.                         "optimeDate" : ISODate("2017-12-22T06:12:15Z"),
  35.                         "lastHeartbeat" : ISODate("2017-12-22T06:12:21.805Z"),
  36.                         "lastHeartbeatRecv" : ISODate("2017-12-22T06:12:20.956Z"),
  37.                         "pingMs" : 1,
  38.                         "electionTime" : Timestamp(1506330005, 1),
  39.                         "electionDate" : ISODate("2017-09-25T09:00:05Z"),
  40.                         "configVersion" : 1
  41.                 },
  42.                 {
  43.                         "_id" : 2,
  44.                         "name" : "172.17.4.39:27017",
  45.                         "health" : 1,
  46.                         "state" : 2,
  47.                         "stateStr" : "SECONDARY",
  48.                         "uptime" : 12356,
  49.                         "optime" : Timestamp(1513923135, 3),
  50.                         "optimeDate" : ISODate("2017-12-22T06:12:15Z"),
  51.                         "syncingTo" : "172.17.4.37:27017",
  52.                         "configVersion" : 1,
  53.                         "self" : true
  54.                 }
  55.         ],
  56.         "ok" : 1
  57. }
mongod实例每隔两秒就会向其他成员发送一个心跳包,并且通过rs.status()中返回的成员的health来判断成员的状态。如果primary节点不可用了,那么复制集中的所有secondary节点都会触发一次选举操作。选出新的primary节点。如果secondary节点有多个,则会选举拥有最新oplog时间戳记录的或者有较高权限的节点成为primary

3)添加、删除副本集成员rs.add,rs.addArb(),rs.remove(),rs.reconfig()

点击(此处)折叠或打开

  1. 1)添加成员
  2. rs.add('172.17.4.39:27017')  //在添加成员之前,需要在目标成员里加上repset参数。

  3. 2)添加选举节点,不会同步数据
  1. rs.add({,"host":"192.168.200.252:27017","arbiterOnly":true}) rs.addArb()

  2. 3)修改配置
  1. 通过rs.reconfig()来实现。

  2. 4)删除成员
  3. rs.remove('172.17.4.39:27017')
4)副本集的配置rs.config()

点击(此处)折叠或打开

  1. arps:PRIMARY> rs.config()
  2. {
  3.         "_id" : "arps",
  4.         "version" : 3,
  5.         "members" : [
  6.                 {
  7.                         "_id" : 0,
  8.                         "host" : "172.17.4.37:27017",//主机名
  9.                         "arbiterOnly" : false,       //是否为选举节点
  10.                         "buildIndexes" : true,       //是否建立索引
  11.                         "hidden" : false,            //是否隐藏
  12.                         "priority" : 1,              //优先级
  13.                         "tags" : {

  14.                         },
  15.                         "slaveDelay" : 0,
  16.                         "votes" : 1                 
  17.                 },
  18.                 {
  19.                         "_id" : 1,
  20.                         "host" : "172.17.4.38:27017",
  21.                         "arbiterOnly" : false,
  22.                         "buildIndexes" : true,
  23.                         "hidden" : false,
  24.                         "priority" : 1,
  25.                         "tags" : {

  26.                         },
  27.                         "slaveDelay" : 0,
  28.                         "votes" : 1
  29.                 },
  30.                 {
  31.                         "_id" : 2,
  32.                         "host" : "172.17.4.39:27017",
  33.                         "arbiterOnly" : false,
  34.                         "buildIndexes" : true,
  35.                         "hidden" : false,
  36.                         "priority" : 1,
  37.                         "tags" : {

  38.                         },
  39.                         "slaveDelay" : 0,
  40.                         "votes" : 1
  41.                 }
  42.         ],
  43.         "settings" : {
  44.                 "chainingAllowed" : true,                  //自动寻找数据源功能
  45.                 "heartbeatTimeoutSecs" : 10,               //心跳连接超时时间
  46.                 "getLastErrorModes" : {                    

  47.                 },
  48.                 "getLastErrorDefaults" : {                //write concern写入安全机制
  49.                         "w" : 1,                          //1为(应答时写入)
  50.                               //确认写入操作已经发送到指定的单个mongodb实例或者副本集的Primary
  51.                         "wtimeout" : 0                   
  52.                 }
  53.         }
  54. }

5.Secondary进入维护模式replSetMaintenance

如何让一个成员远落后于Primary的,进入维护模式

点击(此处)折叠或打开

  1. arps:RECOVERING> db.adminCommand({"replSetMaintenance":true})   //进入维护模式

  2. arps:RECOVERING> rs.status().members
  3. [
  4.         {
  5.                 "_id" : 0,
  6.                 "name" : "172.17.4.37:27017",
  7.                 "health" : 1,
  8.                 "state" : 2,
  9.                 "stateStr" : "SECONDARY",
  10.                 "uptime" : 15241,
  11.                 "optime" : Timestamp(1513926020, 4),
  12.                 "optimeDate" : ISODate("2017-12-22T07:00:20Z"),
  13.                 "lastHeartbeat" : ISODate("2017-12-22T07:00:28.772Z"),
  14.                 "lastHeartbeatRecv" : ISODate("2017-12-22T07:00:28.771Z"),
  15.                 "pingMs" : 0,
  16.                 "syncingTo" : "172.17.4.38:27017",
  17.                 "configVersion" : 3
  18.         },
  19.         {
  20.                 "_id" : 1,
  21.                 "name" : "172.17.4.38:27017",
  22.                 "health" : 1,
  23.                 "state" : 1,
  24.                 "stateStr" : "PRIMARY",
  25.                 "uptime" : 15241,
  26.                 "optime" : Timestamp(1513926020, 4),
  27.                 "optimeDate" : ISODate("2017-12-22T07:00:20Z"),
  28.                 "lastHeartbeat" : ISODate("2017-12-22T07:00:28.772Z"),
  29.                 "lastHeartbeatRecv" : ISODate("2017-12-22T07:00:26.849Z"),
  30.                 "pingMs" : 0,
  31.                 "electionTime" : Timestamp(1506330005, 1),
  32.                 "electionDate" : ISODate("2017-09-25T09:00:05Z"),
  33.                 "configVersion" : 3
  34.         },
  35.         {
  36.                 "_id" : 2,
  37.                 "name" : "172.17.4.39:27017",
  38.                 "health" : 1,
  39.                 "state" : 3,
  40.                 "stateStr" : "RECOVERING",   //进入维护模式,会强制进入RECOVERING状态,不提供服务
  41.                 "uptime" : 15243,
  42.                 "optime" : Timestamp(1513926020, 4),
  43.                 "optimeDate" : ISODate("2017-12-22T07:00:20Z"),
  44.                 "syncingTo" : "172.17.4.38:27017",
  45.                 "maintenanceMode" : 1,
  46.                 "configVersion" : 3,
  47.                 "self" : true
  48.         }
  49. ]

  50. arps:RECOVERING> db.adminCommand({"replSetMaintenance":false})  //退出维护模式
6.主节点变成备份节点:rs.stepDown(time),rs.freeze(time)

rs.stepDown(time)  可以让主节点退成备份节点,timie单位是秒,默认60s。60s内主被副本集的其他成员获得,时间到后,会重新进行选举,一般都会重新成为主(优先级)。

rs.freeze(time):阻止选举,始终出于备份节点状态。比如主节点需要做一些维护,不希望其他成员选举为主节点,可以在每个备份节点上执行。强制他们出于备份节点状态。


点击(此处)折叠或打开

  1. arps:PRIMARY> rs.stepDown(30) //退出主节点,成为secondary节点
  2. .......
  3. .......
  4. 2017-12-22T15:05:52.110+0800 I NETWORK trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed
  5. 2017-12-22T15:05:52.111+0800 I NETWORK reconnect 127.0.0.1:27017 (127.0.0.1) ok
  6. arps:SECONDARY>

  7. arps:SECONDARY> rs.status().members
  8. [
  9.         {
  10.                 "_id" : 0,
  11.                 "name" : "172.17.4.37:27017",
  12.                 "health" : 1,
  13.                 "state" : 1,
  14.                 "stateStr" : "PRIMARY",  //升级为主节点
  15.                 "uptime" : 15797,
  16.                 "optime" : Timestamp(1513926601, 3),
  17.                 "optimeDate" : ISODate("2017-12-22T07:10:01Z"),
  18.                 "lastHeartbeat" : ISODate("2017-12-22T07:09:43.092Z"),
  19.                 "lastHeartbeatRecv" : ISODate("2017-12-22T07:09:43.130Z"),
  20.                 "pingMs" : 0,
  21.                 "electionTime" : Timestamp(1513926357, 1),
  22.                 "electionDate" : ISODate("2017-12-22T07:05:57Z"),
  23.                 "configVersion" : 3
  24.         },
  25.         {
  26.                 "_id" : 1,
  27.                 "name" : "172.17.4.38:27017",
  28.                 "health" : 1,
  29.                 "state" : 2,
  30.                 "stateStr" : "SECONDARY",  //退位至备份节点
  31.                 "uptime" : 15797,
  32.                 "optime" : Timestamp(1513926601, 3),
  33.                 "optimeDate" : ISODate("2017-12-22T07:10:01Z"),
  34.                 "lastHeartbeat" : ISODate("2017-12-22T07:09:43.092Z"),
  35.                 "lastHeartbeatRecv" : ISODate("2017-12-22T07:09:43.245Z"),
  36.                 "pingMs" : 0,
  37.                 "syncingTo" : "172.17.4.37:27017",
  38.                 "configVersion" : 3
  39.         },
  40.         {
  41.                 "_id" : 2,
  42.                 "name" : "172.17.4.39:27017",
  43.                 "health" : 1,
  44.                 "state" : 2,
  45.                 "stateStr" : "SECONDARY",
  46.                 "uptime" : 15799,
  47.                 "optime" : Timestamp(1513926601, 3),
  48.                 "optimeDate" : ISODate("2017-12-22T07:10:01Z"),
  49.                 "syncingTo" : "172.17.4.37:27017",
  50.                 "configVersion" : 3,
  51.                 "self" : true
  52.         }
  53. ]
7.复制延迟状态查看db.printReplicationInfo()db.printSlaveReplicationInfo()

点击(此处)折叠或打开

  1. arps:PRIMARY> db.printReplicationInfo()    //主上面执行的复制状态信息
  2. configured oplog size: 10000.003845214844MB
  3. log length start to end: 31637996secs (8788.33hrs)
  4. oplog first event time: Wed Dec 21 2016 10:54:05 GMT+0800 (CST)
  5. oplog last event time: Fri Dec 22 2017 15:14:01 GMT+0800 (CST)
  6. now: Fri Dec 22 2017 15:14:01 GMT+0800 (CST)

  7. arps:SECONDARY> db.printSlaveReplicationInfo()  //从上面执行的复制延迟信息
  8. source: 172.17.4.38:27017
  9.         syncedTo: Fri Dec 22 2017 15:14:01 GMT+0800 (CST)
  10.         0 secs (0 hrs) behind the primary
  11. source: 172.17.4.39:27017
  12.         syncedTo: Fri Dec 22 2017 15:14:01 GMT+0800 (CST)
  13.         0 secs (0 hrs) behind the primary






阅读(331) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
请登录后评论。

登录 注册