-2

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?**

Update Solution!: -this problem was because xammp port and MySQL server port are not match. it solved by config the xammp MSQL port in my.ini file to 3306 (default)

Owen Pauling
  • 9,864
  • 18
  • 50
  • 58
patppd
  • 75
  • 1
  • 2
  • 12

2 Answers2

0

Try connecting via the cli. Enter this on the terminal -

mysql -u root -p

Enter your password.

If you see a prompt like mysql> you're connected. If not please check your credentials.

Refer to this question too.

Community
  • 1
  • 1
anshulk
  • 418
  • 5
  • 12
  • After grant all it shows 'mysql>Query ok ..', but when i tried 'node server'. It still occurs the same error code. – patppd Apr 21 '17 at 14:57
0

Solution!: -this problem was because xammp port and MySQL server port are not match. it solved by config the xammp MSQL port in my.ini file to 3306 (default)

patppd
  • 75
  • 1
  • 2
  • 12