无法连接到Node.js上的MySQL数据库

时间:2017-08-29 20:08:07

标签: mysql node.js

我仍然无法弄清楚为什么我在尝试连接到Node.js上的MYSQL服务器时仍然收到此错误消息 -

错误 -

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!");
});

2 个答案:

答案 0 :(得分:0)

你的node.js中没有问题

node.js只需要安装mysql

- &GT;在命令提示符下安装nodejs并输入

 npm install mysql 
Eclipse中的

:   1)安装nodejs插件   2)在nodejs项目中打开package.json并输入

       "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了。它将被连接

答案 1 :(得分:0)

如果您在xampp中托管SQL,则localhost中的root用户没有任何密码,因此请删除密码。

对我有用。