尝试使用pymongo时连接被拒绝

时间:2015-01-11 16:55:02

标签: python pymongo bottle

我在我的瓶子virtualenv中安装了pymongo。我试图使用它,但得到以下错误:

connection = Connection()
---------------------------------------------------------------------------
ConnectionFailure                         Traceback (most recent call last)
<ipython-input-9-609d5cb538f5> in <module>()
----> 1 connection = Connection()

/home/python/BENV/local/lib/python2.7/site-packages/pymongo/connection.pyc in __init__(self, host, port, max_pool_size, network_timeout, document_class, tz_aware, _connect, **kwargs)
    234 
    235         super(Connection, self).__init__(host, port,
--> 236                 max_pool_size, document_class, tz_aware, _connect, **kwargs)
    237 
    238     def __repr__(self):

/home/python/BENV/local/lib/python2.7/site-packages/pymongo/mongo_client.pyc in __init__(self, host, port, max_pool_size, document_class, tz_aware, _connect, **kwargs)
    367             except AutoReconnect, e:
    368                 # ConnectionFailure makes more sense here than AutoReconnect
--> 369                 raise ConnectionFailure(str(e))
    370 
    371         if username:

ConnectionFailure: [Errno 111] Connection refused

为什么我会收到此错误,如何解决?

1 个答案:

答案 0 :(得分:1)

如果没有指定要连接的主机和端口,就像通过向Connection传递任何内容一样 connection = Connection()驱动程序尝试连接到localhost上的端口27017。

由于它失败了,它强烈暗示你没有在同一台机器上运行mongod进程pymongo正在侦听端口27017。

如果您已经有要连接的MongoDB服务器,则必须指定其位置 - 传递给Connection的主机和端口。