First SSH into your server and enter the mongo shell by typing MONGO For this example, I will set up a user named ian and give that user read & write access to the cool_db database.
use cool_db
db.createUser({
user: 'ian',
pwd: 'secretPassword',
roles: [{ role: 'readWrite', db:'cool_db'}]
})Enable auth and open MongoDB access up to all IPs
Edit your MongoDB config file
sudo vim /etc/mongod.conf
Look for the net line and comment out the bindIp line under it, which is currently limiting MongoDB connections to localhost:
# network interfacesnet: port: 27017# bindIp: 127.0.0.1 <- comment out this line
Scroll down to the #security: section and add the following line. Make sure to un-comment the security: line.
security: authorization: 'enabled'
after open port 2017 and restart mongo daemon.
profit )))