Connection appears accepted server-side but appears to be refused in my python script
Question: I have a python script I am running to add a document to a collection for a
remote mongodb database. The file is as follows:

    
    
    from pymongo import Connection
    ip    = 'x.x.x.x' #edited out
    
    conn  = Connection()
    db    = conn['netmon']
    
    users = db.users
    print 'number of users: ' + str(users.count())
    if users.count() == 0:
      print 'Please create a new account.'
      t_user = raw_input('Username:')
      users.insert({'username':unicode(t_user)})
    conn.close()
    

When I run this script, I have some interesting behavior. In the server logs,
it appears that the connection is accepted, the query is run, and then the
connection is closed.

    
    
    # Mongodb logs
    14:27:18 [initandlisten] connection accepted from 108.93.46.75:39558 #1
    14:27:18 [conn1] run command admin.$cmd { ismaster: 1 }
    14:27:18 [conn1] command admin.$cmd command: { ismaster: 1 } ntoreturn:1 reslen:71 0ms
    14:27:18 [conn1] run command netmon.$cmd { count: "users", fields: null, query: {} }
    14:27:18 [conn1] Accessing: netmon for the first time
    14:27:18 [conn1] command netmon.$cmd command: { count: "users", fields: null, query: {} } ntoreturn:1 reslen:58 10ms
    14:27:18 [conn1] end connection 108.93.46.75:39558
    

However, this is what I get from the python script:

    
    
    # Python error
    > python testmongo.py
    Traceback (most recent call last):
      File "testmongo.py", line 7, in <module>
        conn  = Connection()
      File "/usr/local/lib/python2.7/dist-packages/pymongo-2.2.1-py2.7-linux-x86_64.egg/pymongo/connection.py", line 290, in __init__
        self.__find_node()
      File "/usr/local/lib/python2.7/dist-packages/pymongo-2.2.1-py2.7-linux-x86_64.egg/pymongo/connection.py", line 586, in __find_node
        raise AutoReconnect(', '.join(errors))
    pymongo.errors.AutoReconnect: could not connect to localhost:27017: [Errno 111] Connection refused
    charles@charles-mbp:~/Desktop$ python testmongo.py
    Traceback (most recent call last):
      File "testmongo.py", line 7, in <module>
        conn  = Connection()
      File "/usr/local/lib/python2.7/dist-packages/pymongo-2.2.1-py2.7-linux-x86_64.egg/pymongo/connection.py", line 290, in __init__
        self.__find_node()
      File "/usr/local/lib/python2.7/dist-packages/pymongo-2.2.1-py2.7-linux-x86_64.egg/pymongo/connection.py", line 586, in __find_node
        raise AutoReconnect(', '.join(errors))
    pymongo.errors.AutoReconnect: could not connect to localhost:27017: [Errno 111] Connection refused
    charles@charles-mbp:~/Desktop$ python testmongo.py
    Traceback (most recent call last):
      File "testmongo.py", line 7, in <module>
        conn  = Connection()
      File "/usr/local/lib/python2.7/dist-packages/pymongo-2.2.1-py2.7-linux-x86_64.egg/pymongo/connection.py", line 290, in __init__
        self.__find_node()
      File "/usr/local/lib/python2.7/dist-packages/pymongo-2.2.1-py2.7-linux-x86_64.egg/pymongo/connection.py", line 586, in __find_node
        raise AutoReconnect(', '.join(errors))
    pymongo.errors.AutoReconnect: could not connect to localhost:27017: [Errno 111] Connection refused
    

Some useful information: The remote server is a VPS running on OpenVZ. I know
there are compatibility problems related to count() and iterators, but I
(assumingly) fixed them by using the --smallfiles option, as well as limiting
my nssize to 100. My firewall on both sides are completely open.

I've also noticed that in the errors, autoreconnect is trying to connect to
localhost, even though I specified a different IP address.


Answer: You need to pass the IP - `conn = Connection(ip)`

