mongoengine.connection.ConnectionError: Cannot connect to database default : [Errno 111] Connection refused

mongoengine.connection.ConnectionError: Cannot connect to database default : [Errno 111] Connection refused

我在将 mongoengine 合并到我的 django 应用程序时遇到问题。以下是我收到的错误:

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='someemail@gmail.com')
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'

我搜索的所有内容均无效。请帮忙。

确保 mongodb 已启动并且 运行。

service mongod status
service mongod start (on ubuntu)

所以我想出了我自己的问题。我是 mongo 的新手,所以我认为它的工作方式与 sqlalchemy 的工作方式相同,其中将在 pythonanywhere 平台中自动创建 sqlite 数据库,但事实并非如此。

对于第一次将 pythonanywhere 与 mongo 一起使用的每个人,请确保获得外部服务(我在 Amazon AWS 上使用 MongoLab)。设置数据库后,您可以通过在 mongoengine.connect 函数中提供正确的 MongoDB URI 来连接它。

Pymongo 3.0及以上版本有此problem.you应先卸载最新版本再安装python-pymongo==2.8.1