3

I am trying to create user for my mongo database.

Using MongoDB Query: db.createUser({user: 'administrator', pwd: '1234567890', roles: ['root']})

2019-01-29T11:31:32.521+0530 E QUERY    [js] Error: couldn't add user: No role named root@test :

mongod.config

there is no file in /etc/mongod.config

Vijay Prajapati
  • 208
  • 1
  • 5
  • 19

3 Answers3

7

Try the following:

db.createUser({ user:"userName", 
                pwd:"passWord", 
                roles:[ { role:"readWrite", 
                          db:"dataBaseName" 
                        } ], 
                mechanisms:[ "SCRAM-SHA-1"] 
              })

Learn more about mechanisms:[ "SCRAM-SHA-1"] at https://docs.mongodb.com/manual/core/security-scram/

floss
  • 2,603
  • 2
  • 20
  • 37
6

I had the same issue. You have to switch to a database on your mongo server that actually has the role root because it's saying that the database test, which is selected when you open a MongoDB shell, doesn't have the role root. just try use admin to switch to the admin database and then try db.createUser({ user: "mongoadmin" , pwd: "mongoadmin", roles: ["root"]}), like you posted in the question. That solved my issue.

Dharman
  • 30,962
  • 25
  • 85
  • 135
StevenBoyce
  • 421
  • 5
  • 12
  • 1
    You can see this more by starting your mongo shell and typing `show roles`. That lists all of the roles available on your currently selected database instance. – StevenBoyce Dec 12 '20 at 14:49
0

Instead of "root" provide readWrite access to the user:

db.createUser({user: 'userName',pwd: 'somePassword',roles: [{  role: 'readWrite',  db: 'databaseName'}]})
KIRAN KUMAR B
  • 404
  • 4
  • 8