Install MongoDB on VPS<!-- --> | Blog | Tuhin

Install MongoDB on VPS

📅 12th February 2021 🧔 Tuhin Kanti Pal

Install MongoDB on VPS

In this Blog I'm gonna show you how can you install MongoDB on Your server. Some times free mongodb atlas becone slow that's why we are going to install mongodb on a vps. Firstly i created a vps and expose its 27017 and 22 port. Then I connected it with a ssh client.

Install MongoDB

apt install wget -y
wget -qO - | apt-key add -
apt-get install gnupg -y
wget -qO - | apt-key add -
echo "deb buster/mongodb-org/4.4 main" | tee /etc/apt/sources.list.d/mongodb-org-4.4.list
apt-get update
apt-get install -y mongodb-org
systemctl start mongod
systemctl daemon-reload
systemctl enable mongod

Now let's create an admin and an user

use admin

1. Create an admin

db.createUser({user: "admin", pwd: "adminpassword", roles: [{role: "root", db: "admin"}]})

2. Create an normal user with read write permission

db.createUser({user: "user", pwd: "userpassword", roles: [{role: "readWriteAnyDatabase", db: "admin"}]})

Type exit and hit enter to exit from MongoShell

BindIP For Remote Connection & Enable authentication

You have to edit a configuration file to bind the ip so that you can use it remotely.

nano /etc/mongod.conf

Edit the configuration file like this:

  port: 27017
  authorization: enabled

Restart the server:

systemctl restart mongod


Now everything is ready to go. Connect MongoDB with your application like this