Mongodb副本集--切换主从节点

思路:
1.为了保证数据的一致性,必须先关闭应用的写服务。
2.提升要升级为Primary节点的Secondary节点的优先级。

操作如下:

点击(此处)折叠或打开

  1. arps:PRIMARY> config=rs.conf()                //查看当前配置,存入config变量中。
  2. arps:PRIMARY> config.members[2].priority =//修改config变量,第三组成员的优先级为3.
  3. arps:PRIMARY> rs.reconfig(config)             //配置生效                       
  4. ......
  5. ......
  6. 2017-12-22T15:19:56.596+0800 I NETWORK trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed
  7. 2017-12-22T15:19:56.597+0800 I NETWORK reconnect 127.0.0.1:27017 (127.0.0.1) ok

  8. arps:SECONDARY> rs.conf()                    //查看当前配置
  9. {
  10.         "_id" : "arps",
  11.         "version" : 4,
  12.         "members" : [
  13.                 {
  14.                         "_id" : 0,
  15.                         "host" : "172.17.4.37:27017",
  16.                         "arbiterOnly" : false,
  17.                         "buildIndexes" : true,
  18.                         "hidden" : false,
  19.                         "priority" : 1,
  20.                         "tags" : {

  21.                         },
  22.                         "slaveDelay" : 0,
  23.                         "votes" : 1
  24.                 },
  25.                 {
  26.                         "_id" : 1,
  27.                         "host" : "172.17.4.38:27017",
  28.                         "arbiterOnly" : false,
  29.                         "buildIndexes" : true,
  30.                         "hidden" : false,
  31.                         "priority" : 1,
  32.                         "tags" : {

  33.                         },
  34.                         "slaveDelay" : 0,
  35.                         "votes" : 1
  36.                 },
  37.                 {
  38.                         "_id" : 2,
  39.                         "host" : "172.17.4.39:27017",
  40.                         "arbiterOnly" : false,
  41.                         "buildIndexes" : true,
  42.                         "hidden" : false,
  43.                         "priority" : 3,             //优先级变为3
  44.                         "tags" : {

  45.                         },
  46.                         "slaveDelay" : 0,
  47.                         "votes" : 1
  48.                 }
  49.         ],
  50.         "settings" : {
  51.                 "chainingAllowed" : true,
  52.                 "heartbeatTimeoutSecs" : 10,
  53.                 "getLastErrorModes" : {

  54.                 },
  55.                 "getLastErrorDefaults" : {
  56.                         "w" : 1,
  57.                         "wtimeout" : 0
  58.                 }
  59.         }
  60. }

  61. arps:SECONDARY> rs.status()
  62. {
  63.         "set" : "arps",
  64.         "date" : ISODate("2017-12-22T07:25:04.641Z"),
  65.         "myState" : 2,
  66.         "syncingTo" : "172.17.4.39:27017",
  67.         "members" : [
  68.                 {
  69.                         "_id" : 0,
  70.                         "name" : "172.17.4.37:27017",
  71.                         "health" : 1,
  72.                         "state" : 2,
  73.                         "stateStr" : "SECONDARY",
  74.                         "uptime" : 7597536,
  75.                         "optime" : Timestamp(1513927481, 3),
  76.                         "optimeDate" : ISODate("2017-12-22T07:24:41Z"),
  77.                         "syncingTo" : "172.17.4.39:27017",
  78.                         "configVersion" : 4,
  79.                         "self" : true
  80.                 },
  81.                 {
  82.                         "_id" : 1,
  83.                         "name" : "172.17.4.38:27017",
  84.                         "health" : 1,
  85.                         "state" : 2,
  86.                         "stateStr" : "SECONDARY",
  87.                         "uptime" : 7597426,
  88.                         "optime" : Timestamp(1513927481, 3),
  89.                         "optimeDate" : ISODate("2017-12-22T07:24:41Z"),
  90.                         "lastHeartbeat" : ISODate("2017-12-22T07:25:02.961Z"),
  91.                         "lastHeartbeatRecv" : ISODate("2017-12-22T07:25:04.091Z"),
  92.                         "pingMs" : 0,
  93.                         "syncingTo" : "172.17.4.39:27017",
  94.                         "configVersion" : 4
  95.                 },
  96.                 {
  97.                         "_id" : 2,
  98.                         "name" : "172.17.4.39:27017",
  99.                         "health" : 1,
  100.                         "state" : 1,
  101.                         "stateStr" : "PRIMARY",    //最后一个节点升级为PRIMARY
  102.                         "uptime" : 3202,
  103.                         "optime" : Timestamp(1513927481, 3),
  104.                         "optimeDate" : ISODate("2017-12-22T07:24:41Z"),
  105.                         "lastHeartbeat" : ISODate("2017-12-22T07:25:02.951Z"),
  106.                         "lastHeartbeatRecv" : ISODate("2017-12-22T07:25:04.344Z"),
  107.                         "pingMs" : 0,
  108.                         "electionTime" : Timestamp(1513927190, 2),
  109.                         "electionDate" : ISODate("2017-12-22T07:19:50Z"),
  110.                         "configVersion" : 4
  111.                 }
  112.         ],
  113.         "ok" : 1
  114. }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值