3

I am having issues incorporating mongoengine into my django app. Below is the error I am getting:

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

Below is the code I am trying to run:

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()

And below is the relevant settings.py code:

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

   }
}

SESSION_ENGINE = 'mongoengine.django.sessions'

Everything I've searched has not worked. Please help.

no8do
  • 66
  • 1
  • 6
  • Removing mongod.lock from /var/lib/mongodb and restarting the service or maybe (if you didn`t ) you can try the solution from here http://stackoverflow.com/questions/24899849/connection-refused-to-mongodb-errno-111 – Matzy schneider Nov 04 '15 at 09:38
  • I looked for the mongodb folder in /var/lib/ and could not find it. Do you know what could be causing this issue? I've installed mongonenine via pip3 install --user mongoengine. – no8do Nov 04 '15 at 17:52

3 Answers3

2

So I figured out my own issue. I'm new to mongo so I thought that it would work the same way as sqlalchemy where a sqlite DB would be automatically created in the pythonanywhere platform, which is not the case.

For everyone using pythonanywhere together with mongo for their first time, make sure you get an external service (I am using MongoLab on Amazon AWS). Once you have set up a database, you can connect to it by providing the right MongoDB URI in the mongoengine.connect function.

no8do
  • 66
  • 1
  • 6
1

Pymongo 3.0 and above versions have this problem.you should first uninstall latest version and then install python-pymongo==2.8.1

Udit Kumawat
  • 654
  • 1
  • 8
  • 21
0

Make sure that mongodb is up and running.

service mongod status
service mongod start (on ubuntu)
Saeed Farzian
  • 324
  • 1
  • 5
  • I tried running both lines and I got the following error:mongod: unrecognized service – no8do Nov 04 '15 at 17:41
  • Which OS are you using? Have you installed mongodb properly? Follow instructions of mongoDB installation (https://docs.mongodb.org/manual/administration/install-on-linux/) tutorials to make sure that it's installed properly on your desired OS – Saeed Farzian Nov 05 '15 at 21:01