Connecting to remote MongoDB server

Hi, I want to be able to edit / create and download data Mongo data in my remote Mongo instance running on Digital Ocean.
On my local machine I have installed Mongo DB Compass, is it possible to connect this to my remote Mongo Server ? I can access my server through SSH and I have Ubunto Server (not desktop) running.

If not could you suggest what options I have please?

Thanks

Hello @Jon_C :wave: welcome to the forum!

You should be able to connect to the db when your database is reachable over the internet. It will not be enough when you can access only the machine via ssh.
To connect you need at least to know the Domain/ IP, port authentication method and credentials on which your DB is running. The standard port will be 27017. In case the database is reachable via the internet, you should be able to connect to it via the mongo shell and / or Compass.

The mongodb documentation describes very good the connection options to a remote database or replication set.

I also might be an option to look into MongoDB Atlas you will get a free account for (I think) 512 MB, including lots of infrastructure benefits. So if there is no strong urge to run MongoDB on your server, Atlas might be the less stressful option.

Cheers,
Michael

Hi @Jon_C!

You can use ssh tunneling to connect to remote MongoDB instances. The Connect to MongoDB portion of the Compass documentation shows you how to do this, although you do have to click on a couple of tabs to find the information.

In the connect section you will see two tabs labeled Paste Connection String and Fill in Individual Fields. You want to click on the Fill in Individual Fields tab. Follow those directions and in the optional step 3, you will see two more tabs labeled Connect Using TLS/SSL and Connect Using SSH. Click on the Connect Using SSH tab and that will explain the information you need to set up SSH Tunneling.

Hi , I created a user with a role of read. I connect to the server with Putty and then I can connect to DB with this user. When I try Compass, I get the following error

Error creating SSH Tunnel: (SSH) Channel open failure: Connection refused

Any ideas on where I’m going wrong, thanks

Hi @Jon_C I can reproduce that error if I put in the host name of my server on the Hostname tab. My MongoDB instance is bound to only the localhost address bindIp: 127.0.0.1. Due to this I need to use a host name of localhost on the Hostname tab.

In the More Options tab you will provide the actual machine that you would ssh into.

Hopefully this takes care of your issue. If not please provide screen shots of the two tabs and your /etc/mongod.conf file as that might help to troubleshoot.

Hi, I changed bind_ip = 127.0.0.1 to bind_ip = 0.0.0.0, then in terminal, restarted mongoDB with command

systemctl restart mongodb

It didn’t seem to make any difference to my web application nor could I work with Compass (locally), so then I tried command below after following a blog.

service mongod restart

Which caused some errors, and my node application broke, at which point I rebooted the server and changed the config back to default values to reinstate the application :frowning, so back at square one.

Also, as I have node running on the server also which pulls data from Mongo, if I change the binding as mentioned above, would it break the app?

===========
# mongodb.conf

# Where to store the data.
dbpath=/var/lib/mongodb

#where to log
logpath=/var/log/mongodb/mongodb.log

logappend=true

bind_ip = 127.0.0.1
port = 27017

# Enable journaling, http://www.mongodb.org/display/DOCS/Journaling
journal=true

# Enables periodic logging of CPU utilization and I/O wait
#cpu = true

#--  uncommenting auth 
# Turn on/off security.  Off is currently the default
#noauth = true
#auth = true

# Verbose logging output.
#verbose = true

# Inspect all client data for validity on receipt (useful for
# developing drivers)
#objcheck = true

# Enable db quota management
#quota = true

# Set diagnostic logging level where n is
#   0=off (default)
#   1=W
#   2=R
#   3=both
#   7=W+some reads
#diaglog = 0

# Diagnostic/debugging option
#nocursors = true

# Ignore query hints
#nohints = true

# Disable the HTTP interface (Defaults to localhost:27018).
#nohttpinterface = true

# Turns off server-side scripting.  This will result in greatly limited
# functionality
#noscripting = true

# Turns off table scans.  Any query that would do a table scan fails.
#notablescan = true

# Disable data file preallocation.
#noprealloc = true

# Specify .ns file size for new databases.
# nssize = <size>

# Accout token for Mongo monitoring server.
#mms-token = <token>

# Server name for Mongo monitoring server.
#mms-name = <server-name>

# Ping interval for Mongo monitoring server.
#mms-interval = <seconds>

# Replication Options

# in replicated mongo databases, specify here whether this is a slave or master
#slave = true
#source = master.example.com
# Slave only: specify a single database to replicate
#only = master.example.com
# or
#master = true
#source = slave.example.com

# Address of a server to pair with.
#pairwith = <server:port>
# Address of arbiter server.
#arbiter = <server:port>
# Automatically resync if slave data is stale
#autoresync
# Custom size for replication operation log.
#oplogSize = <MB>
# Size limit for in-memory storage of op ids.
#opIdMem = <bytes>

# SSL options
# Enable SSL on normal ports
#sslOnNormalPorts = true
# SSL Key file and password
#sslPEMKeyFile = /etc/ssl/mongodb.pem
#sslPEMKeyPassword = pass

If your app and the mongod process are both running on the same server then the app should be able to access the database with a bind_ip config set to either 127.0.0.1 (localhost loopback adapter) or 0.0.0.0 (listen on all adapters). The change shouldn’t have broken the application’s connection.

The use of systemctl or service depends on the dstro you’re using. Most have gone to systemctl.

As for the problem with Compass connecting to the remote server, on the host page did you use localhost on the Hostname tab with the MongoDB user credentials for auth? On the More Options tab all the information for the SSH Tunnel will be the Digital Ocean information you use to SSH to the machine.