连接似乎在服务器端接受,但在我的python脚本中似乎被拒绝

时间:2012-07-16 19:50:42

标签: python mongodb pymongo

我正在运行一个python脚本,将文档添加到远程mongodb数据库的集合中。该文件如下:

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()

当我运行这个脚本时,我有一些有趣的行为。在服务器日志中,似乎接受连接,运行查询,然后关闭连接。

# 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

然而,这是我从python脚本得到的:

# 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

一些有用的信息: 远程服务器是在OpenVZ上运行的VPS。我知道有与count()和迭代器相关的兼容性问题,但我(假设)通过使用--smallfiles选项修复它们,并将我的nssize限制为100.我的防火墙两侧都完全打开。

我还注意到,在错误中,即使我指定了不同的IP地址,autoreconnect也会尝试连接到localhost。

1 个答案:

答案 0 :(得分:0)

您需要传递IP - conn = Connection(ip)