20

When I use MongoChef to connect remote mongo database, I use next parameters:


Server

  • Server: localhost
  • Port: 27017

SSH Tunnel

  • SSH address: 10.1.0.90

  • Port: 25

  • SSH Username: username

  • SSH Password: password


When I connect with Pymongo, I have the next code:

import pymongo

MONGO_HOST = "10.1.0.90"
MONGO_PORT = 25
MONGO_DB = "db_name"
MONGO_USER = "username"
MONGO_PASS = "password"

con = pymongo.MongoClient(MONGO_HOST, MONGO_PORT)
db = con[MONGO_DB]
db.authenticate(MONGO_USER, MONGO_PASS)

print(db)

But I have the next error:

pymongo.errors.ServerSelectionTimeoutError: 10.1.2.84:27017: [Errno 111] Connection refused

Please, could you help me with this problem? What did I do wrong?

Vini
  • 8,299
  • 11
  • 37
  • 49
Volodymyr Nazarenko
  • 902
  • 2
  • 7
  • 17
  • Connect SSH > LOOPBACK = "localhost(127.0.0.1)" unable connect mongodb if restricted all external IP access. – dsgdfg Mar 10 '17 at 13:57
  • So connect via SSH and call `python IDLE` apply your connection commands and grab output(connect to localhost). – dsgdfg Mar 10 '17 at 13:59
  • Thank you for your help! – Volodymyr Nazarenko Mar 13 '17 at 12:06
  • Ideally you could avoid to use ssh by making the MongoDB avaible outside the server too. Here there is a way to do that: http://incredulosanonimos.blogspot.co.uk/2018/04/making-mongodb-remotely-available.html – Rafael Valero Apr 04 '18 at 17:04

3 Answers3

43

The solution which works for me.

from sshtunnel import SSHTunnelForwarder
import pymongo
import pprint

MONGO_HOST = "REMOTE_IP_ADDRESS"
MONGO_DB = "DATABASE_NAME"
MONGO_USER = "LOGIN"
MONGO_PASS = "PASSWORD"

server = SSHTunnelForwarder(
    MONGO_HOST,
    ssh_username=MONGO_USER,
    ssh_password=MONGO_PASS,
    remote_bind_address=('127.0.0.1', 27017)
)

server.start()

client = pymongo.MongoClient('127.0.0.1', server.local_bind_port) # server.local_bind_port is assigned local port
db = client[MONGO_DB]
pprint.pprint(db.collection_names())

server.stop()
Volodymyr Nazarenko
  • 902
  • 2
  • 7
  • 17
  • Hi @Володимир Назаренко, while using the above code I am able to connect to the remote mongodb where the local host machine does not have a mongodb. If the local host machine (from where the code is executed) has a mongodb, the scripts connect to the local mongodb & not the remote one. Any thoughts? – DD1 Mar 26 '19 at 07:04
  • Hi @DebashishDas, Could you show your code, please? – Volodymyr Nazarenko Mar 27 '19 at 09:02
  • Hi @Володимир Назаренко, i was not using MongoClient('127.0.0.1', server.local_bind_port). Instead I was using MongoClient('127.0.0.1:27018'). My bad. – DD1 Mar 28 '19 at 11:37
0

Or, you could just pip install ssh-pymongo:

from ssh_pymongo import MongoSession

session = MongoSession('10.1.0.90',
    port=25,
    user='USERNAME',
    password='PASSWORD',
    uri='mongodb://127.0.0.1:27017'
)

db = session.connection['DATABASE_NAME']

print(db)

session.stop()

More examples from the package:

Example 1 -- If your username is same, and you've configured ssh keys.

from ssh_pymongo import MongoSession

session = MongoSession('db.example.com')

db = session.connection['db-name']

Example 2 -- If you have some custom local authentication.

from ssh_pymongo import MongoSession

session = MongoSession('db.example.com',
    uri='mongodb://user:password@127.0.0.1/?authSource=admin&authMechanism=SCRAM-SHA-256')

db = session.connection['db-name']

Example 3 -- If you want to use different ports and keys.

from ssh_pymongo import MongoSession

session = MongoSession(
    host='db.example.com',
    port=21,
    user='myuser',
    key='/home/myplace/.ssh/id_rsa2'
)

db = session.connection['db-name']
Mindey I.
  • 29
  • 1
  • 6
-2

This helped me to connect pymongo with mLab database in python :

from pymongo import MongoClient

MONGO_HOST = "ds123456.mlab.com"
MONGO_PORT = 23456
MONGO_DB = "db name"
MONGO_USER = "Username"
MONGO_PASS = "password"
connection = MongoClient(MONGO_HOST, MONGO_PORT)
db = connection[MONGO_DB]
db.authenticate(MONGO_USER, MONGO_PASS)
Kshitij
  • 339
  • 1
  • 10