Mike Warren Mike Warren - 10 months ago 70
MySQL Question

Connecting to RDS database from node.js

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;

// 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
// 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)
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("Everything successfully completed!");

This is better than first attempt, when I put a
member to the argument passed to
, 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


Neither my example nor the cited GitHub code make use of that
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
table) before copying, pasting, and running this code.


I fixed the issue with
. 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)