Uploaded image for project: 'Python Driver'
  1. Python Driver
  2. PYTHON-1503

Timeout when accessing to mongoDB from pymongo with tunneling

    • Type: Icon: Task Task
    • Resolution: Cannot Reproduce
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 3.6.1
    • Component/s: Connection Mgmt
    • None
    • Environment:
      In the description

      Hi.
      This question is copied from stckoverflow:

      https://stackoverflow.com/questions/49295194/timeout-when-accessing-to-mongodb-from-pymongo-with-tunneling

      I'm trying to access mongoDB cluster using tunneling. the setup is:

      my machine => jump machine => mongod/mongos...

      I found this answer: https://stackoverflow.com/a/42763361, but I keep getting timeout from mongodb.

      What I tried so far:

      • breakpoint after the tunneling code and before the pymongo code; then access mongo from mongo shell - that worked (so the tunneling is working)
      • connect to: mongos, mongod primary, mongod secondary, remove the read preferences and so on.
      • tried the code with several different environments with similar settings
      • play with the remote bind address (hostname / IP)
      • play with the local bind address ('0.0.0.0' / '127.0.0.1' / 'localhost')
        Any time I'm trying to actually query mongo (e.g. col.count(), col.find_one() etc.), I'm getting timeout.

      the code is:

      #! /usr/bin/python
      from pymongo import MongoClient, ReadPreference
      from sshtunnel import SSHTunnelForwarder
      import paramiko
      
      SSH_KEY_LOCATION = '...'
      JUMP_MACHINE_ADDRESS = '...'
      SSH_USER = '...'
      REMOTE_MONGO_ADDRESS = '...'
      
      DB_NAME = '...'
      COLLECTION_NAME = '...'
      
      pkey = paramiko.RSAKey.from_private_key_file(SSH_KEY_LOCATION)
      server = SSHTunnelForwarder(
          (JUMP_MACHINE_ADDRESS),
          ssh_username=SSH_USER,
          ssh_private_key=pkey,
          remote_bind_address=(REMOTE_MONGO_ADDRESS, 27017),
          local_bind_address=('127.0.0.1', 27017)
      )
      server.start()
      
      client = MongoClient('mongodb://127.0.0.1:27027', readPreference=ReadPreference.SECONDARY)
      db = client[DB_NAME]
      col = db[COLLECTION_NAME]
      
      count = col.count({})
      
      print(count)
      
      server.stop()
      

      The error I'm receiving is:

        File "/Library/Python/2.7/site-packages/pymongo/collection.py", line 1559, in count
          return self._count(cmd, collation, session)
        File "/Library/Python/2.7/site-packages/pymongo/collection.py", line 1503, in _count
          with self._socket_for_reads() as (sock_info, slave_ok):
        File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py", line 17, in __enter__
          return self.gen.next()
        File "/Library/Python/2.7/site-packages/pymongo/mongo_client.py", line 979, in _socket_for_reads
          server = topology.select_server(read_preference)
        File "/Library/Python/2.7/site-packages/pymongo/topology.py", line 224, in select_server
          address))
        File "/Library/Python/2.7/site-packages/pymongo/topology.py", line 183, in select_servers
          selector, server_timeout, address)
        File "/Library/Python/2.7/site-packages/pymongo/topology.py", line 199, in _select_servers_loop
          self._error_message(selector))
      pymongo.errors.ServerSelectionTimeoutError: 127.0.0.1:27027: [Errno 61] Connection refused
      

      Thank you for your help!

            Assignee:
            shane.harvey@mongodb.com Shane Harvey
            Reporter:
            nunatsam@cisco.com Nahshon Unna-Tsameret
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: