Adding/Creating Admin User

MongoDB Enterprise m103-example:PRIMARY> use admin
switched to db admin
MongoDB Enterprise m103-example:PRIMARY> rs.initiate()
{
“operationTime” : Timestamp(1563940560, 1),
“ok” : 0,
“errmsg” : “there are no users authenticated”,
“code” : 13,
“codeName” : “Unauthorized”,
“$clusterTime” : {
“clusterTime” : Timestamp(1563940560, 1),
“signature” : {
“hash” : BinData(0,“SrLHLVdCHM55Lvn5Bom4Hi/emQ8=”),
“keyId” : NumberLong(“6717050897840472066”)
}
}
}
MongoDB Enterprise m103-example:PRIMARY> use admin
switched to db admin
MongoDB Enterprise m103-example:PRIMARY> db.createUser({
… user: “m103-admin”,
… pwd: “m103-pass”,
… roles: [
… {role: “root”, db: “admin”}
… ]
… })
2019-07-24T03:59:54.712+0000 E QUERY [thread1] Error: couldn’t add user: there are no users authenticated :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype.createUser@src/mongo/shell/db.js:1437:15
@(shell):1:1
MongoDB Enterprise m103-example:PRIMARY>

Not sure what mistake I did. I just copied and pasted all from the course. but still not able to add admin user.
Tried disabling security but didn’t work.

Hey @jameroym

From the first part, it looks likteyour replica set was already initiated; you can tell this since the prompt says PRIMARY

And from the rest it seems you have already passed the localhost execption. Have you already created a user before? If not, perhaps you could shutdown these mongod processes and clean the directories and start the lab over again.

1 Like

@natac13, Thank you for the response.
I didn’t create USER for this exercise.
I did shutdown mongod processes and used Kill then started again but had the same problem.
When you said clean the directories, do you mean to clean rm -rf in /var/mongodb/db/node1 ?

Yep try to clear them all and see if you can start the process over.

If you’re getting this error that means the user was already added. I would log out of mongod and log back in with the newly created user m103-admin.

@natac13 @pawlowsg
It did work now. I guess user is already created.
Thank you both.