0

I have a web app written in Django with mongoengine in a Digital Ocean droplet. During the developing step I used a sample of the main database that is located in a remote enviroment. But now I want to connect the Django web app to the main MongoDB database. How can I do this? With an ssh tunnel (I already use it between both environments)? If so, how do I do such connection? I know how to establish an ssh tunnel with pymongo:

from sshtunnel import SSHTunnelForwarder
import pymongo
server  =  SSHTunnelForwarder(
(remote_ip_address, 22),
ssh_private_key="/home/username/.ssh/id_rsa",
ssh_username="username",
remote_bind_address=('127.0.0.1', 27017),
)
server.start()
client = pymongo.MongoClient('127.0.0.1', server.local_bind_port)

But how do I do the equivalent connection with the Django web app? At the present moment I have the following definitions in the settings.py file.

# MongoDB settings 
MONGODB_DATABASES = {'default': {'name':'dbname'} } 
DATABASES = {'default': {'ENGINE': 'django.db.backends.dummy'} }
Miguel
  • 2,738
  • 3
  • 35
  • 51
  • You should not need to create an SSH tunnel. If you do, then that should really be set up on the infrastructure level and not within the application itself. You probably really just want to connect to the remote server on an externally visible address, and preferably with TLS enabled. So in short, just add the external address ( IP/DNS ) and specified connection options from the provider. – Neil Lunn Apr 29 '18 at 01:35
  • Sorry but I don't know how to do that. I think the remote server (where the main DB is) don't have a DNS server. It is just another machine to which I login with ssh. – Miguel Apr 29 '18 at 01:41
  • This is really something for your SysAdmin/DevOps person to sort out. You need "some sort" of additional access to simply being able to ssh into a box. There should be an open port for the database, and their should be correct TLS security set up on the database as well. Again if that person really wants to stick with an SSH Tunnel, then get them to set that up between the database server and the application server. You simply don't code this into an application, short of making a "utility" program to "administer databases", which I don't think you are actually doing. – Neil Lunn Apr 29 '18 at 01:44

0 Answers0