mongodb副本集主从手动切换

  |   0 评论   |   80 浏览

一、方法一rs.setpDown()

将Primary节点降级为Secondary节点

myapp:PRIMARY> rs.stepDown()

这个命令会让primary降级为Secondary节点,并维持60s,如果这段时间内没有新的primary被选举出来,这个节点可以要求重新进行选举。

也可手动指定时间

myapp:PRIMARY> rs.stepDown(30)

在执行完该命令后,原Secondary node3:27017升级为Primary。

二、方法二优先级设置

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

操作如下:

点击(此处)折叠或打开

  1. arps:PRIMARY> config=rs.conf() //查看当前配置,存入config变量中。
  2. arps:PRIMARY> config.members[0].priority = 3 //修改config变量,第三组成员的优先级为3.优先级1-100,数字越大,优先级越高
  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. }

标题:mongodb副本集主从手动切换
作者:SmiteLi
地址:https://smite.site/articles/2019/11/28/1574946340445.html

评论

发表评论