How to enable auth
tip
Enable auth need restart mongo
Requirement to enable auth
- Update
securitysection in mongod.conf
security:
authorization: enabled
Add user to db
use <db> → db.createUser(...)That's all, restart mongo
db.adminCommand( { shutdown: 1 } )
For replica set
If you enable auth in replica set, make sure create a key by commands below and also add it to configuration file
openssl rand -base64 756 > <path-to-keyfile>
chmod 400 <path-to-keyfile>
And in configuration mongod.conf file:
security:
authorization: enabled
keyFile: <path-to-file>