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 :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype.createUser@src/mongo/shell/db.js:1386:11
@(shell):1:1

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

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

Thanks!

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 :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype.getUsers@src/mongo/shell/db.js:1659:15
shellHelper.show@src/mongo/shell/utils.js:914:9
shellHelper@src/mongo/shell/utils.js:819:15
@(shellhelp2):1:1

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

Thanks
Pavel

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.

Regards,
Stennie