Search code examples
mongodbreplicaset

Need to change storage db path in mongo secondary node of replica set


We have 3 members replica set of mongo db (MongoDB server version: 4.2.9). one of the member (secondary) is running on default storage db path and this storage getting nearly full. We need to change path of that specific secondary node of 3-member replica set. I am new to mongodb. I need your expertise to accomplish this task.

MongoDB server version: 4.2.9 OS Version: CentOS Linux release 7.8.2003 (Core).

Regards,


Solution


  • Option 1: ( preserving the current already synced data )

    1. Stop the mongod service:

      mongo --host xxxx --port yyyy  
      use admin
      db.shutdownServer()
      
    2. Copy the data from old location to the new location:

      cp -R /oldPath/ /newPath/
      
    3. Change in the /etc/mongodb.conf the dbPath to point to the new location.

    4. Start the mongodb service.


    Option 2) Init sync the member from the other members.

    Same like option 1) , just ommit the point 2) just you will need to wait abit until the member init sync ( copy the data ) from the other members , it depends on data size and compression.


    Option 3) Better high availability:

    1. From the PRIMARY member add another member on that will use the new path:

      rs.add("newMember:XXXX")
      
    2. Wait until the new member init sync.

    3. Remove the old SECONDARY member:

      rs.remove("oldMember:YYYY")
      
    4. Clean the old files and configuration. ( small disadvantage with this option that you will need to change the port for the new member if on the same host )

    Option 2) seems best suitable for your case ...