Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to connect to mongodb Error: couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:L112

Tags:

mongodb

When ever I try to connect to mongo db I always get this error as below.

MongoDB shell version: 2.4.3 connecting to: test Fri Apr 26 14:31:46.941 JavaScript execution failed: Error: couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:L112 exception: connect failed

I tried various solutions listed in stackoverflow and tried following commands but nothing works.

1) sudo rm /var/lib/mongodb/mongod.lock

// says such file located at this place.

2) sudo service mongodb start

// error: sudo: service: command not found

3) I've made sure nothing else is running at that port.

4) Even tried uninstalling and installing it again.

5) Also tried kill pid

like image 434
JP. Avatar asked Apr 26 '13 21:04

JP.


People also ask

Why is my MongoDB not connecting?

These are some of the solutions: Ensure that your MongoDB instance is running: Compass must connect to a running MongoDB instance. Also check you have installed MongoDB and have a running mongod process. You should also check that the port where MongoDB is running matches the port you provide in the compass connect.

How do I connect to localhost 27017?

To connect to your local MongoDB, you set Hostname to localhost and Port to 27017 . These values are the default for all local MongoDB connections (unless you changed them). Press connect, and you should see the databases in your local MongoDB.

How do I allow remote access to MongoDB?

MongoDB can be managed remotely or connected to a separate application server by making a few changes to the default configuration. First, we'll install the MongoDB using the docker container, then configure the MongoDB installation to access from a trusted remote machine securely.


2 Answers

Same error here, this worked for me:

sudo mongod --repair

followed by

sudo mongod

(forget about service and start)

Then on a different tab/terminal:

mongo
like image 178
Juan Serrats Avatar answered Oct 09 '22 19:10

Juan Serrats


Start mongod server first

mongod

Open another terminal window

Start mongo shell

mongo
like image 38
Esakkiappan .E Avatar answered Oct 09 '22 20:10

Esakkiappan .E