How To Require Password Authentication In MongoDB

To enable and require username and password authentication in a self-hosted MongoDB installation, you would enable authorization in the ‘mongod.conf’ file as shown below (so you can use either the Mongo connection URI or CLI to log in). Locate the file and open it in a text editor (Nano is used for this example):

sudo nano /etc/mongod.conf

The next step is to find the ‘#security’ line in that file and uncomment it, add the ‘enabled’ flag to it as shown below:

security.authorization: enabled