pymongo无法连接到远程服务器上的数据库 [英] pymongo cannot connect to database on remote server

查看:497
本文介绍了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.

这篇关于pymongo无法连接到远程服务器上的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆