Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a database with a user name and password in mongodb in mongodb shell

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

like image 456
bill Avatar asked Nov 15 '25 01:11

bill


1 Answers

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  );
like image 186
yeasayer Avatar answered Nov 17 '25 19:11

yeasayer