About the port number of ReplicaSet

I’m connecting Mongo DB atlas with private endponint and pouring data, but the port of primary node changes automatically.

  1. I want to know why the port number changes automatically

  2. I want to know that it is possible to fix the port

I want to know the above two things.

Hi @1111005,

Welcome the community :wave:

I’m connecting Mongo DB atlas with private endponint and pouring data, but the port of primary node changes automatically.

Can you confirm the port number changes you are seeing and if you received any errors in regards to the port changes?

Regards,
Jason

thank you for your answer.
I made a note before and found out that the Port Number has changed.
I’m in the process of asking the customer if I can get the log file. Is the error correct in the recognition that I check from the log file?

[history]
November 21, 2021
Primary Node Port Number 1030
Secondary Node Port Number 1031
Secondary Node Port Number 1026

January 11, 2022
Primary Node Port Number 1031
Secondary Node Port Number 1030
Secondary Node Port Number 1026

Hi @1111005,

Thanks for getting back to me with that information.

Ports 1026,1030 and 1031 are used based off the history notes you provided both before and after the noted change. I would like to note that these port numbers are not the port numbers the mongod process is listening on. Instead they are ports that Atlas assigns on the load balancer for each Atlas replica set node in the region for which you enabled AWS PrivateLink. Please see further details regarding this on the Atlas private endpoint documentation.

Is the error correct in the recognition that I check from the log file?

Based off the information provided, the main change is the port number that the primary was associated with so you can check the logs to see if any elections had occurred where a new primary was elected.

In the example below, I have a cluster on Atlas which I have connected to using the private endpoint connection.

The initial direct connection over the private link using port 1043:

/// connected to primary
ubuntu@ip-10-0-0-9:~$ mongosh "mongodb://pl-0-ap-southeast-2.zi3an.mongodb.net:1043/myFirstDatabase" --tls --authenticationDatabase admin --username REDACTED

Enter password: *****************
Current Mongosh Log ID: 61e74e3ea2191e26f2833b3f
Connecting to:    mongodb://pl-0-ap-southeast-2.zi3an.mongodb.net:1043/myFirstDatabase?directConnection=true
Using MongoDB:    4.2.18
Using Mongosh:    1.1.8

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

Atlas atlas-mowsqx-shard-0 [direct: primary] myFirstDatabase> exit

Performed a test failover, waited for the failover to complete and used the same direct connection over the private link to port 1043:

/// connected to secondary
ubuntu@ip-10-0-0-9:~$ mongosh "mongodb://pl-0-ap-southeast-2.zi3an.mongodb.net:1043/myFirstDatabase" --tls --authenticationDatabase admin --username REDACTED

Enter password: **************
Current Mongosh Log ID: 61e74fb9a5c6f49b607179aa
Connecting to:    mongodb://pl-0-ap-southeast-2.zi3an.mongodb.net:1043/myFirstDatabase?directConnection=true
Using MongoDB:    4.2.18
Using Mongosh:    1.1.8

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

Atlas atlas-mowsqx-shard-0 [direct: secondary] myFirstDatabase> exit
  1. I want to know that it is possible to fix the port

It is not possible to fix the port with regards to the private link port association to the replica set nodes. I recommend connecting using the DNS seedlist connection format as this should connect you to the primary each time without needing needing to specify a port. However, If you are using the Standard connection string format please be sure to include the replicaSet option as this will search for all nodes within the replica set and connect you to the primary.

Hope this helps.

Regards,
Jason

3 Likes

thank you.
After setting the “DNS seedlist connection format”, the port problem was resolved.

2 Likes

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