2017-08-29 9 views
2

我仍然無法弄清楚,爲什麼我還在越來越試圖連接到Node.js的MySQL服務器時,此錯誤消息 -無法連接到MySQL數據庫上的Node.js

錯誤 -

Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'root'@'localhost' (using password: YES) 
at Handshake.Sequence._packetToError (C:\Users\Admin\Desktop\Test Files\Hello World!\node_modules\mysql\lib\protocol\sequences\Sequence.js:52:14) 
at Handshake.ErrorPacket (C:\Users\Admin\Desktop\Test Files\Hello World!\node_modules\mysql\lib\protocol\sequences\Handshake.js:103:18) 
at Protocol._parsePacket (C:\Users\Admin\Desktop\Test Files\Hello World!\node_modules\mysql\lib\protocol\Protocol.js:279:23) 
at Parser.write (C:\Users\Admin\Desktop\Test Files\Hello World!\node_modules\mysql\lib\protocol\Parser.js:76:12) 
at Protocol.write (C:\Users\Admin\Desktop\Test Files\Hello World!\node_modules\mysql\lib\protocol\Protocol.js:39:16) 
at Socket.<anonymous> (C:\Users\Admin\Desktop\Test Files\Hello World!\node_modules\mysql\lib\Connection.js:103:28) 
at emitOne (events.js:115:13) 
at Socket.emit (events.js:210:7) 
at addChunk (_stream_readable.js:266:12) 
at readableAddChunk (_stream_readable.js:253:11) 

JS -

var mysql = require('mysql'); 
var con = mysql.createConnection({ 
    host: 'localhost', 
    user: 'root', 
    password: '*****', 
    database: 'mydb' 
    }); 

con.connect(function(err) { 
    if (err) throw err; 
    console.log("Connected!"); 
}); 
+1

您使用的是XAMPP服務器嗎? – Amogh

+0

@Amogh - 啊!我忘記打開XAMPP,現在我得到一個不同的錯誤信息 - ER_ACCESS_DENIED_ERROR:拒絕用戶'root'@'localhost'(使用密碼:YES)的訪問。什麼導致這個? –

+3

訪問被拒絕通常意味着您使用了錯誤的密碼。 –

回答

0

。在你的Node.js

沒有prolem個

node.js中只需要安裝mysql

- >去安裝的的NodeJS在命令提示符下,輸入

npm install mysql 

在Eclipse: 1)安裝插件的NodeJS 2 )開包。 JSON在項目的NodeJS並進入

 "author": "Krish", 
     "dependencies": { 
       "mysql": "2.x.x" 
     }, 

3) right click on the project run -> node install 

它將安裝mysql和您準備連接數據庫

ER_ACCESS_DENIED_ERROR

1)去到MySQL命令提示符

mysql> create user 'root'@localhost IDENTIFIED BY 'password'; 

2)賦予權限

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT 
     OPTION; 

mysql>SHOW GRANTS FOR 'root'@'localhost'; 

現在可以運行你的JS。它將被連接