mongodb replica set 添加/刪除節點方法

來源:互聯網
上載者:User

replica set多伺服器主從,添加,刪除節點,肯定會經常遇到的。下面詳細說明一下,添加,刪除節點的2種方法。

一,利用rs.reconfig,來添加,刪除節點

1,添加節點

 代碼如下 複製代碼

repmore:PRIMARY> config = {_id:"repmore",members:[{_id:0,host:'127.0.0.1:27017',priority :2},{_id:1,host:'127.0.0.1:27018',priority:1}]};   //添加節點

repmore:PRIMARY> rs.reconfig(config);   //使配置生效

repmore:PRIMARY> rs.status();     //查看節點狀態


節點添加成功。

注意:新增節點的replSet要和其他節點要一樣

2,刪除節點(刪除節點前最好是先關閉需要刪除的節點,之後通過命令 rs.remove 來刪除)

 代碼如下 複製代碼

repmore:PRIMARY> config = {_id:"repmore",members:[{_id:0,host:'127.0.0.1:27017',priority :2}]};     //刪除節點

repmore:PRIMARY> rs.reconfig(config);   //使配置生效

repmore:PRIMARY> rs.status();   //查看節點狀態

二,利用rs.add和rs.remove來添加刪除節點

 代碼如下 複製代碼

repmore:PRIMARY> rs.add("127.0.0.1:27018");     //添加節點

repmore:PRIMARY> rs.remove("127.0.0.1:27018");  //刪除節


注意:利用rs.add和rs.remove是不用rs.reconfig來使用配置生效的。


最後對於刪除節點我這裡再附一個標準流程給各位


相應的節點,操作如下:
  

 代碼如下 複製代碼

--1 查看當前 Replica Set 配置

 [mongo@redhatB mongodb]$ mongo 127.0.0.1:27018
MongoDB shell version: 2.2.1
connecting to: 127.0.0.1:27018/test
rs0:PRIMARY> rs.conf();
{
        "_id" : "rs0",
        "version" : 4,
        "members" : [
                {
                        "_id" : 0,
                        "host" : "redhatB.example.com:27018"
                },
                {
                        "_id" : 1,
                        "host" : "redhatB.example.com:27019"
                },
                {
                        "_id" : 2,
                        "host" : "redhatB.example.com:27020"
                },
                {
                        "_id" : 3,
                        "host" : "redhatB.example.com:27021"
                }
        ]
}

  
   備忘:計劃刪除節點 "_id" : 3。


--2 關閉 27021 節點服務

 [mongo@redhatB data04]$ ps -ef | grep 27021
mongo    11733     1  0 21:03 ?        00:00:15 mongod -f /pgdata_xc/mongodb/data04/mongodb_27021.conf
mongo    14422  2953  0 21:53 pts/0    00:00:00 mongo 127.0.0.1:27021
mongo    14490  4027  0 21:54 pts/1    00:00:00 grep 27021
[mongo@redhatB data04]$ kill 11733

  


--3 查看 replica set 狀態

 rs0:PRIMARY> rs.status();
{
        "set" : "rs0",
        "date" : ISODate("2012-11-22T13:57:15Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "name" : "redhatB.example.com:27018",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 21279,
                        "optime" : Timestamp(1353589624000, 1),
                        "optimeDate" : ISODate("2012-11-22T13:07:04Z"),
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "redhatB.example.com:27019",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 18908,
                        "optime" : Timestamp(1353589624000, 1),
                        "optimeDate" : ISODate("2012-11-22T13:07:04Z"),
                        "lastHeartbeat" : ISODate("2012-11-22T13:57:13Z"),
                        "pingMs" : 0
                },
                {
                        "_id" : 2,
                        "name" : "redhatB.example.com:27020",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 18900,
                        "optime" : Timestamp(1353589624000, 1),
                        "optimeDate" : ISODate("2012-11-22T13:07:04Z"),
                        "lastHeartbeat" : ISODate("2012-11-22T13:57:14Z"),
                        "pingMs" : 0
                },
                {
                        "_id" : 3,
                        "name" : "redhatB.example.com:27021",
                        "health" : 0,
                        "state" : 8,
                        "stateStr" : "(not reachable/healthy)",
                        "uptime" : 0,
                        "optime" : Timestamp(1353589624000, 1),
                        "optimeDate" : ISODate("2012-11-22T13:07:04Z"),
                        "lastHeartbeat" : ISODate("2012-11-22T13:55:20Z"),
                        "pingMs" : 0,
                        "errmsg" : "socket exception [CONNECT_ERROR] for redhatB.example.com:27021"
                }
        ],
        "ok" : 1
}

  備忘:最後一個節點 stateStr 狀態為 “not reachable/healthy”。
 
--4 刪除節點

 rs0:PRIMARY> rs.remove("redhatB.example.com:27021");
Thu Nov 22 21:58:45 DBClientCursor::init call() failed
Thu Nov 22 21:58:45 query failed : admin.$cmd { replSetReconfig: { _id: "rs0", version: 5, members: [ { _id: 0, host: "redhatB.example.com:27018" }, { _id: 1, host: "redhatB.example.com:27019" }, { _id: 2, host: "redhatB.example.com:27020" } ] } } to: 127.0.0.1:27018
Thu Nov 22 21:58:45 Error: error doing query: failed src/mongo/shell/collection.js:155
Thu Nov 22 21:58:45 trying reconnect to 127.0.0.1:27018
Thu Nov 22 21:58:45 reconnect 127.0.0.1:27018 ok
rs0:SECONDARY>
rs0:PRIMARY>

  


--5 再次查看 replica set 配置

 rs0:PRIMARY> rs.conf();
{
        "_id" : "rs0",
        "version" : 5,
        "members" : [
                {
                        "_id" : 0,
                        "host" : "redhatB.example.com:27018"
                },
                {
                        "_id" : 1,
                        "host" : "redhatB.example.com:27019"
                },
                {
                        "_id" : 2,
                        "host" : "redhatB.example.com:27020"
                }
        ]
}

   
  備忘:節點 "redhatB.example.com:27021" 已刪除。

 --6 刪除舊結點資料目錄

 代碼如下 複製代碼

 [mongo@redhatB mongodb]$ rm -rf  /mongodb/data04 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.