Scenario for me was to build an RPM that sets up the mongoDB and starts authenticated for the application to use appropriately. Solution I came up to start MongoDB by modifying the MongoDB init file. The following example shows how to create a root and multiple app users (with owner / write / readonly access rights).

Create two configuration files, one with security: authorization: enabled and the other without it

/etc/mongodb/auth.conf

/etc/mongodb/noauth.conf

security: authorization: enabled

Create script to add root user to DB

create_user_root.js

(optional) Create script to add application users to DB

create_user_app.js

Update init configuration for MongoDB as below:

* Line 9: config file location with security enabled

* Line 10: config file location with security disabled

* Line: 11: script location for creating root

* Line 12: script location for creating app users (my_app) — optional

* Line 14: mongo command required for adding users

* Line 21: start MongoDB without auth

* Line 23: Add root user and delete script file to prevent rerun

* Line 28: Add app users and delete script file to prevent rerun

* Line 34: Sleep to give enough time for restart

* Line 35: Stop Service

* Line 39 Start Service