Created
          November 19, 2017 16:39 
        
      - 
      
- 
        Save JinhaiZ/3ad536870b9853dbff11ab4241380c0d to your computer and use it in GitHub Desktop. 
    connect to MongoDB via ssh tunnel
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | from sshtunnel import SSHTunnelForwarder | |
| import pymongo | |
| MONGO_HOST = "IP_ADDRESS" | |
| MONGO_USER = "USERNAME" | |
| MONGO_PASS = "PASSWORD" | |
| MONGO_DB = "DATABASE_NAME" | |
| MONGO_COLLECTION = "COLLECTION_NAME" | |
| # define ssh tunnel | |
| server = SSHTunnelForwarder( | |
| MONGO_HOST, | |
| ssh_username=MONGO_USER, | |
| ssh_password=MONGO_PASS, | |
| remote_bind_address=('127.0.0.1', 27017) | |
| ) | |
| # start ssh tunnel | |
| server.start() | |
| connection = pymongo.MongoClient('127.0.0.1', 27017) | |
| db = connection[MONGO_DB] | |
| collection = db[MONGO_COLLECTION] | |
| # close ssh tunnel | |
| server.stop() | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
how can resolve mongo DB connection issues