Reputation: 14524
I want to do the following structure for a website.
The idea is put web app on one server, and put MongoDB on another server. They are different machines.
In nodejs code, the only configuration for db is the following code:
var connectString = 'mongodb://localhost/myDataBase';
Replace 'localhost' with '111.222.111.2', the ip address of the machine where MongoDB is running on, then it should work. Is this correct?
Will this make data retrieval slower comparing with putting the on one physical machine?
How the two machines communicate with each other to get the data? Through what protocol?
Upvotes: 0
Views: 1480
Reputation: 131
You can have NodeJS at one server and MongoDB database at another server. You can test it with mlab.com Mongo Database http://docs.mlab.com/ (free for 500MB).
The easiest way to connect with that DB is to use mongoose:
const mongoose = require('mongoose');
mongoose.connect("mongodb://<login>:<password>@ds140361.mlab.com:45242/mydb");
The database is protected with login and password. However, API of nodeJS should be protected independently.
IMO if one server isn't at the one side of the planet and second on the other side, you shouldn't see the differences in speed of the connection.
About protocol:
https://docs.mongodb.com/manual/reference/mongodb-wire-protocol/
Upvotes: 2
Reputation: 36319
Yes, replacing localhost with the IP address or domain name of your MongoDB server will work, providing that the remote server allows connections on the default port for Mongo (27017) and there are no firewalls in the middle blocking it.
It will be slower than the localhost connection, how much slower will depend entirely on the network infrastructure separating the two.
The protocol is a TCP/IP socket.
Upvotes: 1