0

I'm trying to create a mysql database to node.js server. I've installed mysql module through command prompt:

npm install mysql

Then I execute the following code:

var Client = require('mysql').Client;
console.log(Client);

Console display undefined. That is, Client is undefined. Please tell me why it is undefined?

I'm following this tutorial http://utahjs.com/2010/09/22/nodejs-and-mysql-introduction/

Rodrigo Medeiros
  • 7,814
  • 4
  • 43
  • 54
Rejaul
  • 831
  • 1
  • 12
  • 35
  • Are you running `node` in the same directory your did `npm install` in? – apsillers Jun 30 '14 at 17:17
  • Actually When run var Client = require('mysql').Client; console.log(Client); console display undefined – Rejaul Jun 30 '14 at 17:22
  • @user3782480 So it would be nice to update your question to reflect that. – Rodrigo Medeiros Jun 30 '14 at 17:30
  • 1
    I noticed when I run var Client = require('mysql');console.log(Client); Console displays all method inside Client variable but not Client method. – Rejaul Jun 30 '14 at 17:34
  • @user3782480 That information is useful too. You should put it in your question. And that's why I told in my answer below that the tutorial you're following is a bit old. – Rodrigo Medeiros Jun 30 '14 at 17:43
  • @RodrigoMedeiros I updated the code as you suggested but still not connecting. I used host, user, password value as your suggestion. Console display: ERROR: connect ECONNREFUSED and so on.... – Rejaul Jun 30 '14 at 17:57
  • @user3782480 Ok, fine. But now the error has changed, which means the first problem was solved. I think now you could take a look in some answers for that `ECONNREFUSED` problem [here](http://stackoverflow.com/questions/8825342/connect-econnrefused-node-js-sql), [here](http://stackoverflow.com/questions/19614714/error-connect-econnrefused-while-accessing-mysql) or [here](http://stackoverflow.com/questions/21206801/node-js-mysql-error-econnrefused) – Rodrigo Medeiros Jun 30 '14 at 18:04

2 Answers2

2

Maybe the the tutorial is a little bit old. Just use the instruction on the node-mysql docs:

var mysql = require('mysql');
var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'me',
  password : 'secret'
});

connection.connect();

And you should be able to connect to your MySQL database.

Rodrigo Medeiros
  • 7,814
  • 4
  • 43
  • 54
0

The node js APIs having been changing updating a lot in recent past, so it is highly possible that the tutorial you have been following is out of date according to the version you are using. You can follow the code example here I am updating or you may refer to something else, the only part that matters is it should work at minimum cost.

    var mysql = require('mysql');

    app.use( connection(mysql, {
        host: 'myhost',
        user: 'user_name',
        password: 'password',
        port: 3306,          //port mysql
        database: 'database_name',
        multipleStatements: 'true'  //false by default

    }, 'pool'));

    req.getConnection(function(err, connection) {
        connection.query("SELECT * FROM `table_name`;",function (error,row){
            if(!error){
                           //do something.....
            }
            else console.log("Error : "+err);
            });
        //do something else...
    });

Thank you...!

Abhishek Dhanraj Shahdeo
  • 1,356
  • 2
  • 14
  • 35