18
votes

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?

3
Connect SSH > LOOPBACK = "localhost(127.0.0.1)" unable connect mongodb if restricted all external IP access. - dsgdfg
So connect via SSH and call python IDLE apply your connection commands and grab output(connect to localhost). - dsgdfg
Thank you for your help! - Volodymyr Nazarenko
Ideally you could avoid to use ssh by making the MongoDB avaible outside the server too. Here there is a way to do that: incredulosanonimos.blogspot.co.uk/2018/04/… - Rafael Valero

3 Answers

35
votes

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()
1
votes

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:[email protected]/?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']
-1
votes

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)