Setting up Replication

When setting up the replication the command:

mongod --port “PORT” --dbpath “YOUR_DB_DATA_PATH” --replSet "REPLICA_SET_INSTANCE_NAME

is issued. Questions relating to the command:

  1. Is it possible for the Secondary nodes from the Replicate Set to only copy certain collections in a DB. Can it copy multiple DBs?
  2. Besides Port number 27017 what other ports can be used to create the replSet mongod instance. What ports can be used fo Replication ?

How can you setup a Replicate Set where the members are in different networks? Since the rs.add(HOST_NAME:PORT NUMBER) command to add members will not work since the HOST NAME cannot be recognized because the network is not private but a public one.

Hi @Master_Selcuk ,

Replica sets are designed for high availability and. failover, so each replica set member includes the same set of data (with the exception of the local system database, which is used for instance-specific data like the replication oplog). In a failover/election scenario, you want to have multiple members of a replica set eligible to become primary so there is some fault tolerance allowing some members of your replica set to be temporarily unavailable while still maintaining a primary.

If you only want to copy a subset of collections or databases, you will have to work out a separate solution from the built-in replication. For example, you could have multiple MongoDB deployments and Back up & Restore selected databases with MongoDB tools. You could also implement custom sync in application code using Change Streams to follow and relay data changes of interest.

You can use any available port on your local server:

  • Ports 1024-49151 (“registered ports”) are used for applications. Popular applications often have registered standard ports to avoid conflicts for default installations. The default MongoDB ports are 27017-27020. You generally want to avoid using a port commonly used by another service (for example, MySQL’s default port is 3306) but the main technical limitation is that you can’t have two different processes listening to the same IP:port combination.

  • Ports 0-1023 (“welll-known ports”) are usually used for system services (DNS, SSH, LDAP, …) and typically require superuser access (so are not recommended for applications like MongoDB).

  • Ports 49152-65535 are typically used for dynamic or ephemeral ports.

Your replica set members will need to be able to communicate with each other via the configured IP:port combinations, so you will have to set up appropriate firewall and networking configuration. The MongoDB Security Checklist has more information on recommended security measures.



Thanks for the detailed answers, to further iterate the first question all the DBs available will be replicated to the Secondary nodes is my understanding.

Hi @Master_Selcuk,

Yes, replication has two general stages:

  • Initial Sync: a newly added secondary copies all of the data from another member

  • Replication: ongoing continuous replication after initial sync. Secondaries either pull their updates from the current primary or from another more up-to-date secondary that has less network latency.

For more details please review Replica Set Data Synchronization.

If you want a deeper dive into MongoDB administration I also recommend the free online courses in the MongoDB University Learning Path for DBAs.

M103: Basic Cluster Administration has some useful background and exercises for different on-premises deployment types (standalone, replica set, sharded cluster).


1 Like

thank you I will check the courses out.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.