Tuesday, September 15, 2020

How to Enable Access Control in MongoDB

  No comments

 After you install mongodb and get the warning to enable access control. You can follow this step

Enable Access Control

1. Start MongoDB without access control. (No need, if service already running)

mongod --port 27017

2. Connect to the instance

mongo --port 27017

3. Create the user administrator

> use admin

switched to db admin

> db.createUser(

...   {

...     user: "superman",

...     pwd: "rahasia",

...     roles: [ { role: "root", db: "admin" } ]

...   })

4. Re-start the MongoDB instance with access control

sudo service mongod restart   

5. Add the security.authorization setting to the config file

sudo vi /etc/mongod.conf 

systemLog:
destination: file
path: /usr/local/var/log/mongodb/mongo.log
logAppend: true
storage:
dbPath: /usr/local/var/mongodb
net:
bindIp: 127.0.0.1
security:
authorization: enabled

6. Restart mongodb 

sudo service mongod restart

7. Connect to database

mongo --port 27017 -u "superman" -p "rahasia" --authenticationDatabase "admin"

now the warning has disappeared

No comments :

Post a Comment