Prompt chaged to PRIMARY when I added superuser

When I ran rs.initiate the command prompt changed to m103-example:SECONDARY. This makes some sense, I thought “it’s now part of a replica set, but there are no other members, so it cannot be the primary”.

But as soon as I added the superuser, the prompt changed again, to m103-example:PRIMARY, and this I was not expecting.

Why does it do this?

That is the normal behavior.

Until a second node is added the single node has to become PRIMARY as you cannot write/update/modify anything on SECONDARY nodes.

My own question is why does it become SECONDARY right after rs.initiate(), even if only temporary for the time of a single prompt?

Great, that makes sense. If I read this behavior correctly:

(Speculation follows)
– the node on which you call initiate starts a replica set and joins it as secondary
– it waits a moment for others to join and then holds an election whether they do or not
– it becomes primary because there are no other candidates

To try to answer your question, perhaps no election can take place if there is a primary, and this is why node #1 joins the replica set as secondary.