MongoDB doesnt start normally

mongodb

I want to start mongo on terminal but it gives connection error like:

MongoDB shell version: 2.4.5
connecting to: test
Sat Jan  4 10:18:16.779 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 run the following commands:

rm /var/lib/mongodb/mongod.lock
mongod --dbpath /var/data/mongodb --repair
restart the mongodb

But unable to start mongo.
When I run the following command:

mongod --profile=1 --slowms=20 --dbpath /var/data/mongodb --fork --logpath /var/log/mongodb/mongodb.log\

It works fine but log is not created inside logger.

Please help me to fix this issue this is so wierd issue for me.

Best Answer

There are two separate processes that you are referring to.

  • mongod is the command that starts the MongoDB server process. It listens on a port (27017 by default) and you can connect to it with mongo shell or with your driver from an application.

  • mongo is the mongo interactive shell. It tries to connect to a port (27017 by default) to talk to the mongod server process. It will not be able to connect to a port that does not have a running mongod process listening on.

You need to start mongod first. Whether you start it logging to the console or to a log file (--logpath) it needs to be started and running successfully. If you provide a --logpath argument then you can also provide a --fork argument which will detach it and run it as a separate process, rather than attached to your console.

Once you have mongod successfully running and waiting for connections, you can now run mongo program to start up the shell and connect to the server.