21

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?

4
  • Connect SSH > LOOPBACK = "localhost(127.0.0.1)" unable connect mongodb if restricted all external IP access. Commented Mar 10, 2017 at 13:57
  • So connect via SSH and call python IDLE apply your connection commands and grab output(connect to localhost). Commented Mar 10, 2017 at 13:59
  • Thank you for your help! Commented Mar 13, 2017 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: incredulosanonimos.blogspot.co.uk/2018/04/… Commented Apr 4, 2018 at 17:04

3 Answers 3

46

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()
Sign up to request clarification or add additional context in comments.

3 Comments

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?
Hi @DebashishDas, Could you show your code, please?
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.
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:[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 Comment

This library just uses the same method as above.
-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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.