Connecting to the replica set

I’m not sure I understand the difference between connecting to one of the nodes in a replica set:
mongo --port 27003 -u m103-admin -p m103-pass

and connecting specifying the replica set name:
mongo --host “m103-example/m103.mongodb.university:27011” -u “m103-admin” -p “m103-pass” --authenticationDatabase “admin”

In both cases I connect to the host and port I specify - what does adding the name of the replica set to the connection string actually do?

1 Like

Hey @dgiffin

Let us say that the connection you are making is one from a Client application.
So if the connection like your first example

And the server running the DB goes down the application will lose all ability to access the data.

However If we have a 3 node replica set and the connection is like your second example.

And the server running the PRIMARY goes down. Then there is an election and a new PRIMARY will be selected. In which case the application will then connect to the new PRIMARY, with one SECONDARY replicating the data; until the first server comes back online.

Hope that help!

4 Likes

So in the second case you don’t have to list all of the hosts in the replica set in the connection string? just list one that you will connect to initially, but if that node fails then the replica set will automagically hand your connection off to another node after the election?

The documentation suggests you DO have list all of the hosts:

mongodb://mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/admin?replicaSet=myRepl

So I’m a bit confused.

Hi @dgiffin,

So in the second case you don’t have to list all of the hosts in the replica set in the connection string? just list one that you will connect to initially, but if that node fails then the replica set will automagically hand your connection off to another node after the election?

Yes that’s right!

You may also refer to the following documentation link for better understanding:
https://docs.mongodb.com/manual/administration/replica-set-deployment/

Happy learning!

Muskan
Curriculum Support Engineer

2 Likes

No, that doesn’t;t really help, what I want to know is what is the difference between these 2 connection strings to a replica set:

  1. mongo --host “m103-example/host1:27011” -u “m103-admin” -p “m103-pass” --authenticationDatabase “admin”

  2. mongo --host “m103-example/host1:27001, host2:27001, host3:27001” -u “m103-admin” -p “m103-pass” --authenticationDatabase “admin”

Hi @dgiffin,

There is no difference between the two connection strings.
However, please rectify your connection string to use the correct port names for each node as shown below.

mongo --host “m103-example/host1:27011, host2:27012, host3:27013” -u “m103-admin” -p “m103-pass” --authenticationDatabase “admin”

Thanks,
Muskan
Curriculum Support Engineer

So specifying more than 1 host in the replica set connection string makes no difference? This doesn’t seem to make sense to me, sorry.

Hi @dgiffin,

Both ways will work fine and there is no difference in the results.

The only difference is that specifying the replica set name with primary member is meant to be a shortcut: to avoid having to add every member name.
If you are including all members in the connection string, you don’t need to also include the replica set name.

Please let me know if you have any further questions.

Thanks,
Muskan
Curriculum Support Engineer

Ok, I think I understand, a follow-on question if I may…

  1. What happens if I don’t put the replica set name but only specify SOME of the members of the replica set, does that any practical difference?

Actually no, I don’t understand, sorry!

This always connects me to the primary:

mongo --host “m103-repl/localhost:27001” -u m103-admin -p m103-pass

In this example it connected me to localhost:27002 as that was primary (checked with rs.isMaster())), I tried this several times

But this always connects me to the first listed host

mongo --host “localhost:27001,localhost:27002,localhost:27003” -u m103-admin -p m103-pass

If I swap the hosts around in the connection string it connects to the first listed.
If I include a non-existent host in the connection string there is no error, UNLESS it is listed first

That doesn’t look like the first option is a “shorthand” for the second as the behaviour is different, what am I missing?

Hi @dgiffin,

Let me start by showing you the configuration. I have below replica set configured on my machine.

Setup: My replica set is having 3 nodes:

Primary: 27001
Secondary: 27002
Secondary: 27003

If you specify single or multiple hosts (without replica set name) in the connection string, it will always connect to the first mentioned node regardless of the node being primary or secondary.
Please check below screenshots:



However if you specify replica set name along with the host, then you will always be connected to the primary node of the replica set:


To check the node to which you are connected you can do so using db.serverStatus().



In this case, if you mention the replica set name, then you will connect to the primary node of the replica set. It will also show the error message while connecting to the non-existent node as shown in the screenshot below:


If you skip the replica set name, then it will connect to the other node mentioned in the connection string. In this case also, error message will be displayed while connecting to the non-existent node:

I hope it helps!!

Please let me know, if you have any questions.

Thanks,
Sonali

3 Likes

So, if I do NOT use the replica set name in the connection string mongo works through the list of named hosts in order until it finds one it can connect to BUT in that case of the node I am connected to goes down I do NOT get switched to another node.

If I DO put the name of the replica set in the connection string I always get connected to the primary, regardless of which hosts(s) or the order in which they are listed, and if the node I am connected to goes down I will get moved to the new primary.

Is that correct?

Hi @dgiffin,

If you are already connected to a node, and later that node goes down, you will not be connected to the other nodes in replica set. It will just give you “Connection attempt failed error” as shown in the screenshot below:


You will need to exit the mongo shell in this case and connect to other healthy node.

In this case, if you are already connected to the primary node of the replica set, and then the primary node goes down, then Replica Set Election will take place and after the new primary is elected, you will be connected to the new primary node automatically.

I hope it helps!!

Please let me know, if you have any questions.

Thanks,
Sonali

1 Like