Configuring MongoDB

The default configuration settings are sufficient for most users. However for production environments it is recommend to uncomment the security section and enable authorization as shown bellow:

/etc/mongod.conf

security:authorization:enabled

The authorization option enables Role-Based Access Control (RBAC) that regulates users access to database resources and operations. If this option is disabled each user will have access to all databases and perform any action.

After making changes to the MongoDB configuration file, restart the mongod service with:

sudo systemctl restart mongod

To find more information about the configuration options available in MongoDB 4.0 visit the Configuration File Options documentation page.

Creating Administrative MongoDB User

If you enabled the MongoDB authentication, create an administrative MongoDB user that will be used to access and manage the MongoDB instance.

First access the mongo shell with:

mongo

Once you are inside the MongoDB shell type the following command to connect to the admin database:

useadmin

switched to db admin

Issue the following command to create a new user named mongoAdmin with the userAdminAnyDatabase role: