当前位置 : 主页 > 网页制作 > Nodejs >

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

来源:互联网 收集:自由互联 发布时间:2021-06-16
我仍然无法弄清楚为什么我在尝试连接到Node.js上的MYSQL服务器时仍然收到此错误消息 – 错误 – Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'root'@'localhost' (using password: YES)at Handshake.Sequen
我仍然无法弄清楚为什么我在尝试连接到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!");
});
你的node.js中没有问题

node.js只需要安装mysql

– >在命令提示符下安装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了.它会被连接起来

网友评论