从node.js连接到RDS数据库 [英] Connecting to RDS database from node.js

查看:296
本文介绍了从node.js连接到RDS数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习node.js,以便实际上可以着手进行我的个人项目.我一直在尝试遵循学习node.js"一书(由Marc Wandschneider撰写)中的示例.此时,我决定放弃练习他的示例,而直接使用他的代码作为我的练习代码的框架.

在我的练习代码中,我要做的就是连接到RDS数据库(不,我没有使用Elastic Beanstalk,顺便说一句),然后输出其中一个表的内容.看起来很简单,但是当我为它编写代码(基于本书)时,它似乎尝试进行连接,但在过程中陷入了困境.这是我的代码:

var pool = require('generic-pool');
var async = require('async');
var mysql = require('mysql');

var host = "<database-name>.cu8hvhstcity.us-west-2.rds.amazonaws.com",
    database = "<database-name>", 
    user = "<username>",
    password = "<someLongPassword>";

var dbClient; 

async.waterfall([
    // 1. establish connection to database
    function (callback) {
        console.log("Connecting to database " + database + "...");
        dbClient = mysql.createConnection({
            host: host,
            database: database,
            user: user,
            password: password,
            port: 3306
        });
        dbClient.connect();
    },
    // 2. select all from a table (let's go for locations)
    function (cb)
    {
        var query = "SELECT * FROM locations"
        console.log("running query \"" + query + "\"...");
        dbClient.query(query, cb);  
    },

    function (rows, fields, callback)
    {
        console.log(fields);
        for (var i = 0; i < rows.length; i++)
        {
            console.log(JSON.stringify(rows, null, '\t'));
        }
    }



], function (err, results) {
    if (err)
    {
        console.log("An error occurred...");
        console.log(err);
    }
    else
    {
        console.log("Everything successfully completed!");
    }       
    dbClient.end();
})

当我将database成员放入传递给mysql.createConnection()的参数时,它比第一次尝试要好,它抱怨数据库未知.在任何情况下都没有发生错误..."或一切都成功完成!"输出到窗口.

是否正在进行任何异步处理,从而导致某种非终止的无限循环或其他后果?我该如何解决?

这本书具有相关的 GitHub页面

注意:

我的示例和引用的GitHub代码均未使用该pool变量,因此可以将其注释掉.在复制,粘贴和运行此代码之前,您自己要做的所有事情就是说npm install asyncnpm install mysql(以及创建指向指向的虚拟RDS数据库,其中包含locations表)./p>

我用database解决了该问题.我意识到数据库的名称使用的是"_",而不是-".相同的问题(代码挂起)仍然存在...

解决方案

我执行了以下操作:

在数组的第二个函数中,我需要两个参数,而不是一个. 我这样修复:function(results, cb) 第三个功能只需要callback(null)

I am trying to learn node.js so that I can actually get started on working on my personal project. I have been trying to follow the examples in the book "Learning node.js" (by Marc Wandschneider). I, at this point, decided to forgo practicing his example, and go straight into using his code as framework for my practice code.

In my practice code, all I am trying to do is connect to my RDS database (no, I am not using Elastic Beanstalk, btw), and output contents of one of the tables. Seems simple enough, but when I whip up the code for it (based on the book), it seems to attempt connection, but get hung up in the process. This is my code:

var pool = require('generic-pool');
var async = require('async');
var mysql = require('mysql');

var host = "<database-name>.cu8hvhstcity.us-west-2.rds.amazonaws.com",
    database = "<database-name>", 
    user = "<username>",
    password = "<someLongPassword>";

var dbClient; 

async.waterfall([
    // 1. establish connection to database
    function (callback) {
        console.log("Connecting to database " + database + "...");
        dbClient = mysql.createConnection({
            host: host,
            database: database,
            user: user,
            password: password,
            port: 3306
        });
        dbClient.connect();
    },
    // 2. select all from a table (let's go for locations)
    function (cb)
    {
        var query = "SELECT * FROM locations"
        console.log("running query \"" + query + "\"...");
        dbClient.query(query, cb);  
    },

    function (rows, fields, callback)
    {
        console.log(fields);
        for (var i = 0; i < rows.length; i++)
        {
            console.log(JSON.stringify(rows, null, '\t'));
        }
    }



], function (err, results) {
    if (err)
    {
        console.log("An error occurred...");
        console.log(err);
    }
    else
    {
        console.log("Everything successfully completed!");
    }       
    dbClient.end();
})

This is better than first attempt, when I put a database member to the argument passed to mysql.createConnection(), and it complained that database was unknown. In neither case did either "An error occurred..." nor "Everything successfully completed!" output to the window.

Is there any async stuff going on that is resulting in some kind of non-terminating infinite loop or something? How do I fix this?

The book has an associated GitHub page

NOTE:

Neither my example nor the cited GitHub code make use of that pool variable, so it can simply be commented out. All you need to do to run this yourself is to say npm install async,npm install mysql (as well as creating a dummy RDS database to point to, that contains locations table) before copying, pasting, and running this code.

EDIT:

I fixed the issue with database. I realized that the name of the database used '_', not '-'. Same issue (code hangs) still persists...

解决方案

I did the following:

In the second function in the array, I needed two parameters, not one. I fixed thus:function(results, cb) The third function simply needed to callback(null)

这篇关于从node.js连接到RDS数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆