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 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.

  • 2
    you need to use more precise terms. 'mongod' is the daemon that needs to be running for "mongoDB" to be running. Then you connect to it via 'mongo' shell - so 'mongo' gives an error for you because 'mongod' is not running. You say when you start mongod at the end there "it works fine" - what does that mean? Can you now run 'mongo' shell and connect to the server? Jan 4 '14 at 5:07
  • Actually simple mongo command gives error everytime for me and mongod command works for me. And I want to run simple mongo command for running mongodb. the config file is as usual like mongodb config file there is no changes made by me. Jan 6 '14 at 5:02
  • 'mongo' command starts the interactive shell for connection to mongodb it is not the database. 'mongod' is the command for starting mongodb server. Jan 7 '14 at 3:12
  • I know mongo is the command-line shell that connects to a specific instance of mongod. When you run mongo with no parameters it defaults to connecting to the localhost on port 27017, but in my case mongo is not running with localhost:27017 but mongod is connecting after using --fork. Jan 7 '14 at 6:05
  • mongod does not connect - mongod is the server. --fork just says "spawn mongod into a separate process so I can get the console control back". Jan 7 '14 at 19:09

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.


I believe there are 3 points:

  1. you should try to use sudo when you are writing log to /var/log. Or at least make sure the .log file have correct permission for your current user. We had tons of issues with this before.

  2. I see you are deleting .lck file. Did you shut down mongo by having a crash? In that case, definitely check if .lck file are locking mongo. I believe they are safe to delete.

  3. If you are using mongo.exe, be sure the mongod process is running first. Try top command to check for the process.

  • I am using mongodb in Ubuntu Linux and there is no any .exe file When I start/restart the mongodb with command mongodb start/restart it says there is no service available for stop. and one thing there is no permission issue with .log file it is not going with sudo its working simply and mongodb:mongodb is the user for log file. Jan 6 '14 at 5:06
  • Can you add the log file from the mongod to help the diagnosis of your problem as this will provide much more detail in understanding what is occurring ?
    – eoinbrazil
    Jan 10 '14 at 15:30
  • I noticed for your log file, you wrote:--logpath /var/log/mongodb/mongodb.log\ . By any chance, can you try to remove the trailing \ from the log file name?
    – Grant Li
    Jan 10 '14 at 20:23

As I have noticed your issue for that you have stop your service. Delete your existing log file. After that check PID of the your service. If still exist PID then kill that PID.

Finally Start the mongod service. Hopefully this will help for you.Once you started the service log file will be newly generated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.