Thursday, 20 April 2017

Nodejs cannot connect to MySQL using express

I'm trying to connect my project to MySQL with XAMMP for a Login page.

I can't connect to MySQL

My code:

var mysql=require('mysql');
var connection=mysql.createConnection({
  host:'127.0.0.1',
  port:'3307',
  user:'root',
  password:'1234',
  database:'db'
});

connection.connect(function(error){
  if(!!error){
    console.log(error);
  }else{
    console.log('Connected!:)');
  }
})

Error After run node server

{ Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'root'@'localhost' (using password: YES)
    at Handshake.Sequence._packetToError (D:\db\database\node_modules\mysql\lib\protocol\sequences\Sequence.js:52:14)
    at Handshake.ErrorPacket (D:\db\database\node_modules\mysql\lib\protocol\sequences\Handshake.js:103:18)
    at Protocol._parsePacket (D:\db\database\node_modules\mysql\lib\protocol\Protocol.js:280:23)
    at Parser.write (D:\db\database\node_modules\mysql\lib\protocol\Parser.js:75:12)
    at Protocol.write (D:\db\database\node_modules\mysql\lib\protocol\Protocol.js:39:16)
    at Socket.<anonymous> (D:\db\database\node_modules\mysql\lib\Connection.js:103:28)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:176:18)
    at Socket.Readable.push (_stream_readable.js:134:10)
    --------------------
    at Protocol._enqueue (D:\db\database\node_modules\mysql\lib\protocol\Protocol.js:141:48)
    at Protocol.handshake (D:\db\database\node_modules\mysql\lib\protocol\Protocol.js:52:41)
    at Connection.connect (D:\db\database\node_modules\mysql\lib\Connection.js:130:18)
    at Object.<anonymous> (D:\db\database\config\express.js:15:12)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
  code: 'ER_ACCESS_DENIED_ERROR',
  errno: 1045,
  sqlState: '28000',
  fatal: true }

** And I would like to know how can I do insert or select from database in another js file in the project. Can I wirte it at controller file?**



via C A L O J Y

No comments:

Post a Comment