与mongoHQ的Python连接失败了吗?

时间:2013-10-15 17:14:00

标签: python mongodb pymongo mongohq

我想通过python连接到mongoHq数据库。 这是我到目前为止所做的事情

设置环境变量:

MONGOHQ_URL = mongodb://myusername:mypassword@paulo.mongohq.com:10084/mydb

和app.py文件

import os
import datetime
import pymongo
from pymongo import MongoClient
MONGO_URL = os.environ.get('MONGOHQ_URL')
client = MongoClient(MONGO_URL)
db = client.mydb

当我运行此文件后出现错误

Traceback (most recent call last):
File "C:\Python33\lib\site-packages\pymongo-2.6.2-py3.3-win-amd64.egg\pymongo\mongo_client.py", line 349, in __init__
self.__find_node(seeds)
File "C:\Python33\lib\site-packages\pymongo-2.6.2-py3.3-win-amd64.egg\pymongo\mongo_client.py", line 750, in __find_node
raise AutoReconnect(', '.join(errors))
pymongo.errors.AutoReconnect: could not connect to localhost:27017: [WinError 10061] No connection could be made because the target machine actively refused it

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Python33\flaskk\test.py", line 9, in <module>
client = MongoClient(MONGO_URL)
File "C:\Python33\lib\site-packages\pymongo-2.6.2-py3.3-win-    amd64.egg\pymongo\mongo_client.py", line 352, in __init__
raise ConnectionFailure(str(e))
pymongo.errors.ConnectionFailure: could not connect to localhost:27017: [WinError    10061] 
No connection could be made because the target machine actively refused it

请帮忙!!在此先感谢!!

1 个答案:

答案 0 :(得分:0)

我认为,没有必要设置环境变量来在本地机器上测试它,也许错误是因为它没有正确设置。

我可以看到的另一个问题是,您没有替换用户并传递您为特定数据库创建的用户的用户名密码。尝试通过键入将您的mongo shell与该monhqurl连接 mongo --username <user> --password <pass> --host <host> --port 28015 用你的细节替换它。如果它连接成功,那么mongohqurl是正确的