NodeJS/mySQL - ER_ACCESS_DENIED_ERROR 用户“root"@“localhost"的访问被拒绝(使用密码:YES)
我正在尝试通过 NodeJS 文件连接到 mySQL,但收到以下错误:
I am attempting to connect to mySQL through a NodeJS file, but I receive the following error:
{ 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 代码如下:
The weird thing is that I can connect fine through the terminal by running mysql -u root -p
. I only get this error when running my javascript. I have been all over Google and StackOverflow, but still have not found a solution that works. I am using MySQL 5.7.16 on Ubuntu 16.04.1 on a VIRTUAL MACHINE. Not sure if a VM makes a difference here. My Javascript code is below:
'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'; 看到这一点.
I have tried using 'locahost' as well as '127.0.0.1' in my javascript. I have a 'root' user for both 'localhost' and '127.0.0.1' in mySql.user table and I am able to see this by executing SELECT user, host FROM mysql.user WHERE user='root';
我通过执行此操作为root"用户添加了权限:
I have added privileges to 'root' user by executing this:
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY 'password';
我也在 127.0.0.1 上运行了上面的程序.我也试过这个:
I ran the above on 127.0.0.1 as well. I have also tried this:
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION
我尝试像这样重置 root 密码:https://help.ubuntu.com/community/MysqlPasswordReset
I have attempted to reset the root password like this: https://help.ubuntu.com/community/MysqlPasswordReset
我在每次尝试后都运行了 FLUSH PRIVILEGES
.我已经停止并重新启动了 mySQL.我已经完全卸载了 mySQL 并重新安装了.
I have run FLUSH PRIVILEGES
after each attempt. I've stopped and restarted mySQL. I have uninstalled mySQL completely and reinstalled.
一切都无济于事.每次尝试运行 javascript 时,我都会收到访问被拒绝错误,但是当我通过终端连接到 mySQL 时绝对没有问题.
All to no avail. I receive the access denied error every time I try to run the javascript, but I have absolutely no issues when I connect to mySQL via the terminal.
有什么想法吗?
推荐答案
我也遇到了同样的问题,我把密码改成空字符串解决了.
I have the same problem, I solved it by changing the password to empty string.
var mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: ''
});
相关文章