Hexycode
Hexycode

Reputation: 458

How to connect VPS to MongoDB Compass

I bought a VPS and I want to put MongoDB server there. I connect to VPS with ssh tunnel, install MongoDB and start it. The question is, that how can I add VPS server to MongoDB Compass? Is it even possible?

I was trying to connect via hostname but I receive this error - connect ECONNREFUSED my.ip.adress.here Also was trying to connect via ssh tunnel in MongoDB Compass - doesn't work for me.

Maybe there is a problem with port?

I can connect to my VPS from terminal with ssh.

Edit1: I was using this documentation to install MongoDB on ubuntu and I'm using MongoDB 4.2.8

EDIT2: When I install mongodb on VPS, MongoDB ip is 127.0.0.1 how can I change it? I want it to set it for VPS ip address. If it's possible, otherwise I don't understand how I can connect to my DB on VPS.

Sorry if it's lame questions, just first time using VPS and it's pretty complex to me.

Upvotes: 3

Views: 3799

Answers (1)

Rex. A
Rex. A

Reputation: 499

Before you can connect to vps with mongodb compass, you need to allow remote connections to mongodb from your vps.

  1. It is recommended to secure your mongodb connection with a username and password

  2. Edit the mongod.conf file located at

sudo nano /etc/mongod.conf

  1. Enter a comma and append the IP address of the vps to the end of the bindIp field. This will allow remote connections to the mongodb instance

  2. Save the file and restart the mongodb connection

sudo systemctl restart mongod

  1. Setup the firewall to allow connections to port 27017 (the default mongodb port) from anywhere. (Please note that will expose your mongo database to the internet. Therefore it is recommended to close this port when shipping to production)

sudo ufw allow 27017

That's it for the vps.

  1. You can now open up mongodb compass
  2. Choose "fill in connection fields individually"
  3. Fill in the hostname with the IP address of your vps, port is the port which you opened previously 27017 in this case and select authentication by username and password.
  4. Enter the username and password of the mongodb admin which you should have created already.
  5. Choose the authentication database (usually admin)

That's it. You should be connected. You can now view the collections and fields of your mongodb instance on your remote machine

Upvotes: 9

Related Questions