Rs.initiate() return "not authorized on admin to execute command"

I met this problem when I was learning the Chapter 2: Replication — Setting Up a Replica Set.

I already mongod node1-3, and I got “MongoDB Enterprise m103-example:PRIMARY>” when I enter mongo shell by using mongo --port 27011.

Then I received error message follows after using rs.initiate().

{
“operationTime” : Timestamp(1540398758, 1),
“ok” : 0,
“errmsg” : “not authorized on admin to execute command { replSetInitiate: undefined, $clusterTime: { clusterTime: Timestamp(1540398748, 1), signature: { hash: BinData(0, F881F33E34B61395F620F2EFF4578D9C8551D114), keyId: 6615942630343704578 } }, $db: “admin” }”,
“code” : 13,
“codeName” : “Unauthorized”,
“$clusterTime” : {
“clusterTime” : Timestamp(1540398758, 1),
“signature” : {
“hash” : BinData(0,“ZVrcG9QEA+Y+7tbuRFPzRIpOdXM=”),
“keyId” : NumberLong(“6615942630343704578”)
}
}
}

The first question would be: which account are you logged in with?

db.runCommand({connectionStatus : 1})

Check the authenticatedUsers and authenticatedUserRoles fields. In order to initiate a replica set, you have to have very high privileges such as root.

my authInfo is null…
“authInfo” : {
“authenticatedUsers” : [ ],
“authenticatedUserRoles” : [ ]
},

Well then there’s your smoking gun :slight_smile:

Is your MongoD running in the secured mode that requires authentication? (“authorization: true”). If not, then you cannot perform authenticated actions.

If it is, did you connect the shell in an authenticated fashion? (mongo --username XXX --password --authenticationDatabase admin etc etc). If not, then you cannot perform authenticated actions.

I just follow the ‘Lecture Notes’ that it said mongod running node 1-3, and only run mongo in port 27011 without authenticated, then I got nothing.

I can get the ‘authInfo’ which likes “authInfo” : {
“authenticatedUsers” : [
{
“user” : “m103-admin”,
“db” : “admin”
}
],
“authenticatedUserRoles” : [
{
“role” : “root”,
“db” : “admin”
}
]
},
by using ‘-u “m103-admin” -p “m103-pass”’. But the feedback of rs.initiate() is different with the lecture notes, such as
{
“info2” : “no configuration specified. Using a default configuration for the set”,
“me” : “192.168.103.100:27011”,
“info” : “try querying local.system.replset to see current configuration”,
“ok” : 0,
“errmsg” : “already initialized”,
“code” : 23,
“codeName” : “AlreadyInitialized”,
“operationTime” : Timestamp(1540411060, 1),
“$clusterTime” : {
“clusterTime” : Timestamp(1540411060, 1),
“signature” : {
“hash” : BinData(0,“A6hhEdf75v+vDFlpD7wkhlUPjlU=”),
“keyId” : NumberLong(“6615942630343704578”)
}
}
}

Hang on, it seems that you’re mixing various assignments.

The multi-node configuration with nodes 1 through 3 uses ports 27001-27003, not the 27011 mongod. So please make sure that you keep them all separate in your head :slight_smile:

Your second copy/paste shows that the replication set has already been configured. Look at the errmsg field.

I checked conf code of lecture notes again, there are 27011-27013, is it wrong???:face_with_raised_eyebrow:

Sorry, I must’ve read that incorrectly, as I set things up with 27001-27003. Apologies.

Either way, the last error message you posted shows that the replica set is already configured. So now you’re left with adding the other nodes and with checking the current rs.status().

EDIT:
Nope, I was remembering it correctly. The lab instructions for this week specifically tells us to build things on 27001-3 :slight_smile: -> https://university.mongodb.com/mercury/M103/2018_October/chapter/Chapter_2_Replication/lesson/5a4d32d779235b109001c7a4/tab/5a4d32e079235b109001c7a5

I see where you got the 27011-27013 from thought; that’s what is shown in the lecture notes two pages before that -> https://university.mongodb.com/mercury/M103/2018_October/chapter/Chapter_2_Replication/lesson/5a9b45ca97182d4140a45eb3/tab/5a9b45ca97182d4140a45eb4

It’s ok, the new issue is rs.add(“m103.mongodb.university:27012”),
then feedback is
{
“ok” : 0,
“errmsg” : “Quorum check failed because not enough voting nodes responded; required 2 but only the following 1 voting nodes responded: 192.168.103.100:27011; the following nodes did not respond affirmatively: m103.mongodb.university:27012 failed with HostUnreachable”,
“code” : 74,
“codeName” : “NodeNotFound”,
“operationTime” : Timestamp(1540412489, 1),
“$clusterTime” : {
“clusterTime” : Timestamp(1540412489, 1),
“signature” : {
“hash” : BinData(0,“n5BohSJT1XNnydRQehbS2Z6RCgs=”),
“keyId” : NumberLong(“6615942630343704578”)
}
}
}

yes, they are different between lecture and lab

Now come on :slight_smile: This is basic networking troubleshooting and you shouldn’t be copying the commands from the class notes 1:1.

Try to understand what you’re asking MongoDB to do here. You’re telling it to:

  • Add a replica set member
  • The new member is host m103.mongodb.university
  • The port is 27012

Is m103.mongodb.university really a host that exists on your network? :wink: No, it’s not. Either add the proper IP address, or the alias “m103” that was registered on the Vagrant VM in /etc/hosts.

EDIT:
Correction, I am an idiot. I just checked /etc/hosts and the Mongo-U folks actually -did- add the hostname+domain from the example in there as well. So I’m wrong about your hostname usage. Sorry!

But there’s something that appears to be blocking you from accessing it, so we’re still down to basic troubleshooting.

getent hosts m103.mongodb.university
cat /etc/hosts
ping m103.mongodb.university
netstat -an | grep “:27”
netcat localhost 27012
netcat m103.mongodb.university 27012

Oh, I got it…I’m a back-end rookie. This lecture is unfriendly for a leaner…
I use rs.add(“192.168.103.100:27012”) works. Thanks man! :smiley_cat:

You’re very welcome! And I just checked to see that I am wrong: the VM + Vagrant plan we’re given -should- make that hostname resolvable. The problem I found though, is that it sometimes points to 127.0.1.1 instead of 127.0.0.1. And because MongoD doesn’t listen on the 1.1 address, you can’t connect.

Also, sorry if I sounded harsh :confused: That was not my intention.

@Kanika: this is something you guys may want to look into, the “m103” hostname is often resolved to 127.0.1.1 instead of 127.0.0.1 which is not an IP our MongoD listener gets tied to in the exercises.

Hi Tess_Sluijter,

Thanks for the note. We will do the needful change. But these processes take long time. :slight_smile:

Kanika

1 Like

what is solution badly stuck in Lab - Remove and Re-Add a Node homework.
should i use in configuration .conf filr
bindip : localhost,m103

or any thing else.?

MongoDB Enterprise m103-repl:PRIMARY> rs.initiate()
{
“operationTime” : Timestamp(1540463935, 1),
“ok” : 0,
“errmsg” : “not authorized on admin to execute command { replSetInitiate: undefined, $clusterTime: { clusterTime: Timestamp(1540463915, 1), signature: { hash: BinData(0, 4E2C9A327526EDBC2C8A8F53AE8AF4DC0A30B985), keyId: 6615851233439645698 } }, $db: “admin” }”,
“code” : 13,
“codeName” : “Unauthorized”,
“$clusterTime” : {
“clusterTime” : Timestamp(1540463935, 1),
“signature” : {
“hash” : BinData(0,“zrNqvZuzgpWdJZii/gMSwb19CI8=”),
“keyId” : NumberLong(“6615851233439645698”)
}
}
}

This error also come after initiating rs.

revert please.
Thank you.

Hi Nagesh_46115,

You don’t need to change your configuration file as mentioned in the lab too. You need to remove one node from your current replica cluster. And add it back using m103 instead of 192.168.103.100. Please check my reply here.

You need to login using m103-admin user while initiating mongod.

1 Like

How to remove below error

MongoDB Enterprise m103-repl:PRIMARY> rs.initiate()
{
“operationTime” : Timestamp(1540464941, 1),
“ok” : 0,
“errmsg” : “not authorized on admin to execute command { replSetInitiate: undefined, $clusterTime: { clusterTime: Timestamp(1540464941, 1), signature: { hash: BinData(0, 60A8D64E9AA7BAFE2DF67DD20FFEEDDE28DD1946), keyId: 6615851233439645698 } }, $db: “admin” }”,
“code” : 13,
“codeName” : “Unauthorized”,
“$clusterTime” : {
“clusterTime” : Timestamp(1540464941, 1),
“signature” : {
“hash” : BinData(0,“YKjWTpqnuv4t9n3SD/7t3ijdGUY=”),
“keyId” : NumberLong(“6615851233439645698”)
}
}
}

Thanks

Hi Nagesh_46115,

I thought I already shared, you need to authenticate with the valid user:

MongoDB Enterprise m103-repl:PRIMARY> use admin
MongoDB Enterprise m103-repl:PRIMARY> db.auth("m103-admin","m103-pass")

Then try initiating the replica set. And if it is already initialised, check its status using:

MongoDB Enterprise m103-repl:PRIMARY> rs.status()

I hope it helps!

Kanika

That’s what I have been saying: yes, you DO need to edit the mongod config because the host record for “m103” resolves to 127.0.1.1 and not to 127.0.0.1. The mongod does NOT listen on that IP address, which is why you cannot connect to it.

So either /etc/hosts needs to be adjusted to m103 is hardcoded to 127.0.0.1, or the mongod config needs to be updated so the listener also binds to 127.0.1.1. The latter is easier, since it’s already part of this training.

Host name should be placed along with localhost like:

bindip: localhost,m103

or
simply
bindip: m103

Thanks.

1 Like