I am having problem connecting to MongoDB from NodeJS using following sample code. I tried running \"mongod\" with or without sudo but nodejs code still fail to connect. I a
The first time, I made the obvious mistake that mongod
(the mongo Server) was not even running.
The second time, I had both the Server (mongod
) and the Client (mongo
) running on my Windows; in separate Command Prompts (of course mongo
running first). mongod
and/or its Command Prompt was "hung up" (i.e. the last line only said this, and the last line remained like this despite attempts to connections:)
2017-06-20T12:31:02.937-0600 I NETWORK [thread1] waiting for connections on port 27017
I clicked in the Command Prompt pressed the space-bar to "kick it" , and the Command Prompt printed lines like these:
2017-06-20T12:31:48.517-0600 I NETWORK [thread1] connection accepted from 127.0.0.1:50260 #1 (1 connection now open)
Which worked!
./mongod --bind_ip localhost
yes , I came across this error too, and my hosts is as follow:
127.0.0.1 xxxxxx(the ip of my computer)
and when I ran npm start
in express project, it got an error like that . After I tried to change the mapping of 127.0.0.1
to localhost
, it had no error.
Try adding 127.0.0.1 localhost
in /private/etc/hosts
file.
Somebody on Github found a solution: Instead of writing:
Mongoose.connect(config.database.url);
Write:
Mongoose.connect(config.database.url, {
keepAlive: true,
reconnectTries: Number.MAX_VALUE,
useMongoClient: true
});
(Source https://github.com/Automattic/mongoose/issues/5399)
Never mind, I was able to resolve the issue by using 127.0.0.1 instead of localhost, not sure why I have to use ip address. My tomcat, apache, redis, and even node server all works using localhost but not the mongodb. Is there is config I need change to make it work using local host?