Can't connect to localhost via Node

Hi, I’m new at Mongo, I do have worked with CLI and have a basic understanding of Mongo.
I’m trying to connect to a local database with my NodeJS app, and I have a ‘dotent’ file, with I use to store the connection URI, like this :
DATABASE=mongodb://localhost:27017/node-form

I use mongoose btw.
I get this error :

    -: Connection error: connect ECONNREFUSED 127.0.0.1:27017 :-
(node:8745) UnhandledPromiseRejectionWarning: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
    at new MongooseServerSelectionError (/Users/gaetan/WorkSpace/Back et front/Back/Fichiers_de_cours/node-forms_4/node_modules/mongoose/lib/error/serverSelection.js:24:11)
    at NativeConnection.Connection.openUri (/Users/gaetan/WorkSpace/Back et front/Back/Fichiers_de_cours/node-forms_4/node_modules/mongoose/lib/connection.js:823:32)
    at Mongoose.connect (/Users/gaetan/WorkSpace/Back et front/Back/Fichiers_de_cours/node-forms_4/node_modules/mongoose/lib/index.js:333:15)
    at Object.<anonymous> (/Users/gaetan/WorkSpace/Back et front/Back/Fichiers_de_cours/node-forms_4/start.js:8:10)
    at Module._compile (internal/modules/cjs/loader.js:1138:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
    at Module.load (internal/modules/cjs/loader.js:986:32)
    at Function.Module._load (internal/modules/cjs/loader.js:879:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47
(node:8745) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:8745) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

How do i fix this ?
I use MacOS and I can’t even run “mongod” in CLI, yet I have mongo installed. When I write mongo --version in CLI, I have version 4.4.0 installed. I just don’t get it.

Do you have a mongod process running? Can you connect to that process by running the mongo shell, mongo. These are the first steps you should take as new user of MongoDB.

We have a full tutorial here. As a beginner you may be better starting with MongoDB Atlas.

1 Like

I have atlas installed, and the conection works fine with my online cluster. I’m accustomed to mongo, but I don’t quite understand why the local instance of mongo won’t connect.

1 Like

Can I clarify. Your local mongo shell (mongo not mongod) can connect to Atlas but your local mongo shell cannot connect to a local instance of mongod running on your own workstation/laptop?

Sory i din’t updated my post. Problem soved !
I just use the path argument when running local database :
mongod --dbpath /usr/local/var/mongodb

Thanks anyway.

4 Likes

Excellent glad you sorted the problem. Please feel to post any and all beginner questions here. It’s easy to go wrong when you are starting.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.