Tuesday, 24 February 2015

MongoDB Correct way to start MongoDB - Error Cannot connect to mongodb errno:61 Connection refused [Solved]

Recently, while involved in full stack Django development and developing an application using Mongodb as database backend, I encountered following error after installing Mongodb on my mac. Ok first, I installed Mongodb by using brew install command:

brew update
brew install mongodb

After installing Mongodb, I tried to access the mongo shell by using mongo command, but I got "Cannot connect to mongodb errno:61 Connection refused error" . Here is the full trace:

alis-air:~ aliraza$ mongo
MongoDB shell version: 2.6.6
connecting to: test
2015-02-24T17:51:36.067+0500 warning: Failed to connect to 127.0.0.1:27017, reason: errno:61 Connection refused
2015-02-24T17:51:36.069+0500 Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed at src/mongo/shell/mongo.js:146
exception: connect failed

I searched for correct ways to start Mongodb on Mac OS. I tried several solutions but the one which worked for me is as follows: In the shell write sudo mongod to start it.

$ mkdir -p /data/db
$ sudo mongod

This started my Mongodb. Next I opened another shell instance (new shell window) and wrote mongo command and it worked without Error Cannot connect to mongodb errno:61 Connection refused! MongoDB starting Problem Solved.

$ mongo
MongoDB shell version: 2.6.6
connecting to: test
Server has startup warnings: 
2015-02-24T18:20:51.384+0500 [initandlisten] 
2015-02-24T18:20:51.384+0500 [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
> 

10 comments:

  1. Hey, thanks! This was very helpful to me, as I had the same problem on my mac. Maybe you could mention, that the "/data/db" folder has to be placed in the root-directory of the mac even if the "/" at the beginning should determine that. (I placed mine in my project folder at first, it didn't work well - the only logical conclusion was the root-dir.. neverthless..)

    ReplyDelete
    Replies
    1. I have the same problem, thank you very much!

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. I encountered the same problem, 'sudo mongod' solved this problem, Thank you!

    ReplyDelete
  4. This field involves the use of Machine Learning that has enabled industries to create higher quality merchandise that are customized for each consumer. data science course syllabus

    ReplyDelete
  5. This is a very useful and practical guide for developers facing MongoDB connection issues. The explanation of checking the MongoDB service status, ensuring the server is running, and troubleshooting connection errors provides a clear approach to resolving common setup problems. Such step-by-step troubleshooting resources are valuable because they help developers quickly identify configuration issues and maintain a more reliable database environment. Great work sharing these helpful insights with the community.
    Saudi building codes compliance

    ReplyDelete
  6. This article is useful because it focuses on a very specific MongoDB startup problem encountered during Django full stack development. The distinction between installing MongoDB with Homebrew and actually starting the MongoDB server is important, since running the mongo shell before the server is available results in the 127.0.0.1:27017 connection-refused error.

    The troubleshooting sequence is easy to follow, particularly the creation of the /data/db directory followed by starting mongod. Opening another terminal and then running the mongo command demonstrates the difference between the database server process and the client shell, which is a useful concept when working with a MongoDB Course.

    ReplyDelete
  7. The article also connects MongoDB with Django application development, showing how a database backend becomes part of a larger full stack application environment. The example is especially relevant for developers working across application logic, database connectivity, and server configuration in a Python Full Stack Course.

    ReplyDelete
  8. The error message itself provides a good diagnostic clue: the shell attempts to connect to 127.0.0.1:27017, but the connection is refused because the MongoDB server has not been started. This kind of separation between service availability and application-level connectivity is also relevant when developing Web Development Final Year Projects.

    ReplyDelete