Purpose: To show you how to create User and Database Administrator accounts and enable authentication on your MOngoDB server.

For create Administrator account

Step:1 Create file name :add_admin_account.js

mongo=new Mongo(“localhost”);
db=mongo.getDB(“admin”);
db.addUser({
            user:”dbadmin”,
            pwd:”test”,
            roles:[“readWriteAnyDatabase”,
                    “dbAdminAnyDatabase”,
                    “clusterAdmin”]
            });
db.addUser({
            user:”useradmin”,
            pwd:”test”,
            roles:[“userAdminAnyDatabase”]
        })

ice_screenshot_20151210-123033

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Now allow only authenticate user,

ice_screenshot_20151210-125623

Create file with name : mongod_config_auth.txt

verbose=true
port=27017
dbpath=I:\mongo\
auth=true

ice_screenshot_20151210-134501

After config the file write below command in shell

ice_screenshot_20151210-134656

It will show error msg which indicate you have not permission to access user detail.

Now we add authentication using username and password using

db.auth(“<username>”,”<password>”)

ice_screenshot_20151210-134849

 

 

 

Advertisements