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?

Tgwizman
  • 1,469
  • 2
  • 19
  • 34
  • 3
    see: http://stackoverflow.com/questions/3878818/node-js-and-mysql-drivers – therin Oct 19 '11 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). – Raynos Oct 19 '11 at 22:23
  • Then what database type should I use with it? – Tgwizman Oct 19 '11 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. – Chris F Oct 19 '11 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. – bumperbox Oct 19 '11 at 23:52
  • Meh, MongoDB, CouchDB and Redis are very popular among node. I would only use mySQL for legacy database support. – Raynos Oct 20 '11 at 09:59
  • I would like to revisit this question. I am now using JSON as my key/value storage method. I simply load the text file with the file system and parse it with the `JSON.parse` method. I save my data in a similar fashion. I use `JSON.stringify` to make it plain text and then use the file system again to store the data straight to a text file for later use. It is what I would consider a perfect system (as long as I don't use circular referencing; which I never do). – Tgwizman May 04 '18 at 03:59

3 Answers3

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 mysql@2.0.0-alpha8

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!

luigi7up
  • 5,779
  • 2
  • 48
  • 58
  • 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! – Tgwizman May 04 '18 at 04:01
2

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

holex
  • 23,961
  • 7
  • 62
  • 76
chovy
  • 72,281
  • 52
  • 227
  • 295
-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!!!

Tgwizman
  • 1,469
  • 2
  • 19
  • 34