zoukankan      html  css  js  c++  java
  • MongoDB 副本集主从切换方法

    一、方法一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[2].priority = 3  //修改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. }
     
    整理自:
    https://www.cnblogs.com/ivictor/p/6804408.html
    http://blog.chinaunix.net/uid-26446098-id-5781440.html
  • 相关阅读:
    mysql全文索引
    oracle窗口函数的实战
    数据库的三大范式
    Oracle12cR1 Data Guard 实施文档
    Oracle 11gR2 RAC 删除节点
    Jboss部署文档
    Hexo搭建静态博客踩坑日记(一)
    一行代码引入博客园樱花飘落特效
    Hexo搭建静态博客踩坑日记(二)
    学习axios必知必会(2)~axios基本使用、使用axios前必知细节、axios和实例对象区别、拦截器、取消请求
  • 原文地址:https://www.cnblogs.com/xibuhaohao/p/11320209.html
Copyright © 2011-2022 走看看