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

JavaScript User Authentication With Express and Mongo User Registration Setting Up Mongoose and a Mongo Database

MongoDB Connection Error - first connect

Thanks for helping.

After $ nodemon, I receive this error:

Express app listening on port 3000 connection error: { [MongoError: failed to connect to server [mongodb:27017] on first connect] name: 'MongoError', message: 'failed to connect to server [mongodb:27017] on first connect' }


Before, I ran $ mongod, recieving this error:

2016-11-10T13:42:32.969-0500 I CONTROL [initandlisten] MongoDB starting : pid=2016 port=27017 dbpath=/data/db 64-bit host=Tiffs-computer.local 2016-11-10T13:42:32.971-0500 I CONTROL [initandlisten] db version v3.2.9 2016-11-10T13:42:32.971-0500 I CONTROL [initandlisten] git version: 22ec9e93b40c85fc7cae7d56e7d6a02fd811088c 2016-11-10T13:42:32.971-0500 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2i 22 Sep 2016 2016-11-10T13:42:32.971-0500 I CONTROL [initandlisten] allocator: system 2016-11-10T13:42:32.971-0500 I CONTROL [initandlisten] modules: none 2016-11-10T13:42:32.971-0500 I CONTROL [initandlisten] build environment: 2016-11-10T13:42:32.971-0500 I CONTROL [initandlisten] distarch: x86_64 2016-11-10T13:42:32.971-0500 I CONTROL [initandlisten] target_arch: x86_64 2016-11-10T13:42:32.971-0500 I CONTROL [initandlisten] options: {} 2016-11-10T13:42:33.121-0500 I STORAGE [initandlisten] exception in initAndListen: 29 Data directory /data/db not found., terminating 2016-11-10T13:42:33.122-0500 I CONTROL [initandlisten] dbexit: rc: 100 Tiffs-computer:S2V3 tiffinyli$ mongod 2016-11-10T13:43:58.074-0500 I CONTROL [initandlisten] MongoDB starting : pid=2024 port=27017 dbpath=/data/db 64-bit host=Tiffs-computer.local 2016-11-10T13:43:58.074-0500 I CONTROL [initandlisten] db version v3.2.9 2016-11-10T13:43:58.074-0500 I CONTROL [initandlisten] git version: 22ec9e93b40c85fc7cae7d56e7d6a02fd811088c 2016-11-10T13:43:58.074-0500 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2i 22 Sep 2016 2016-11-10T13:43:58.074-0500 I CONTROL [initandlisten] allocator: system 2016-11-10T13:43:58.074-0500 I CONTROL [initandlisten] modules: none 2016-11-10T13:43:58.074-0500 I CONTROL [initandlisten] build environment: 2016-11-10T13:43:58.074-0500 I CONTROL [initandlisten] distarch: x86_64 2016-11-10T13:43:58.074-0500 I CONTROL [initandlisten] target_arch: x86_64 2016-11-10T13:43:58.074-0500 I CONTROL [initandlisten] options: {} 2016-11-10T13:43:58.075-0500 I STORAGE [initandlisten] exception in initAndListen: 29 Data directory /data/db not found., terminating 2016-11-10T13:43:58.075-0500 I CONTROL [initandlisten] dbexit: rc: 100

I see the error is " exception in initAndListen: 29 Data directory /data/db not found., terminating", where do I fix this / or how do I get this running? Thank you!

3 Answers

Alessandro Romanelli
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alessandro Romanelli
Full Stack JavaScript Techdegree Graduate 16,220 Points

For any Linux users, I've encountered this error, too, despite having my data/db folder setup. Turns out that the super user authorization is required by Mongod to properly run and access that folder.

Therefore to avoid this common error either login as root (run the command "~ sudo -s" and then "~ whoami", you should now be logged as root) and only then run "~ mongod" when you're done remember to "~ exit" or you'll stay logged in as root (not recommended), OR run the same command as superuser with the following command "~ sudo mongod", you'll then be prompted to authenticate yourself!

I hope that this can be of any help to future students! Happy coding!

Tom Geraghty
Tom Geraghty
24,174 Points

You may also want to adjust permissions on the /data/db folder (chmod/chown) so any program that needs write access doesn't also have to be sudo/super user/admin.

Mine looks like:

~ ls /data -al
drwxrwxrwx  4 root root 4096 Oct  4 07:17 db

In my experience I've always needed sudo for mongod, but not for mongo

Seth Kroger
Seth Kroger
56,413 Points

It looks like you still need to create a directory (or folder) for the mongo database on the root directory (or top directory of the drive in Windows) named data then a directory inside that called db.

Thanks. It was in part to that. Yes, I needed to create the /data/db root folder, but it still didn’t run with $ mongod

I suspect? mongo searches for that folder on my system but doesn’t know to look in my root folder of my project.

When i ran in root folder of my project: $ mongod --dbpath whateverpathtodbdata/data/db it works.

Seth Kroger
Seth Kroger
56,413 Points

Yes, by "Root directory" I meant the system root, / or C:\ depending on you OS, where mongod looks by default.