【MongoDB學習筆記32】MongoDB修改複本集配置

來源:互聯網
上載者:User

標籤:mongodb   修改複本集配置   

一、刪除複本集成員

spock:PRIMARY> rs.config()   {            "_id" : "spock",            "version" : 1,            "members" : [                    {                            "_id" : 0,                            "host" : "192.168.1.112:27017"                    },                    {                            "_id" : 1,                            "host" : "192.168.1.113:27017"                    }            ]    }    spock:PRIMARY> rs.remove("192.168.1.113:27017")    {            "errmsg" : "exception: can‘t find self in new replset config",            "code" : 13433,            "ok" : 0    }    spock:PRIMARY> rs.remove("192.168.1.112:27017")    2015-02-02T21:59:13.303+0800 DBClientCursor::init call() failed    2015-02-02T21:59:13.304+0800 Error: error doing query: failed at src/mongo/shell/query.js:81    2015-02-02T21:59:13.305+0800 trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed    2015-02-02T21:59:13.308+0800 reconnect 127.0.0.1:27017 (127.0.0.1) ok    spock:PRIMARY> rs.config()    {            "_id" : "spock",            "version" : 2,            "members" : [                    {                            "_id" : 1,                            "host" : "192.168.1.113:27017"                    }            ]    }

   

二、添加複本集成員  

spock:PRIMARY> rs.add("192.168.1.112:27017")    { "ok" : 1 }    spock:PRIMARY> rs.config()    {            "_id" : "spock",            "version" : 3,            "members" : [                    {                            "_id" : 1,                            "host" : "192.168.1.113:27017"                    },                    {                            "_id" : 2,                            "host" : "192.168.1.112:27017"                    }            ]    }

   
   
從上述過程來看,配置文檔中version欄位都會自增,它的初始值為1;

 

三、或者通過rs.reconfig()函數修改複本集

例如,將host用主機名稱來替代ip地址

[[email protected] ~]# ping host113   PING host113 (192.168.1.113) 56(84) bytes of data.    64 bytes from host113 (192.168.1.113): icmp_seq=1 ttl=64 time=0.060 ms    64 bytes from host113 (192.168.1.113): icmp_seq=2 ttl=64 time=0.030 ms    64 bytes from host113 (192.168.1.113): icmp_seq=3 ttl=64 time=0.026 ms    64 bytes from host113 (192.168.1.113): icmp_seq=4 ttl=64 time=1.04 ms    --- host113 ping statistics ---    4 packets transmitted, 4 received, 0% packet loss, time 3001ms    rtt min/avg/max/mdev = 0.026/0.289/1.041/0.434 ms    [[email protected] ~]# mongo    MongoDB shell version: 2.6.7    connecting to: test    spock:PRIMARY> rs.config()    {                "_id" : "spock",                "version" : 3,                "members" : [                                        { "_id" : 1, "host" : "192.168.1.113:27017"},                    { "_id" : 2, "host" : "192.168.1.112:27017"}                            ]     }    spock:PRIMARY> var config=rs.config()   spock:PRIMARY> config.members[0].host="host113:27017"    host113:27017    spock:PRIMARY> rs.reconfig(config)    2015-02-02T22:21:22.793+0800 DBClientCursor::init call() failed    2015-02-02T22:21:22.795+0800 trying reconnect to 127.0.0.1:27017 (127.0.0.1) faile2015-02-02T22:21:22.797+0800 reconnect 127.0.0.1:27017 (127.0.0.1) ok    reconnected to server after rs command (which is normal)spock:PRIMARY> rs.config()   {                "_id" : "spock",                "version" : 4,                "members" : [                                { "_id" : 1,"host" : "host113:27017"},            { "_id" : 2, "host" : "192.168.1.112:27017"}                            ]    }    spock:PRIMARY>

 

對於複雜的複本集配置,rs.reconfig函數修改比rs.add和rs.remove更有用;

本文出自 “緣隨心愿” 部落格,請務必保留此出處http://281816327.blog.51cto.com/907015/1610912

【MongoDB學習筆記32】MongoDB修改複本集配置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.