Skip to content
Advertisement

Problems establishing connection with DB

Platform: LINUX.

I am a beginner of MongoDB and pymongo. After installing pymongo, here is a simple test I tried on ipython:

import pymongo
client = pymongo.MongoClient();
# Also tried to specify the local host and port number
db = client['myDB']
collections = db['temptables']
collections.insert({'a':'1'})

At this point, it chokes. And in the end, spits out a “Error 111: connection refused” error. So, I tried invoking MongoDB straight from the terminal and I still got the error below [look at the far end]. So, I searched a bit and tried:

  1. removing the lock ( sudo rm /var/lib/mongodb/mongod.lock ). Turns out there was no lock in the first place.

  2. sudo mongod –repair

  3. I even saw a suggestion to comment out the host and port number from the config file. Tried that too, didn’t work.

None of the above worked.

This is the error I see when I try to invoke mongodb from command line.


017-08-17T15:25:30.265-0700 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: Connection refused 2017-08-17T15:25:30.265-0700 E QUERY [thread1] Error: couldn’t connect to server 127.0.0.1:27017, connection attempt failed : connect@src/mongo/shell/mongo.js:237:13 @(connect):1:6

exception: connect failed

Help, please.

Advertisement

Answer

Your mongo server isn’t running.

You can confirm this by executing sudo ps -ef | grep mongod

If you have mongo installed and in your path, you can execute:

cd && mkdir -p ~/temp_mongo_db && mongod --dbpath=./temp_mongo_db

This will launch mongo and place all database files in your home directory under ‘temp_mongo_db’.

Finally, in a new terminal window, execute sudo ps -ef | grep mongod again. You’ll now see mongod running.

If you want to run mongo in production, you should configure it to be managed by SystemD or some other init system.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement