0

i am trying to connect to mysql server (host : localhost) through node.js, i am really new to node.js and i have no idea on how to do this, please help!!!

current code which i am using to connect to the server

var client = require('socket.io').listen(8080).sockets,
    mysql = require('mysql');

var connection = mysql.createConnection({
    host : 'localhost',
    user : 'root',
    password : '',
    database : 'testDatabase',
    port : '3306'
});
connection.connect();
var strQuery = 'select * from users';

connection.query( strQuery, function(err, rows){
    if(err){
        throw err;
    }else{
        console.log(rows);
    }
});
connection.end();

which apparently gives the below error

Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'prashanthsun9'@'localhost
' (using password: YES)
    at Handshake.Sequence._packetToError (C:\wamp\www\node_chat\js\node_modules\
mysql\lib\protocol\sequences\Sequence.js:30:14)
    at Handshake.ErrorPacket (C:\wamp\www\node_chat\js\node_modules\mysql\lib\pr
otocol\sequences\Handshake.js:99:18)
    at Protocol._parsePacket (C:\wamp\www\node_chat\js\node_modules\mysql\lib\pr
otocol\Protocol.js:205:24)
    at Parser.write (C:\wamp\www\node_chat\js\node_modules\mysql\lib\protocol\Pa
rser.js:62:12)
    at Protocol.write (C:\wamp\www\node_chat\js\node_modules\mysql\lib\protocol\
Protocol.js:37:16)
    at Socket.<anonymous> (C:\wamp\www\node_chat\js\node_modules\mysql\lib\Conne
ction.js:73:28)
    at Socket.EventEmitter.emit (events.js:95:17)
    at Socket.<anonymous> (_stream_readable.js:746:14)
    at Socket.EventEmitter.emit (events.js:92:17)
    at emitReadable_ (_stream_readable.js:408:10)
    --------------------
    at Protocol._enqueue (C:\wamp\www\node_chat\js\node_modules\mysql\lib\protoc
ol\Protocol.js:110:48)
    at Protocol.handshake (C:\wamp\www\node_chat\js\node_modules\mysql\lib\proto
col\Protocol.js:42:41)
    at Connection.connect (C:\wamp\www\node_chat\js\node_modules\mysql\lib\Conne
ction.js:99:18)
    at Object.<anonymous> (C:\wamp\www\node_chat\js\server.js:11:12)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)

Please help, please help!!!

Prashanth Kumar B
  • 566
  • 10
  • 25
  • 1
    Are you able to connect to that database with that user otherwise, such as with [the `mysql` CLI](http://dev.mysql.com/doc/refman/5.7/en/mysql.html)? Have you [`GRANT`ed the user access to the database](http://stackoverflow.com/questions/1720244/create-new-user-in-mysql-and-give-it-full-access-to-one-database)? – Jonathan Lonowski May 02 '14 at 17:25
  • yup i have granted the permission for that user to access that database i even created new user and database and gave the new user all privileges (basically god mode) nothing works, i dont know if i have to follow some specific set of steps to make this work, for all i know i am felling so dumb right now :( – Prashanth Kumar B May 03 '14 at 09:43

2 Answers2

0

Your connection is not yet setup when you try your query. To debug, change this line:

connection.connect();

to

connection.connect(function(err, conn) {
    if(err) {
         console.log('MySQL connection error: ', err);
         process.exit(1);
    }

});

So you'll get the error trace of why your connection is not ok.

Zlatko
  • 18,936
  • 14
  • 70
  • 123
0

Try to upgrade your npm mysql version to ^2.1.1.

mytharcher
  • 742
  • 7
  • 18