我正在尝试通过NodeJS文件连接到mySQL,但是收到以下错误:
{ Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'root'@'localhost' (using password: YES) at Handshake.Sequence._packetToError (/home/matthew/Node/mySqlTest/node_modules/mysql/lib/protocol/sequences/Sequence.js:30:14) at Handshake.ErrorPacket (/home/matthew/Node/mySqlTest/node_modules/mysql/lib/protocol/sequences/Handshake.js:67:18) at Protocol._parsePacket (/home/matthew/Node/mySqlTest/node_modules/mysql/lib/protocol/Protocol.js:197:24) at Parser.write (/home/matthew/Node/mySqlTest/node_modules/mysql/lib/protocol/Parser.js:62:12) at Protocol.write (/home/matthew/Node/mySqlTest/node_modules/mysql/lib/protocol/Protocol.js:37:16) at Socket.ondata (_stream_readable.js:555:20) at emitOne (events.js:101:20) 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 (/home/matthew/Node/mySqlTest/node_modules/mysql/lib/protocol/Protocol.js:110:26) at Protocol.handshake (/home/matthew/Node/mySqlTest/node_modules/mysql/lib/protocol/Protocol.js:42:41) at Connection.connect (/home/matthew/Node/mySqlTest/node_modules/mysql/lib/Connection.js:81:18) at Connection._implyConnect (/home/matthew/Node/mySqlTest/node_modules/mysql/lib/Connection.js:222:10) at Connection.query (/home/matthew/Node/mySqlTest/node_modules/mysql/lib/Connection.js:137:8) at Object.<anonymous> (/home/matthew/Node/mySqlTest/index.js:11: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) code: 'ER_ACCESS_DENIED_ERROR', errno: 1045, sqlState: '28000', fatal: true }
奇怪的是,我可以通过运行终端通过终端连接mysql -u root -p。我只在运行JavaScript时收到此错误。我已经遍历了Google和StackOverflow,但仍然找不到有效的解决方案。我在虚拟机上的Ubuntu 16.04.1上使用MySQL 5.7.16。不知道VM是否在这里有所作为。我的Javascript代码如下:
mysql -u root -p
'use strict'; var mysql = require('mysql'); var connection = mysql.createConnection({ host: 'localhost', user: 'root', password: 'password' }); connection.query( 'SELECT "foo" AS first_field, "bar" AS second_field', function(err, results, fields) { console.log(err); console.log(results); connection.end(); } );
我已经尝试在JavaScript中使用“ locahost”以及“ 127.0.0.1”。我在mySql.user表中同时具有’localhost’和‘127.0.0.1’的’root’用户,我可以通过执行以下命令来查看SELECT user, host FROM mysql.user WHERE user='root';
SELECT user, host FROM mysql.user WHERE user='root';
我通过执行以下命令为“ root”用户添加了特权:
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY 'password';
我也在127.0.0.1上运行了上述代码。我也尝试过这个:
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION
我试图像这样重置根密码:https : //help.ubuntu.com/community/MysqlPasswordReset
FLUSH PRIVILEGES每次尝试后我都已奔跑。我已经停止并重新启动了mySQL。我已经完全卸载了mySQL并重新安装。
FLUSH PRIVILEGES
一切都无济于事。每次尝试运行javascript时,我都会收到拒绝访问错误,但是通过终端连接到mySQL时,我绝对没有问题。
有任何想法吗?
在 package.json中 使用最新的MySQL版本可以解决此问题。
我正在使用2.0.0版。我将版本更改为2.10.2。