pymongo无法连接到远程服务器上的数据库

pymongo无法连接到远程服务器上的数据库

问题描述:

我已经设置了在外部主机服务器上运行的mongodb服务器,通过在客户端计算机外壳中执行以下操作,可以轻松连接mongo客户端:

I have setup a mongodb server running on an external host server, I can connect the mongo client easily by doing the following in the client computers shell:

162.243.27.34 --port 27017

但是当我尝试使用python的pymongo连接到服务器时,得到以下输出

but when I try to connect to the server using pymongo using python I get the following output

import pymongo
client = pymongo.MongoClient('162.243.27.34', 27017)

错误

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/pymongo/mongo_client.py", line 352, in __init__
    raise ConnectionFailure(str(e))
pymongo.errors.ConnectionFailure: could not connect to 162.243.27.34:27017: [Errno 111] Connection refused

我在这里做什么错了?

位于IP 162.243.27.34上的数据库服务器和客户端是具有不同IP的单独服务器.在上述两种情况下的示例中,我都尝试从远程客户端服务器进行连接,而不是连接至"localhost".

The database server in on the IP 162.243.27.34 and the client is a separate server with a different IP. In the above examples in both cases I'm attempting to connect from a remote client server rather than connecting to 'localhost'.

数据库服务器上有防火墙,但是客户端确实可以访问,并且由于mongo客户端命令行程序与客户端服务器的连接良好,我假设从客户端到数据库服务器的连接没有网络问题,而不是我在使用python进行连接时出现问题.

There is a firewall on the database server but the client does have access and as the mongo client command line program connects fine from the client server I'm assuming that there is no network problem in connecting from the client to the database server, rather that there is a problem in how I'm using python to connect.

运行命令:

cat /etc/mongodb.conf

显示:

bind_ip = 127.0.0.1
#port = 27017

我也正在使用MongoDB Shell版本:2.0.4

Also I'm using MongoDB shell version: 2.0.4

bind_ip = 127.0.0.1替换为bind_ip = 0.0.0.0,以允许从远程主机连接到MongoDB.

Replace the bind_ip = 127.0.0.1 with bind_ip = 0.0.0.0 to allow connections to MongoDB from remote hosts.