Segregation of DB’s from Existing Replica Set to New Replica Set in MongoDB.

Dinesh Chandra Choubey
3 min readDec 23, 2020

In this Article I am sharing one of my learning, Might be helpful for someone.
Recently there was a requirement that we have to segregate the DB’s from existing Replica Set and create a new replica Set of these DB’s.

E.g.
We have 4 DB’s AA, BB, ZZ, YY in a Replica Set and have to move 2 DB’s i.e ZZ and YY DBs to New Nodes and create a new replica Set.

IP’s/Port
192.168.2.1:27017 (Primary)
192.168.2.2:27017 (Secondary)
192.168.2.3:27017 (Secondary-Hidden node)

Replica Set Name : “repl-Current”
Now we have to move it to new nodes and create it a new replica Set and IPs are:

IP’s/Port

192.168.2.101:27020 (Primary)
192.168.2.102:27020 (Secondary)
192.168.2.103:27020 (Secondary-Hidden node)

Replica Set Name : “repl-New”

So , Replica Set Name : “repl-Current” is currently running which is a 3 node ReplicaSet and Server IP’s are 192.168.2.1 , 192.168.2.2 , 192.168.2.3

To do the activity with Minimal Downtime we will follow below steps:-

Step 1:-
First we will add a new node (192.168.2.101) in replicaSet repl-Current.
To add a new node in existing replica Set, we can use two methods
a) Intial Sync — — In case, we are using MongoDB version > = 3.4, This approach will be fast. Also Disk read Iops will increase on Source node but we can choose sync target from which node we want to Intial Sync one New Node and command will be rs.syncFrom(“hostname<:port>”) and it will be execute after adding a new node in Existing Replica Set
b) Stopping the Hidden Node and Initiate Data Copy from it to new node and Once Data Copy completion Completed, we will start the MongoDB on 192.168.2.101 with Copied data and add node on Primary 192.168.2.1 after executing

>rs.add({hosts: “192.168.2.101:27020”, priority: 0 , votes:0 , hidden: true})

and then after sometime New Node will be in sync with Primary.
Step 2:-
Now we will take the Downtime from Business & We will Shutdown the application Services & we will change the application code connection String/URI (For which we are targetting to move on New Replica Set) and replace old IPs, Port & Replica Set
At DB End we will follow below Steps:-
a) ShutDown the mongoDB on Server 192.168.2.101:27020.
b) Start the MongoDB without Replica Set after commenting the Replica Set Name in mongod Config.
c) Once MongoDB started, login to mongo Shell and execute below commands to remove the existing mongoDB configurations in local DB.
>use local
>db.dropDatabase()

d) Then we will again stop the MongoDB process on 192.168.2.101:27020.
e) and restart the mongoDB process after changing the Replica Set name from “repl-Current” to “repl-New” and uncommenting it.
f) Once mongoDB service, After Login to Mongo Shell, execute below command:-
>rs.initiate()
g) And this node will become Primary.
e) Then we can start the Application which is going to connect on this replica Set.
g) Further we will initiate initial Sync on 192.168.2.102:27020 & 192.168.2.103:27020 nodes.

In Step 2 will take hardly 10 mins of time and this will be the total Downtime for Segregating some of the DB’s from exiting Replica Set to New Replica Set.

Step 3:-
a) Now we can Safely drop YY and ZZ DB’s from rpl-Current Replica Set after login in to the Primary.
b) and Drop AA and BB DB’s from rpl-New Replica Set after login in to the Primary.

--

--

Dinesh Chandra Choubey

Open Source Enthusiast, Working as Database Administrator. MySQL, MongoDB, Cassandra, Linux, AWS. Passionate to know about new technologies.