zoukankan      html  css  js  c++  java
  • mongodb数据库从库同步主库维护js脚本

    脚本是日常维护使用js脚本。适用于集群数目很多,手工操作很麻烦费时,所以写了自动脚本,在每个集群自动运行即可。
    功能描述:检查本机的状态(主,从),确保从机始终从主机同步数据。
    适用范围:repli set 副本集模式
    运行脚本:mongo路径/mongo 10.0.0.1:30000 syncfrom.js
    syncfrom.js 如下
     1 /*
     2  * check rs.status, replSetSyncFrom to Primary
     3  * 
     4  * 20131216
     5  */
     6 function sleep(seconds) {
     7         this.date = Math.round(new Date().getTime()/1000);
     8         while(1) {
     9                 if(Math.round(new Date().getTime()/1000) - this.date >= seconds) break;
    10         }
    11         return true;
    12 }
    13 var status=rs.status();
    14 print(status["myState"]);
    15 var mystatus=status["myState"];
    16 var syncingTo=status["syncingTo"];
    17 print('syncto '+syncingTo);
    18 members=status["members"];
    19 if(mystatus==1)//primary
    20 {
    21         print('my status is primary... no need to replSetSyncFrom')
    22 }
    23 else if(mystatus==2)//secondary
    24 {
    25         print('my status is secondary...will start execute commmand "replSetSyncFrom" ')
    26         for(var i=0;i<members.length;i++)
    27         {
    28                 if(members[i]["stateStr"]=="PRIMARY")
    29                 {
    30                         priname=members[i]["name"]
    31                         print("primary: "+priname)
    32                         if(syncingTo==priname)
    33                         {
    34                                 print("the host of SyncFrom is already Primary, no need to change")
    35                         }
    36                         else
    37                         {
    38                                 print("start command...replSetSyncFrom")        
    39                                 db.adminCommand({replSetSyncFrom:priname}) 
    40                                 sleep(5);
    41                                 printjson(rs.status())
    42                         }
    43                 }
    44         }
    45 }
    46 }

  • 相关阅读:
    [HEOI2015]兔子与樱花
    [HNOI2015]亚瑟王
    [JSOI2011]分特产
    某考试 T3 sine
    [JSOI2015]最小表示
    51NOD 1258 序列求和 V4
    Codeforces 622F The Sum of the k-th Powers
    Loj #6261. 一个人的高三楼
    [HAOI????] 硬币购物
    bzoj4318 OSU!
  • 原文地址:https://www.cnblogs.com/mytech/p/3476394.html
Copyright © 2011-2022 走看看