How to connect remote mongodb with pymongo

When I use MongoChef to connect to a remote Mongo database, I use the following options:


  • Server Server: localhost
  • Port: 27017

Ssh tunnel

  • SSH Address: 10.1.0.90

  • Port: 25

  • SSH username: username

  • SSH Password: Password


When I connect to Pymongo, I have the following 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 following error:

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

Please, could you help me with this problem? What have I done wrong?

+7
source share
2 answers

A solution that 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() 
+19
source

This helped me link pymongo to the 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) 
-5
source

Source: https://habr.com/ru/post/1265288/


All Articles