1

I've looked around for a bit, but I haven't found a way to use Node.js to access MySQL. How would I do that without using a side program?

7
  • 3
    see: stackoverflow.com/questions/3878818/node-js-and-mysql-drivers Commented Oct 19, 2011 at 22:15
  • 2
    Why would you want to use mySQL with node.js, it's like one of the worst databases to choose (Only SQL Server would be worse). Commented Oct 19, 2011 at 22:23
  • Then what database type should I use with it? Commented Oct 19, 2011 at 22:39
  • 2
    @Tgwizman There's absolutely nothing wrong with using MySQL with Node.js. There are many other options as well, including PostgreSQL, MongoDB, Redis, and Riak. Commented Oct 19, 2011 at 22:49
  • 1
    i have used mysql on heaps of projects and haven't had any problems, however i must admit i prefer it to sqlserver due to the easier sql syntax in some of the queries i have used. Commented Oct 19, 2011 at 23:52

3 Answers 3

5

The way to go is to

  • download node.js,
  • install mysql server (I had it bundled in wamp)
  • using node npm install mysql driver by felixge
  • connect to mysql from your server .js files

1 download and install node.js

2 Install mysql server (google it)

3 install mysql driver (node-mysql) using node package manager (npm comes with node)

   c:\your_node_server_folder\npm install [email protected]

4 Now, in your server.js file put something like: var PORT = 1983; //Tyear of my birth ;) var restify = require('restify'); var db = require('./mysql_conn');

var options = { serverName: 'Lets meetapp node.js apis', accept: [ 'application/json' ] }

var PORT = 1983;
server.listen(PORT, '0.0.0.0');
console.log("listening "+PORT);
var db = require('./mysql_conn'); 

notice the last line. I am importing the file mysql_conn.js that has the following content:

//Require mysql connector that you installed with npm
var mysql      = require('mysql');

var conn_conf= {
    host     : 'localhost',
    port     :3306,
    user     : 'root',
    password : 'root',
    database: 'mydatabasename'
}

var connection = mysql.createConnection(conn_conf);

connection.connect(function(err) {
    if(err) console.log("Could not connect to DB");
    else{
        console.log("Connected to "+conn_conf.database+' on '+conn_conf.host );
    }
});

The code above will connecto to mysql db that is on the same machine listening the default 3306 port....

And finally a simple query:

connection.query( 'SELECT * FROM mydatabase.mytable ', function(err, rows) {
            console.log("SELECT * FROM mytable ");

            for(var i=0; i<rows.length; i++){
                console.log(rows[i]);
            }

            return rows;

    }

hope it helps!

Sign up to request clarification or add additional context in comments.

1 Comment

As much as I love that this is a valid and accurate answer, I made a comment on the question and I made a note as to the method I use now. If you have any other suggestions, I am very open to using whatever makes everything easier on my part. Thank you!
2

Search http://search.npmjs.org/ (broken link) for mysql (there are several)

Comments

-1

Looking back at this, I really should have simply went this route in the first place. Here's a little lib that I wrote for Node that is really, realy, realy helpful!

var fs = require('fs');
exports.saveJsonToFile = function(filePath, json) {
    fs.writeFileSync(filePath+'.json', JSON.stringify(json, null, " ")
}
exports.readJsonFromFile = function(filePath) {
    return JSON.parse(fs.readFileSync(filePath+'.json'))
}

Just save this to a file, then load the file to your project using:

var JsonFs = require('./Path/To/File');

players = JsonFs.readJsonFromFile('players');

JsonFs.saveJsonToFile('players', players);

PLEASE NOTE: JSON files don't support functions!!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.