Configuring MongoDB
Check if mongodb is running
mongod --version
Making user for mongodb
sudo nano /etc/mongod.conf
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
To bind to all IPv4 and IPv6 address you’ll set:
bindIp: 0.0.0.0
To save the file press CTRL + X
then press Y
and then press ENTER
Restart mongodb
sudo systemctl restart mongod
Login to mongodb shell
mongosh
Once your in mongodb shell type this command
> use admin
db.createUser({user:"ararat", pwd:"passwordhere", roles:[{role:"root", db:"admin"}]})
Make sure to change the password to something more secure
exit
Next we need to edit the service file for mongodb
sudo nano /lib/systemd/system/mongod.service
Add this line into this file
ExecStart=/usr/bin/mongod --quiet --auth --config /etc/mongod.conf
sudo systemctl daemon-reload && sudo systemctl restart mongod
Check if you can login to mongodb shell with the user you created
mongosh -u ararat -p --authenticationDatabase admin
db.runCommand({connectionStatus : 1})
Now you got fully working mongodb server with user and password