Not authorized to create user

Hi !
I’m sure I’m overseeing something obvious when going through a “Deploy Replica Set” lab.

I am unable to create the root user:

db.createUser({
  user: "m103-admin",
  pwd: "m103-pass",
  roles: [
    {role: "root", db: "admin"}
  ]
})

results in this output:

Error: couldn't add user: not authorized on test to execute command { createUser: "m103-admin", pwd: "xxx", roles: [ { role: "root", db: "admin" } ], digestPassword: true, writeConcern: { w: "majority", wtimeout: 600000.0 }, lsid: { id: UUID("2a2face7-705c-42b4-a680-d4b9675a6694") }, $clusterTime: { clusterTime: Timestamp(1667242504, 1), signature: { hash: BinData(0, FED6DECB4A44CD42A40C87327AC1AF9221759FB2), keyId: 7160749967596847105 } }, $db: "test" } :
_getErrorWithCode@src/mongo/shell/utils.js:25:13

I entered the mongo shell as: mongo --port 27001 and initiated the rs.
I got the PRIMARY prompt as expected, however was unable to create a user.
Thank you.

It helps if you state which chapter/lab you’re having problems with. I will assume that this is for Chapter 1: The Mongod and Lab: Configuration File.

My guess is you are in the mongo shell when you ran the createUser command. Make sure you are in the admin database when before trying to run the command. Once auth is enabled, MongoDB will allow you to connect to localhost and then create a single user in the admin database. This is referred to as localhost exception.

Once there is a user in the system, you will need to authenticate with credentials that have the rights to do the tasks you are trying to accomplish.

1 Like

Hi Doug,
thank you for your reply.
In the previous post I mentioned that I am trying pass through “Deploy Replica Set” lab.
I suppose I managed to initiate the replica set while being in the localhost exception stage.
I could see I got the PRIMARY prompt in the mongo shell.
Then I tried to create a root user in order to proceed further according to the proposed lab steps, but I got that error of not being authorized to do so.
Well, just having written this, I realize I possibly forgot to issue “use admin” command. I am almost certain this is the case.
Thank you and best regards !

1 Like

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