Couldn't add user: not authorized on admin to execute

Not able to execute createUser() command in mongo. Showing error
“couldn’t add user: not authorized on admin to execute”…need help

Hi @Rohan_82787,

Check if you have already create this user. Try authenticating with the user using:

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

If it outputs 1, means the user is already there.

If it shows 0 and you are unable to create the user m103-admin , then stop mongod and run mongod without authentication. Comment the authentication line in config file if you are using configuration file.

Let me know if it helps!

Thanks,
Muskan
Curriculum Support Engineer

1 Like

Hi Rohan_82787,

I got a similar error - in my case I forgot to switch to admin db priror to creating the user:

m103-repl:PRIMARY> use admin
switched to db admin

m103-repl:PRIMARY> db.createUser({
… user: “m103-admin”,
… pwd: “m103-pass”,
… roles: [
… {role: “root”, db: “admin”}
… ]
… })
Successfully added user: {
“user” : “m103-admin”,
“roles” : [
{
“role” : “root”,
“db” : “admin”
}
]
}
m103-repl:PRIMARY> use admin
switched to db admin
m103-repl:PRIMARY>
m103-repl:PRIMARY> db.auth(“m103-admin”,“m103-pass”)
1
m103-repl:PRIMARY>

This thread is 2 years old.

I am sure Rohan_82787 is not stuck on this issue anymore.

Right - shared for benefit of others as well.