如何在节点上设置MongoDB。在EC2环境中使用node-mongodb-native的js服务器?

时间:2021-07-28 22:44:24

I got help from many people here, and now I want to contribute back. For those who are having trouble making a Node.js server work with MongoDB, here is what I've done.

我在这里得到了很多人的帮助,现在我想做出贡献。对于那些创建节点有困难的人。js服务器与MongoDB一起工作,以下是我所做的。

3 个解决方案

#1


10  

This was originally posted by the question asker. A mod asked him in the comments to post it as an answer, but got no response. So, I cleaned it up and am posting it myself.

这个问题最初是由提问者发布的。一个国防部在评论中要求他发布这条消息作为回应,但没有得到回应。所以,我把它清理干净,自己贴上去。

When you look at the code, you will notice that the createServer code is inside db.open. It won't work if you reverse it. Also, do not close the db connection. Otherwise, after the first time, the db connection will not be opened again. (Of course, db.open is declared outside of createServer.) I have no clue why createServer is inside db.open. I guess it may have to do with not opening too many db connections?

当您查看代码时,您将注意到createServer代码位于db.open中。如果你把它倒过来,它就不工作了。另外,不要关闭db连接。否则,第一次连接后,将不会再次打开db连接。(当然,db。open是在createServer之外声明的。我不知道为什么createServer在db.open里面。我猜这可能与没有打开太多的db连接有关吧?

Also, one problem I face is that when I run it via SSH, even if I run the server in the background (e.g. $ node server.js &), after 2.5 hours, the server dies (not the instance though). I am not sure if it is because of terminal connection or what.

另外,我面临的一个问题是,当我通过SSH运行它时,即使我在后台运行服务器(例如$ node server)。在2.5小时后,服务器死亡(尽管不是实例)。我不确定是由于终端连接还是什么原因。

Here is the procedure & code

这是程序和代码

Environment: EC2, AMS-Linux-AMI

环境:AMS-Linux-AMI EC2

Purpose: Take an HTTP request and log the query, IP and timestamp into MongoDB.

目的:获取一个HTTP请求并将查询、IP和时间戳记录到MongoDB中。

Steps

步骤

1) After creating the instance (server), install gcc.

创建实例(服务器)后,安装gcc。

$ yum install gcc-c++

2) Download Node.js files and unzip them. (I used version 2.6.)

2)下载节点。js文件并解压它们。(我使用的是版本2.6。)

$ curl -O http://nodejs.org/dist/node-v0.2.6.tar.gz
$ tar -xzf node-v0.2.6.tar.gz

I renamed the unzipped folder to just "nodejs"

我将未压缩文件夹重命名为“nodejs”

$ cd nodejs
$ sudo ./configure --without-ssl
$ sudo make
$ sudo make install

make takes a long while.... After that you can try running the sample in nodejs.org

制作需要很长一段时间....之后,您可以尝试在nodejs.org中运行示例

3) Install MongoDB. I installed version 1.6.5, not 1.7.

3)安装MongoDB。我安装了版本1.6.5,而不是1.7。

$ curl -O http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-1.6.5.tgz
$ tar -xzf mongodb-linux-x86_64-1.6.5.tgz
$ sudo mkdir /data/db/r01/

I renamed the folder to "mongodb"

我将该文件夹重命名为“mongodb”

Run the db process:

db运行过程:

$ ./mongodb/bin/mongod --dbpath /data/db/r01/

Then if you like, you can run and try out the command line. Refer to MongoDB's website.

然后,如果您愿意,您可以运行并尝试命令行。请参阅MongoDB的网站。

4) I recommend that you create your own AIM based on your instance. It will take 20 minutes. Then, recreate the install and run MongoDB again.

4)我建议你根据你的实例创建自己的目标。需要20分钟。然后,重新创建安装并再次运行MongoDB。

5) Install node-mongodb-native

5)安装node-mongodb-native

$ curl -O https://download.github.com/christkv-node-mongodb-native-V0.8.1-91-g54525d8.tar.gz
$ tar -xzf christkv-node-mongodb-native-V0.8.1-91-g54525d8.tar.gz

I renamed the folder to node-mongodb-native

我将该文件夹重命名为node-mongodb-native。

$ cd node-mongodb-native
$ make

6) Here is the code for the server:

6)以下是服务器代码:

GLOBAL.DEBUG = true;
global.inData = '';
var http = require('http');
sys = require("sys");

/* set up DB */

var Db = require('./node-mongodb-native/lib/mongodb').Db,
  Connection = require('./node-mongodb-native/lib/mongodb').Connection,
  Server = require('./node-mongodb-native/lib/mongodb').Server,
  BSON = require('./node-mongodb-native/lib/mongodb').BSONNative;

var host = process.env['MONGO_NODE_DRIVER_HOST'] != null ? process.env['MONGO_NODE_DRIVER_HOST'] : 'localhost';
var port = process.env['MONGO_NODE_DRIVER_PORT'] != null ? process.env['MONGO_NODE_DRIVER_PORT'] : Connection.DEFAULT_PORT;
var db = new Db('test01', new Server(host, port, {}), {native_parser:true});

db.open(function(err, db) { 
    http.createServer(function (req, res) {
        res.writeHead(200, {'Content-Type': 'text/plain'});

        global.inData = {'p':'', 'url':''};

        // get IP address
        var ipAddress = req.connection.remoteAddress;
        global.inData.ip = ipAddress;

        // date time
        var d = new Date();
        var ts = d.valueOf();
        global.inData.ts = ts;

        // get the http query
        var qs = {};
        qs = require('url').parse(req.url, true);
        if (qs.query !== null) {
            for (var key in qs.query) {
                if (key == 'p') {
                    global.inData.p = qs.query[key];
                }
                if (key == 'url') {
                    global.inData.url = qs.query[key];
                }
            }
        }

        if (global.inData.p == '' && global.inData.url == '') {
            res.end("");
        } else {
            db.collection('clickCount', function(err, collection) { 
                if (err) {
                    console.log('is error \n' + err);
                }

                collection.insert({'p':global.inData.p, 
                  'url':global.inData.url,
                  'ip':global.inData.ip, 
                  'ts':global.inData.ts});
                res.end("");
                //db.close();  // DO NOT CLOSE THE CONNECTION
            }); 
        }
    }).listen(8080); 
});

console.log('Server running at whatever host :8080');

This may not be perfect code, but it runs. I'm still not used to the "nested" or LISP kind of coding style. That's why I cheated and used global.inData to pass data along. :)

这可能不是完美的代码,但是它运行。我还不习惯“嵌套”或LISP之类的编程风格。这就是我作弊和使用global的原因。inData传递数据。:)

Don't forget to put res.end("") in the appropriate location (where you think the HTTP request call should be ended).

不要忘记将res.end(“”)放在适当的位置(您认为应该在那里结束HTTP请求调用)。

#2


0  

By the way, the answer I posted above works for CentOS and Fedora.

顺便说一下,我上面发布的答案适用于CentOS和Fedora。

For people who have Ubuntu, here it is:

对于使用Ubuntu的人来说,它是这样的:

# for Gcc
$ sudo apt-get install build-essential

# for SSL
$ sudo apt-get install libssl-dev

Then just install node.js and mongodb as described above.

然后安装节点。如上面所述的js和mongodb。


Also, after few months of development, I find out using "npm", "express" and "mongoose" can bring my life much easier. Also, I have installed other tools, like debugger.

而且,经过几个月的发展,我发现用“npm”、“express”和“mongoose”可以让我的生活更轻松。此外,我还安装了其他工具,如调试器。

# Install Node Package Manager
$ sudo curl http://npmjs.org/install.sh | sh

# for debugging
$ sudo npm install node-inspector

# for Profiling
$ sudo npm install profile

# Install Express, the Node.js framework
$ sudo npm install express

# Install Template Engines (Now, let’s install Jade, jQuery Templates and EJS. You can pick the one you want)
$ sudo npm install jade jqtpl ejs

# XML related, install node-expat and then node-xml2js-expat
$ sudo apt-get install -y libexpat1-dev
$ sudo npm install node-xml2js
$ sudo npm install xml2js-expat

# Install Mongoose, (Mongo Driver)
$ sudo npm install mongoose

Reference: http://npmjs.org

参考:http://npmjs.org

http://expressjs.com

http://expressjs.com

http://mongoosejs.com

http://mongoosejs.com

#3


0  

It looks like there might be a bug. It won't allow me to use a var for the first argument in a:b inside collection.insert({

看起来好像有个bug。它不允许我在a:b内的第一个参数中使用var。

It is treating first agument as 'a' or a, hard coded, either way.

它是把第一次交谈当做“a”或“a”,硬编码,无论哪种方式。

I will look into this and post a fix on github

我将对此进行调查,并在github上发布一个补丁

#1


10  

This was originally posted by the question asker. A mod asked him in the comments to post it as an answer, but got no response. So, I cleaned it up and am posting it myself.

这个问题最初是由提问者发布的。一个国防部在评论中要求他发布这条消息作为回应,但没有得到回应。所以,我把它清理干净,自己贴上去。

When you look at the code, you will notice that the createServer code is inside db.open. It won't work if you reverse it. Also, do not close the db connection. Otherwise, after the first time, the db connection will not be opened again. (Of course, db.open is declared outside of createServer.) I have no clue why createServer is inside db.open. I guess it may have to do with not opening too many db connections?

当您查看代码时,您将注意到createServer代码位于db.open中。如果你把它倒过来,它就不工作了。另外,不要关闭db连接。否则,第一次连接后,将不会再次打开db连接。(当然,db。open是在createServer之外声明的。我不知道为什么createServer在db.open里面。我猜这可能与没有打开太多的db连接有关吧?

Also, one problem I face is that when I run it via SSH, even if I run the server in the background (e.g. $ node server.js &), after 2.5 hours, the server dies (not the instance though). I am not sure if it is because of terminal connection or what.

另外,我面临的一个问题是,当我通过SSH运行它时,即使我在后台运行服务器(例如$ node server)。在2.5小时后,服务器死亡(尽管不是实例)。我不确定是由于终端连接还是什么原因。

Here is the procedure & code

这是程序和代码

Environment: EC2, AMS-Linux-AMI

环境:AMS-Linux-AMI EC2

Purpose: Take an HTTP request and log the query, IP and timestamp into MongoDB.

目的:获取一个HTTP请求并将查询、IP和时间戳记录到MongoDB中。

Steps

步骤

1) After creating the instance (server), install gcc.

创建实例(服务器)后,安装gcc。

$ yum install gcc-c++

2) Download Node.js files and unzip them. (I used version 2.6.)

2)下载节点。js文件并解压它们。(我使用的是版本2.6。)

$ curl -O http://nodejs.org/dist/node-v0.2.6.tar.gz
$ tar -xzf node-v0.2.6.tar.gz

I renamed the unzipped folder to just "nodejs"

我将未压缩文件夹重命名为“nodejs”

$ cd nodejs
$ sudo ./configure --without-ssl
$ sudo make
$ sudo make install

make takes a long while.... After that you can try running the sample in nodejs.org

制作需要很长一段时间....之后,您可以尝试在nodejs.org中运行示例

3) Install MongoDB. I installed version 1.6.5, not 1.7.

3)安装MongoDB。我安装了版本1.6.5,而不是1.7。

$ curl -O http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-1.6.5.tgz
$ tar -xzf mongodb-linux-x86_64-1.6.5.tgz
$ sudo mkdir /data/db/r01/

I renamed the folder to "mongodb"

我将该文件夹重命名为“mongodb”

Run the db process:

db运行过程:

$ ./mongodb/bin/mongod --dbpath /data/db/r01/

Then if you like, you can run and try out the command line. Refer to MongoDB's website.

然后,如果您愿意,您可以运行并尝试命令行。请参阅MongoDB的网站。

4) I recommend that you create your own AIM based on your instance. It will take 20 minutes. Then, recreate the install and run MongoDB again.

4)我建议你根据你的实例创建自己的目标。需要20分钟。然后,重新创建安装并再次运行MongoDB。

5) Install node-mongodb-native

5)安装node-mongodb-native

$ curl -O https://download.github.com/christkv-node-mongodb-native-V0.8.1-91-g54525d8.tar.gz
$ tar -xzf christkv-node-mongodb-native-V0.8.1-91-g54525d8.tar.gz

I renamed the folder to node-mongodb-native

我将该文件夹重命名为node-mongodb-native。

$ cd node-mongodb-native
$ make

6) Here is the code for the server:

6)以下是服务器代码:

GLOBAL.DEBUG = true;
global.inData = '';
var http = require('http');
sys = require("sys");

/* set up DB */

var Db = require('./node-mongodb-native/lib/mongodb').Db,
  Connection = require('./node-mongodb-native/lib/mongodb').Connection,
  Server = require('./node-mongodb-native/lib/mongodb').Server,
  BSON = require('./node-mongodb-native/lib/mongodb').BSONNative;

var host = process.env['MONGO_NODE_DRIVER_HOST'] != null ? process.env['MONGO_NODE_DRIVER_HOST'] : 'localhost';
var port = process.env['MONGO_NODE_DRIVER_PORT'] != null ? process.env['MONGO_NODE_DRIVER_PORT'] : Connection.DEFAULT_PORT;
var db = new Db('test01', new Server(host, port, {}), {native_parser:true});

db.open(function(err, db) { 
    http.createServer(function (req, res) {
        res.writeHead(200, {'Content-Type': 'text/plain'});

        global.inData = {'p':'', 'url':''};

        // get IP address
        var ipAddress = req.connection.remoteAddress;
        global.inData.ip = ipAddress;

        // date time
        var d = new Date();
        var ts = d.valueOf();
        global.inData.ts = ts;

        // get the http query
        var qs = {};
        qs = require('url').parse(req.url, true);
        if (qs.query !== null) {
            for (var key in qs.query) {
                if (key == 'p') {
                    global.inData.p = qs.query[key];
                }
                if (key == 'url') {
                    global.inData.url = qs.query[key];
                }
            }
        }

        if (global.inData.p == '' && global.inData.url == '') {
            res.end("");
        } else {
            db.collection('clickCount', function(err, collection) { 
                if (err) {
                    console.log('is error \n' + err);
                }

                collection.insert({'p':global.inData.p, 
                  'url':global.inData.url,
                  'ip':global.inData.ip, 
                  'ts':global.inData.ts});
                res.end("");
                //db.close();  // DO NOT CLOSE THE CONNECTION
            }); 
        }
    }).listen(8080); 
});

console.log('Server running at whatever host :8080');

This may not be perfect code, but it runs. I'm still not used to the "nested" or LISP kind of coding style. That's why I cheated and used global.inData to pass data along. :)

这可能不是完美的代码,但是它运行。我还不习惯“嵌套”或LISP之类的编程风格。这就是我作弊和使用global的原因。inData传递数据。:)

Don't forget to put res.end("") in the appropriate location (where you think the HTTP request call should be ended).

不要忘记将res.end(“”)放在适当的位置(您认为应该在那里结束HTTP请求调用)。

#2


0  

By the way, the answer I posted above works for CentOS and Fedora.

顺便说一下,我上面发布的答案适用于CentOS和Fedora。

For people who have Ubuntu, here it is:

对于使用Ubuntu的人来说,它是这样的:

# for Gcc
$ sudo apt-get install build-essential

# for SSL
$ sudo apt-get install libssl-dev

Then just install node.js and mongodb as described above.

然后安装节点。如上面所述的js和mongodb。


Also, after few months of development, I find out using "npm", "express" and "mongoose" can bring my life much easier. Also, I have installed other tools, like debugger.

而且,经过几个月的发展,我发现用“npm”、“express”和“mongoose”可以让我的生活更轻松。此外,我还安装了其他工具,如调试器。

# Install Node Package Manager
$ sudo curl http://npmjs.org/install.sh | sh

# for debugging
$ sudo npm install node-inspector

# for Profiling
$ sudo npm install profile

# Install Express, the Node.js framework
$ sudo npm install express

# Install Template Engines (Now, let’s install Jade, jQuery Templates and EJS. You can pick the one you want)
$ sudo npm install jade jqtpl ejs

# XML related, install node-expat and then node-xml2js-expat
$ sudo apt-get install -y libexpat1-dev
$ sudo npm install node-xml2js
$ sudo npm install xml2js-expat

# Install Mongoose, (Mongo Driver)
$ sudo npm install mongoose

Reference: http://npmjs.org

参考:http://npmjs.org

http://expressjs.com

http://expressjs.com

http://mongoosejs.com

http://mongoosejs.com

#3


0  

It looks like there might be a bug. It won't allow me to use a var for the first argument in a:b inside collection.insert({

看起来好像有个bug。它不允许我在a:b内的第一个参数中使用var。

It is treating first agument as 'a' or a, hard coded, either way.

它是把第一次交谈当做“a”或“a”,硬编码,无论哪种方式。

I will look into this and post a fix on github

我将对此进行调查,并在github上发布一个补丁