Mongodb replica set add/delete node method

Source: Internet
Author: User
Tags mongodb

Replica set multi-server master/slave, add and delete nodes, which are common. The following describes how to add or delete a node in detail.

1. Use rs. reconfig to add and delete nodes

1. Add nodes

The code is as follows: Copy code

Repmore: PRIMARY> config = {_ id: "repmore", members: [{_ id: 0, host: '2017. 0.0.1: 100', priority: 2}, {_ id: 1, host: '123. 0.0.1: 27018 ', priority: 1}]}; // add a node

Repmore: PRIMARY> rs. reconfig (config); // Make the configuration take effect

Repmore: PRIMARY> rs. status (); // view node status


Node added successfully.

Note: replSet of the newly added node must be the same as that of other nodes.

2. Delete a node (it is best to close the node to be deleted before deleting the node, and then delete it by running the rs. remove command)

The code is as follows: Copy code

Repmore: PRIMARY> config = {_ id: "repmore", members: [{_ id: 0, host: '2017. 0.0.1: 27017 ', priority: 2}]}; // delete a node

Repmore: PRIMARY> rs. reconfig (config); // Make the configuration take effect

Repmore: PRIMARY> rs. status (); // view node status

2. Use rs. add and rs. remove to add and delete nodes.

The code is as follows: Copy code

Repmore: PRIMARY> rs. add ("127.0.0.1: 27018"); // add a node

Repmore: PRIMARY> rs. remove ("127.0.0.1: 27018"); // delete a section


Note: using rs. add and rs. remove does not require rs. reconfig to take effect.


Finally, I will add a standard process for you to delete nodes.


The operations are as follows:
  

The code is as follows: Copy code

-- 1 view the current Replica Set configuration

[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"
                }
        ]
}

  
Note: "_ id": 3.


-- 2 close the 27021 Node Service

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

  


-- 3 view the status of the 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
}

Note: the status of the last node stateStr is not reachable/healthy ".
 
-- 4 delete a node

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 view the replica set configuration again

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"
                }
        ]
}

   
Note: The node "redhatB.example.com: 27021" has been deleted.

-- 6 delete the data directory of the old node

The code is as follows: Copy code

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.