Can’t create a root user from mongo shell

I’m working on a server setup tutorial, but the error below is not resolved, so I’m asking a question.

When I try to create a user with db.createUser in the mongo shell, I keep getting an error like the one below.

> use admin;
switched to db admin
> db.createUser({user: "test", pwd: "test", roles:["root"]});
uncaught exception: Error: couldn't add user: command createUser requires authentication :

In the config, security is commented out like this:
# security:
# authorization: enabled

I’m using AWS EC2 instance which uses Ubuntu Server 18.04


Seeing that the auth error appears even when listing users, there seems to be a problem with auth, but I don’t know how to solve it.

> show users;
uncaught exception: Error: command usersInfo requires authentication :

Hi @111860 ,

Is the shell run on the same host as this process? How do you connect?

Have you restarted the server after commenting security section?

It seems that security is stilll enabled


After commenting security section, I restarted mongod and it worked fine!

The problem was that I didn’t restart the server.

Thank you so much. Actually, I almost quit studying programming after trying to solve this all day by myself. But actually it was no big deal…

Thank you so much and I hope you have a nice day!

1 Like

Welcome to the MongoDB Community Forums @111860!

If you enabled access control without creating any users, there is a Localhost Exception that allows you to login via localhost in order to create the first admin user (after which, the exception will no longer be available).

Alternatively you can restart mongod without enabling access control in order to create the first user, but that is a riskier approach I would not recommended. If you take the latter approach, don’t forget to restart your mongod with access control enabled after creating your initial admin user.