Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Databases Mongo Basics Getting Started With MongoDB Setting Up MongoDB

Santiago Lopez
Santiago Lopez
3,446 Points

Connection Refused

Lopez-MacBook-Pro:mongodb-osx-x86_64-3.2.8 Lopezeselmejor$ bin/mongod 2016-08-06T18:26:28.283-0500 I CONTROL [initandlisten] MongoDB starting : pid=2022 port=27017 dbpath=/data/db 64-bit host=Lopez-MacBook-Pro.local 2016-08-06T18:26:28.284-0500 I CONTROL [initandlisten] db version v3.2.8 2016-08-06T18:26:28.284-0500 I CONTROL [initandlisten] git version: ed70e33130c977bda0024c125b56d159573dbaf0 2016-08-06T18:26:28.284-0500 I CONTROL [initandlisten] OpenSSL version: OpenSSL 0.9.8zh 14 Jan 2016 2016-08-06T18:26:28.284-0500 I CONTROL [initandlisten] allocator: system 2016-08-06T18:26:28.284-0500 I CONTROL [initandlisten] modules: none 2016-08-06T18:26:28.284-0500 I CONTROL [initandlisten] build environment: 2016-08-06T18:26:28.284-0500 I CONTROL [initandlisten] distarch: x86_64 2016-08-06T18:26:28.284-0500 I CONTROL [initandlisten] target_arch: x86_64 2016-08-06T18:26:28.284-0500 I CONTROL [initandlisten] options: {} 2016-08-06T18:26:28.284-0500 I STORAGE [initandlisten] exception in initAndListen: 98 Unable to create/open lock file: /data/db/mongod.lock errno:13 Permission denied Is a mongod instance already running?, terminating 2016-08-06T18:26:28.284-0500 I CONTROL [initandlisten] dbexit: rc: 100

Lopez-MacBook-Pro:mongodb-osx-x86_64-3.2.8 Lopezeselmejor$ bin/mongo MongoDB shell version: 3.2.8 connecting to: test 2016-08-06T18:28:08.433-0500 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, reason: errno:61 Connection refused 2016-08-06T18:28:08.433-0500 E QUERY [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed : connect@src/mongo/shell/mongo.js:229:14 @(connect):1:6

exception: connect failed

Why does this happen, and how can I fix it?

2 Answers

evans tsai
evans tsai
3,735 Points

Try this on terminal ,it may fix the problem.

brew services start mongodb

I also had trouble connecting to Mongo, but on my Windows machine. The trouble for me occurred because I wasn't setting the proper path to the database (and I also wasn't sure which order to run the "mongod" & "nodemon" commands). After I figured that out everything seemed to work perfectly (so far at least :)

Here were the steps I took:

  1. Make sure the database server is running before the node server is running
    a. Run mongod command with "--dbpath=" flag). The --dbpath= should point to the path where your database files are/will be located.
    b. Let's say your todo project is stored in the "mean-todo" folder and the path from root looks like this: "/myProjects/treehouse/mean-todo". Create a sub-folder called "data" within "mean-todo" to hold your mongo data.
    c. Now, run "mongod --dbpath=/myProjects/treehouse/mean-todo/data" - this tells mongo to set up a database in the "data" folder and to start a database server. (Note that this path is from the root of the drive. You don't have to assign --dbpath a path from root. If you don't want to assign from root, your path must be relative from whatever directory you're currently in.)
  2. Run "nodemon" or "node app.js" to start the node server & see if it works

Note, to anyone running Windows, the forward slashes ("/") in the directory paths I listed would be backward slashes ("\") instead :/

Here were some sources I looked at along with my own experimentation until it finally worked
The Dead Simple Step-by-Step Guide
StackOverFlow: OSX errno 61: Connection Refused
StackOverFlow: OSX Can't Connect to MongoDB

Please post back whether or not this was able to solve your problem.