4

I'm trying to create a database with user name and password, some answers says to addUser for admin .but there isn't any database called in by mongodb.

I run the mongodb with super user privilege.

MacBook:~ root# mongod

or

 MacBook:~ macbook$ mongo

the in new tab I run following command

MacBook:~ root# mongo

then I see for my dbs with show dbs command it show followings

>show dbs
clist  0.078GB
hinfo  0.078GB
local  0.078GB

I tried with this local database with system.indexes but I didn't get any succesful result. am I wrong with doing this. hope your help. I'm new to mongodb and I'm using monogo 3.2.4 versioin

2 Answers 2

15

Since it's the first Stack Overflow link from "mongodb create db with username and password" query in Google, here's the answer:

use mydb
db.createUser(
   {
     user: "myuser",
     pwd: "mypassword",
     roles: [ "dbOwner" ]
   }
)

These commands will create a database named mydb, assign a user named myuser with password mypassword and grant him full access rights on that db.

Make sure you are admin beforehand, so that you're able to create dbs and users.

[Note] In order to get the newly created database to show in the list returned by command show dbs, you need to insert at least one document into a collection of the newly created database. For example:

> mydoc = { firstName : "Adam" }
> db.mycollection.insert( mydoc  );
Sign up to request clarification or add additional context in comments.

Comments

0

Authentication in MongoDB is done at the instance level. What that means is that when you start mongod you will need to pass the --auth option.

When authentication is enabled you will need to create users to give them access to the appropriate database(s). The admin database won't exist by default, it will be created dynamically when you create the first user.

See the Enable Authentication page for details and examples.

3 Comments

isn't it possible now
Sorry, revised my answer for clarity.
I created a user as docs.mongodb.com/manual/tutorial/enable-authentication this tutorial. It successfully created an admin user. but with or without login with username and password, I can access any database.is this apply for new databases

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.