tajidyakub
2/13/2018 - 2:12 PM

Install MongoDB, enable access control, create Admin User and Database user for MongoDB in Ubuntu

Install MongoDB, enable access control, create Admin User and Database user for MongoDB in Ubuntu

MongoDB Kickstart in Ubuntu

Install and enable access control, create Admin and database User via MongoDB Shell

Installation and Enable Access Control

Use apt-get install for installation

$ apt-get install mongodb
$ systemctl status mongodb

Access control can be enabled via configuration file which default in /etc/mongodb.conf or via parameters when starting mongodb service --auth. Before enable access control create an Admin User first, this admin have the privileges to create user and give access to any database in the server.

Create Admin User via Shell

Connect to MongoDB service via mongo shell; and create user in database admin.

$ mongo
> use admin
> db.createUser({user:"Admin", pwd:"PasswordAdmin", roles:[{role:"userAdminAnyDatabase", db:"admin"}]})

Role userAdminAnyDatabase is a built-in role for managing users in the server. After creating the Admin user change the configuration, restart the service and reconnect.

Create Database User

$ vim /etc/mongodb.conf
# Comment out auth=true, by default configuration use auth=false
$ systemctl restart mongodb

Reconnect to the server and authenticate to create other user using db.createUser and different role.

$ mongo
> db.auth("Admin","PasswordAdmin")
> db.createUser({user:"DbUser", password:"DbAdminPass", roles:[{role: "readWrite", db:"db_name"}]})