Follow the guide with MongoDb site
https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/
use databaseName
db.createUser({
user: 'username',
pwd: 'secretPassword',
roles: [{ role: 'readWrite', db:'databaseName'}]
})
sudo nano /etc/mongod.conf
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0
security:
authorization: 'enabled'
sudo iptables -A INPUT -p tcp --destination-port 27017 -m state --state NEW,ESTABLISHED -j ACCEPT
sudo iptables -A OUTPUT -p tcp --source-port 27017 -m state --state ESTABLISHED -j ACCEPT
sudo service mongod restart
Reference: