0

我想通过 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

请帮忙 !!提前致谢 !!

4

1 回答 1

0

I think , there is no need of setting environment variable for testing it in local machine , and maybe the error is because it didn't get set properly.

Another issue I can see , that you haven't replaced user and pass with your username password of user you created for particular database.Try connecting your mongo shell with that monhqurl by typing mongo --username <user> --password <pass> --host <host> --port 28015 replacing it with your details .If it connects successfully then mongohqurl is correct

于 2013-10-15T17:30:32.423 回答