8

I follow this mongoose document enter link description here

mongoose.connect('mongodb://localhost/waterDB');

Using This, I can connect local machine waterDBmongoDB DataBase

My Personal Machine Local IP : 192.168.1.5

My Server Machine Local IP : 192.168.1.100

Both machine have waterDB DataBase . There is no username and password for both DB

I wanted to connect Server Machine waterDB Inside My Personal Machine.

According To This : mongoose.connect('mongodb://username:password@host:port/database?options...');

I try : mongoose.connect('mongodb://192.168.1.100:27017/waterDB');

But,

MongoError: failed to connect to server [192.168.1.100:27017] on first connect
at null.<anonymous> (/home/water/node_modules/mongodb-core/lib/topologies/server.js:313:35)
at emitOne (events.js:77:13)
at emit (events.js:169:7)
..........

Any solution for err ?

Thank (@_@)

Amila Sampath
  • 655
  • 4
  • 11
  • 28

4 Answers4

27

It might be a problem with your MongoDB instance listening on localhost only. You can change the bind address in MongoDB's configuration file. The config file may be located in /etc/mongodb.conf or /etc/mongod.conf. There are also 2 config file formats:

Old format (still supported):

bind_ip = 0.0.0.0

YAML (version 2.6+):

net:
    bindIp: 0.0.0.0

After changing the config file you have to restart the MongoDB server.

Lukasz Wiktor
  • 19,644
  • 5
  • 69
  • 82
  • 1
    Why `0.0.0.0` though? What does it mean in this case? EDIT: Found the answer to my question: "To bind to all IPv4 addresses, enter 0.0.0.0." from the `bind address` link. – Zebiano Feb 17 '21 at 00:48
3

Try without the Port no.

mongoose.connect('mongodb://192.168.1.100/waterDB');

this should work for you.

But make sure both are connected on the same network, if you are connected on other network than your server is, then it wont work

Ravi Shankar Bharti
  • 8,922
  • 5
  • 28
  • 52
2

For me (using windows and mongo version 6, and remotely connecting from Mac), after changing config file to

net:
  port: 27017
  bindIp: "*" # OR 0.0.0.0

Then starting server with: mongod, it was still binding to 127.0.0.1 (localhost). I had to start the server with: mongod --bind_ip_all

OJ Smith
  • 43
  • 4
1

You will have to use SSH tunnel in this case. Refer to the following link which shows how you can create SSH tunnel.
Node.js SSH Tunneling to MongoDB using Mongoose