Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to connect another machine mongodb database inside local network ?

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 (@_@)

like image 872
Amila Sampath Avatar asked Jan 05 '17 06:01

Amila Sampath


People also ask

How do I access MongoDB from another machine?

To allow remote connections, you must edit the MongoDB configuration file — /etc/mongod. conf — to additionally bind MongoDB to an IP address which can be reached by your trusted remote computer. This way, your MongoDB installation will be able to listen to connections made to your MongoDB server from remote machines.

How does MongoDB connect to another database?

According to the fine manual, createConnection() can be used to connect to multiple databases. However, you need to create separate models for each connection/database: var conn = mongoose. createConnection('mongodb://localhost/testA'); var conn2 = mongoose.

How do I connect to a specific database in MongoDB?

Connect to a Single MongoDB Instance const MongoClient = require('mongodb'). MongoClient; const assert = require('assert'); // Connection URL const url = 'mongodb://localhost:27017'; // Database Name const dbName = 'myproject'; // Use connect method to connect to the server MongoClient.


1 Answers

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.

like image 140
Lukasz Wiktor Avatar answered Oct 18 '22 10:10

Lukasz Wiktor