2015-11-04 170 views
2

我遇到了將mongoengine加入我的django應用程序的問題。下面是我得到的錯誤:mongoengine.connection.ConnectionError:無法連接到數據庫默認值:[Errno 111]連接被拒絕

Traceback (most recent call last): 
    File "/home/dk2459/.local/lib/python3.4/site-packages/pymongo/mongo_client.py", line 374, in __init__ 
    self._ensure_connected(True) 
    File "/home/dk2459/.local/lib/python3.4/site-packages/pymongo/mongo_client.py", line 939, in _ensure_connected 
    self.__ensure_member() 
    File "/home/dk2459/.local/lib/python3.4/site-packages/pymongo/mongo_client.py", line 813, in __ensure_member 
    member, nodes = self.__find_node() 
    File "/home/dk2459/.local/lib/python3.4/site-packages/pymongo/mongo_client.py", line 904, in __find_node 
    raise AutoReconnect(', '.join(errors)) 
pymongo.errors.AutoReconnect: [Errno 111] Connection refused 
During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "/home/dk2459/.local/lib/python3.4/site-packages/mongoengine/connection.py", line 133, in get_connection 
    _connections[alias] = connection if connection else connection_class(**conn_settings) 
    File "/home/dk2459/.local/lib/python3.4/site-packages/pymongo/mongo_client.py", line 377, in __init__ 
    raise ConnectionFailure(str(e)) 
pymongo.errors.ConnectionFailure: [Errno 111] Connection refused 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "create_admin.py", line 7, in <module> 
    connect('happy_lens') 
    File "/home/dk2459/.local/lib/python3.4/site-packages/mongoengine/connection.py", line 173, in connect 
    return get_connection(alias) 
    File "/home/dk2459/.local/lib/python3.4/site-packages/mongoengine/connection.py", line 135, in get_connection 
    raise ConnectionError("Cannot connect to database %s :\n%s" % (alias, e)) 
mongoengine.connection.ConnectionError: Cannot connect to database default : 
[Errno 111] Connection refused 

下面是我試圖運行代碼:

import getpass 

from mongoengine import connect 

from models import User 

connect('happy_lens') 

admin = User(username='dk', email='[email protected]') 
admin.password='pwd' 
admin.save() 

及以下相關的settings.py代碼:

DATABASES = { 
    'default' : { 
     'ENGINE': 'django_mongodb_engine', 
     'NAME': 'happy_lens', 
     'HOST': '192.168.1.35', 
     'POST': 12345 

    } 
} 

SESSION_ENGINE = 'mongoengine.django.sessions' 

一切我搜索沒有工作。請幫忙。

+0

在/ var/lib中/ MongoDB的拆卸mongod.lock並重新啓動服務或可能(如果你沒有),你可以嘗試從這裏的解決方案http://stackoverflow.com/questions/24899849/connection-refused-to-mongodb-errno-111 –

+0

我查找/ var/lib中的mongodb文件夾/並找不到它。你知道什麼可能導致這個問題?我已經通過pip3安裝 - 用戶mongoengine安裝了mongonenine。 – no8do

回答

2

所以我想出了我自己的問題。我是mongo的新手,所以我認爲它的工作方式與sqlalchemy的工作方式相同,即在pythonanywhere平臺上自動創建sqlite數據庫,但情況並非如此。

對於大家第一次使用pythonanywhere和mongo,確保你得到一個外部服務(我在Amazon AWS上使用MongoLab)。一旦你建立了一個數據庫,你可以通過在mongoengine.connect函數中提供正確的MongoDB URI來連接它。

0

確保mongodb已啓動並正在運行。

service mongod status 
service mongod start (on ubuntu) 
+0

我試着運行兩條線,我得到了以下錯誤:mongod:無法識別的服務 – no8do

+0

您使用的是哪個操作系統?你有沒有正確安裝mongodb?按照mongoDB安裝說明(https://docs.mongodb.org/manual/administration/install-on-linux/)教程,確保它已正確安裝在所需的操作系統上 –

1

Pymongo 3.0及以上版本都有這個problem.you應該先卸載最新版本,然後安裝python-pymongo == 2.8.1

相關問題