Search code examples
node.jsnode-mysql

node.js mysql client undefined


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/


Solution

  • 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.