简体   繁体   English

如何配置MongoDB和Node.js使用两个不同的服务器

[英]How to configure MongoDB and nodejs to use two different servers

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. 这个想法是将Web应用程序放在一台服务器上,而将MongoDB放在另一台服务器上。 They are different machines. 他们是不同的机器。

In nodejs code, the only configuration for db is the following code: 在nodejs代码中,db的唯一配置是以下代码:

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. 将“ localhost”替换为“ 111.222.111.2”(运行MongoDB的计算机的IP地址),然后它将正常工作。 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? 通过什么协议?

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. 是的,只要您的远程服务器允许Mongo的默认端口(27017)上的连接且中间没有防火墙对其进行阻止,则用MongoDB服务器的IP地址或域名替换localhost即可。

It will be slower than the localhost connection, how much slower will depend entirely on the network infrastructure separating the two. 它会比localhost连接慢,多少慢将完全取决于将两者分开的网络基础结构。

The protocol is a TCP/IP socket. 该协议是一个TCP / IP套接字。

You can have NodeJS at one server and MongoDB database at another server. 您可以在一台服务器上拥有NodeJS,而在另一台服务器上拥有MongoDB数据库。 You can test it with mlab.com Mongo Database http://docs.mlab.com/ (free for 500MB). 您可以使用mlab.com Mongo数据库http://docs.mlab.com/ (免费提供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. 但是,nodeJS的API应该独立保护。
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. IMO,如果一台服务器不在地球的一侧而另一台不在另一侧,则您应该不会看到连接速度的差异。
About protocol: 关于协议:
https://docs.mongodb.com/manual/reference/mongodb-wire-protocol/ https://docs.mongodb.com/manual/reference/mongodb-wire-protocol/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM