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

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

问题描述:

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

I still cannot figure out why Im still getting this error message when trying to connect to the MYSQL Server on 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!");
});

node.js中没有问题

There is no prolem in your node.js

node.js只需要安装mysql

node.js require only mysql installation

->在命令提示符下转到nodejs的安装,然后输入

-> go to installation of nodejs in the command prompt and enter

 npm install mysql 

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

in Eclipse: 1) install the nodejs plugin 2) open package.json in the nodejs project and enter

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

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

它将安装mysql并准备连接数据库

it will install the mysql and you are ready to connect the Database

ER_ACCESS_DENIED_ERROR

ER_ACCESS_DENIED_ERROR

1)转到mysql命令提示符

1) go to mysql command prompt

   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了.它会被宽恕

now you can run you JS . it will be conneced