3

I have put all the steps to initiate a replica-set in a shell script. Basically, I am trying to convert a standalone instance A to replica-set that includes one arbiter B and one secondary node C. These are the following steps in the script:

  1. Start the standalone instance without auth parameter.
  2. Create an admin user for mongodb admin database with the following role:

    Successfully added user: { "user" : "mongodb_admin", "roles" : [ { "role" : "root", "db" : "admin" } ] }

  3. Stop the standalone instance A.
  4. Start all the three nodes(A, B,C) with replica-set option and key-file access control.
  5. Initiate replicaset on instance A by calling rs.initiate.
  6. Call rs.status to check if the health of all members is 1 (currently there is only A in replicaset)
  7. Add the arbiter node B and secondary node C to replicaset.
  8. Wait until all the nodes are healthy.

Everything is fine upto step 5. I am able to successfully initiate a replicaset. But step 6 throws the following error:

2018-08-27T11:43:47.080+0100 E QUERY [thread1] Error: auth failed : 
DB.prototype._authOrThrow@src/mongo/shell/db.js:1608:20 @(auth):6:1 
@(auth):1:2

When I put a sleep of 15 seconds in between steps 5 and 6, it works perfectly fine. But I still want to make sure that I am doing things correctly. If putting sleep is fine, then how can I make sure that 15 seconds is fine to wait before checking the status in step 6. Also I am unable to understand why its giving an auth error even though I used the same user and password for initiating the replica-set?

MongoDB version: 3.6.2

Vince Bowdren
  • 8,326
  • 3
  • 31
  • 56
Eman Zaman
  • 31
  • 3

1 Answers1

2

I know this question is a bit dated, but try running this after the call to rs.initiate in step 5.

let isPrimary = false;
let count = 0;

while(isPrimary == false && count < 30) {
  const rplStatus = db.adminCommand({ replSetGetStatus : 1 });

  isPrimary = rplStatus.members[0].stateStr === "PRIMARY";
  print("is primary result: ", isPrimary);
  count = count + 1;
  sleep(1000);
}

This would filter the status of the replica set and try every second for 30 seconds to check that the status of the first member of the replica set has changed to "PRIMARY", at which point it will release the thread for further execution.

goonerify
  • 1,668
  • 25
  • 27