From your post, I assume you already have active users on your MongoDB installation,correct?
Let’s assume that’s correct, I think you have enabled it auth as well but let’s say you haven’t just for the sake of the answer.
Enable MongoDB Auth
Open your conf
and add the following lines
This will tell mongodb that whenever it starts up next, it needs to enforce database access control using the roles you have already configured.
By default mongodb is configured to allow connections only from localhost. We need to allow remote connections. In the same config file, go to the network interfaces section and change the bindIp from 127.0.0.1 to 0.0.0.0 which means allow connections from all ip addresses.
# network interfaces
bindIp: 0.0.0.0 #default value is 127.0.0.1
Now save, exit the file and restart your MongoDB.
Open up network port
MongoDB uses port number 27017 for all connections by default. So let’s open up that port
ubuntu:~$ sudo iptables -A INPUT -p tcp --destination-port 27017 -m state --state NEW,ESTABLISHED -j ACCEPT
ubuntu:~$ sudo iptables -A OUTPUT -p tcp --source-port 27017 -m state --state ESTABLISHED -j ACCEPT
Test the remote connections
Test the connection like
mongo -u user1 -p user1password <your_server_ip>/sampledb