Mongodb – Ports to use for mongodb sharding + replication

mongodb

When deploying a replica set or a sharded cluster (any situation with several mongod's really) – how does one know what port numbers to use?

Up until now I have used all the port no's I've found in examples and tutorials, but there doesnt seem to be any obvious system or way to go about it.
For example, one example uses ports 27010 – 27014. Another example uses 27001-27003, 27020-22, 26050 etc.

The only information I found on the docs of mongodb, was the one of the default ports.

As a student (so not production deployment) who is just using a single machine, what should I use? The default ones are only 4 and I probably need atleast 8 mongod's running (for a small sharded cluster with replica sets).
Can more than 1 shardserver be using the same port?

(I am sure this is a naive question as this has not been asked about other places nor addressed in any of the mongodb books I have found, but I want to know.)

Thankful in advance.

Best Answer

You can use any ports you like. Particularly on a toy system, it makes no practical difference what number port you choose; it works just as well with port 29589 as port 27017.

Some things to keep in mind:

  • on a single machine, two different processes must listen on different ports. So no, you can't run two shard servers on the same port.
  • you need to administer all these processes; if you pick a system where different sets of ports are used for different purposes, that will make it easier to keep track. For example, you might choose to run configsvr processes on ports 21001-21003, shard mongod processes on ports 22001-22999, and mongos processes on ports 23001 upwards.
  • the default port is 27017, and that is public knowledge; so if your system is open to the internet, be aware that malicious ransomers using port scanners will be looking for an exposed mongod system on port 27017 they can access and exploit. So you might prefer to avoid using that port, to add an extra layer to your security.