Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Superuser in mongo

I'm trying to create a user in mongo who can do anything in any db.

According to the guide I created a new admin: http://docs.mongodb.org/manual/tutorial/add-user-administrator

This is the code:

use admin db.addUser( { user: "try1",               pwd: "hello,               roles: [ "userAdminAnyDatabase" ] } ) 

Then I stopped mongo, enabled the auth and restarted mongo.

Then I tried to create a database with his user.

According with this guide: http://www.mkyong.com/mongodb/how-to-create-database-or-collection-in-mongodb/

use fragola  db.users.save( {username:"fragolino"} ) 

And I get this: "not authorized for insert on fragola.users"

Anyone can help me?

like image 491
Simo Avatar asked Mar 25 '14 14:03

Simo


People also ask

How do I create a user and grant privileges in MongoDB?

A role in MongoDB grants privileges to perform some set of operations on a given resource. In MongoDB, users are created using createUser() method. This method creates a new user for the database, if the specified user is already present in the database then this method will return an error.

What is root role in MongoDB?

Admin vs Root: The role userAdminAnyDatabase in MongoDB gives ability to create users and assign roles to them, but by itself it doesn't allow the user to do anything else. The superuser role in MongoDB is the root.

How do I give access to Mongo?

MongoDB does not enable access control by default. You can enable authorization using the --auth or the security. authorization setting. Enabling internal authentication also enables client authorization.


1 Answers

from docs.mongodb.org-superuser-roles

Lets write answer that looks simple & also simple to implement

Steps :

1 : sudo apt-get install mongodb-org - in new terminal

2 : sudo mongod --port 27017 --dbpath /var/lib/mongodb

3 : mongo --port 27017 - in new terminal

4 : use admin

5 : As @drmirror said a user should have all 4 roles to be superuser

For Mongo Version 2.

db.createUser( {     user: "tom",     pwd: "jerry",     roles: [               { role: "userAdminAnyDatabase", db: "admin" },               { role: "readWriteAnyDatabase", db: "admin" },               { role: "dbAdminAnyDatabase", db: "admin" },               { role: "clusterAdmin", db: "admin" }            ] }) 

For Mongo Version 3.

db.createUser(    {        user: "tom",         pwd: "jerry",         roles:["root"]    }) 

6 : sudo /etc/init.d/mongod stop OR sudo service mongod stop - in new terminal

7 : sudo /etc/init.d/mongod start OR sudo service mongod start

8 : restart your pc

9 : sudo mongod --auth --port 27017 --dbpath /var/lib/mongodb - in new terminal

10: mongo --port 27017 -u "tom" -p "jerry" --authenticationDatabase "admin" - in new terminal

Note : step 10 is most important step .

it will give Output on terminal like

MongoDB shell version: 2.6.11 connecting to: 127.0.0.1:27017/test > 
like image 177
vijay Avatar answered Sep 19 '22 10:09

vijay