Basic Security Part 2 Create new user with root

I spent so much time going round and round with this error:

2019-12-07T18:59:59.399+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:100:

I finally solved it, by

  1. killing all mongod instances from within the mongo shell with kill all after issuing a ps aux -ef | grep mongod command to confirm PIDs going dark
  2. I changed my mongod.conf file to port: “27001” a new port from the previous attempts
  3. I started mongo on port 27001 from the mongo shell (if that makes sense)
  4. I ussed the db.createUser command AND IT WORKED!

I forgot one other VERY IMPORTANT CHANGE TO THE mongod.conf file BEFORE i added a new user: I removed security enabled, and that may have also helped me add the root user.

It’s part of the requirement to have authentication enabled:

You encountered this error because you had exhausted the localhost exception.

Basically, if authentication is enabled, you have one try to create any user via the localhost. If you make a mistake (i.e. forget to switch to the admin db before creating the user, create a user that doesn’t have enough privileges to create other users, etc), you have to start from scratch by shutting down the server, deleting the sub directory that contains the db files and fire up mongod again.

And after you have created that user using the localhost exception, you must login using that user before you can perform any other actions.

Hi @Rufel_05530,

You are getting this error because you have authentication : enabled in your configuration file and you have already created a user using the localhost exception. Now you must authenticate against the first user before creating any other user or performing any other task in your database.

Localhost excpetion and other concepts are discussed in the Lecture - Basic Security: Part 2.

This is not how you do it. If it would have been part of a Lab, then you would have gotten an error. You basically created a new user with different credentials than the existing one by disabling the security.

Ideally this is what you should have done.

  • Connect and authenticate using the credentials of the previously created user.
  • And then create a new user.

Hope it helps!

If you have any other query then please feel free to get back to us.

Happy Learning :slight_smile:

Thanks,
Shubham Ranjan
Curriculum Support Engineer