For connecting to MySQL with node.js, I've had great success using node-odbc
It's also worked flawlessly for connecting to other databases such as IBM's DB2, and it's been surprisingly fast.
This page is particularly useful for configuring ODBC on linux.
After installing with yum install mysql-connector-odbc
, my /etc/odbc.ini file looks like this:
[MYSQL]
Description = MySQL ODBC Driver
Driver = /usr/lib64/libmyodbc3.so
I left out stuff such as server, user, database, port, password etc. so that I can set these from my connection string (I need to connect to multiple databases).
After saving /etc/odbc.ini, it's installed with this command: odbcinst -i -s -l -f /etc/odbc.ini
And here's a code sample for testing it out:
var odbc = require("odbc");
var db = new odbc.Database();
var conn = "dsn=mysql;server=localhost;user=root;database=mydb;port=3306;password=mypwd;command timeout=30000;";
db.open(conn, function(err) {
if(err) throw err;
var params = ['jiy@stackoverflow.com'];
var qry = "select * users where email = ?";
db.query(qry, params, function(err, rows, def) {
if(err) console.log(err);
console.log(rows);
});
});
Or if you wanted to use coffeescript:
odbc = require "odbc"
db = new odbc.Database()
conn = "dsn=mysql;server=localhost;user=root;database=mydb;port=3306;password=mypwd;command timeout=30000;"
db.open conn, (err) ->
throw err if err
qry = "select * from users where email = ?"
db.query sql, ["jiy@stackoverflow.com"], (err, rows, def) ->
if err? then console.log err else
console.log rows