[英]can't connect to mysql using nodejs
i am new to nodejs ,while creating a connection to the database i got this errors我是 nodejs 的新手,在创建与数据库的连接时出现此错误
C:\\Users\\devashis khandelwal\\node_modules\\mysql\\lib\\protocol\\Parser.js:80 throw err;
C:\\Users\\devashis khandelwal\\node_modules\\mysql\\lib\\protocol\\Parser.js:80 抛出错误; // Rethrow non-MySQL errors ^
// 重新抛出非 MySQL 错误 ^
Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'Root'@'localhost' (using password: YES) at Handshake.Sequence._packetToError (C:\\Users\\devashis khandelwal\\node_modules\\mysql\\lib\\protocol\\sequences\\Sequence.js:52:14) at Handshake.ErrorPacket (C:\\Users\\devashis khandelwal\\node_modules\\mysql\\lib\\protocol\\sequences\\Handshake.js:103:18) at Protocol._parsePacket (C:\\Users\\devashis khandelwal\\node_modules\\mysql\\lib\\protocol\\Protocol.js:279:23) at Parser.write (C:\\Users\\devashis khandelwal\\node_modules\\mysql\\lib\\protocol\\Parser.js:76:12) at Protocol.write (C:\\Users\\devashis khandelwal\\node_modules\\mysql\\lib\\protocol\\Protocol.js:39:16) at Socket.
错误:ER_ACCESS_DENIED_ERROR:在 Handshake.Sequence._packetToError (C:\\Users\\devashis khandelwal\\node_modules\\mysql\\lib\\protocol\\sequences\\Sequence.js: 52:14) at Handshake.ErrorPacket (C:\\Users\\devashis khandelwal\\node_modules\\mysql\\lib\\protocol\\sequences\\Handshake.js:103:18) at Protocol._parsePacket (C:\\Users\\devashis khandelwal\\node_modules\\ mysql\\lib\\protocol\\Protocol.js:279:23) 在 Parser.write (C:\\Users\\devashis khandelwal\\node_modules\\mysql\\lib\\protocol\\Parser.js:76:12) 在 Protocol.write (C: \\Users\\devashis khandelwal\\node_modules\\mysql\\lib\\protocol\\Protocol.js:39:16) 在 Socket。 (C:\\Users\\devashis khandelwal\\node_modules\\mysql\\lib\\Connection.js:103:28) at emitOne (events.js:96:13) 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 (C:\\Users\\devashis khandelwal\\node_modules\\mysql\\lib\\protocol\\Protocol.js:145:48) at Protocol.handshake (C:\\Users\\devashis khandelwal\\node_modules\\mysql\\lib\\protocol\\Protocol.js:52:23) at Connection.connect (C:\\Users\\devashis khandelwal\\node_modules\\mysql\\lib\\Connection.js:130:18) at Object.
(C:\\Users\\devashis khandelwal\\node_modules\\mysql\\lib\\Connection.js:103:28) at emitOne (events.js:96:13) at Socket.emit (events.js:188:7) at readableAddChunk ( _stream_readable.js:176:18) 在 Socket.Readable.push (_stream_readable.js:134:10) -------------------- 在 Protocol._enqueue (C: \\Users\\devashis khandelwal\\node_modules\\mysql\\lib\\protocol\\Protocol.js:145:48) 在 Protocol.handshake (C:\\Users\\devashis khandelwal\\node_modules\\mysql\\lib\\protocol\\Protocol.js:52:23 ) 在 Connection.connect (C:\\Users\\devashis khandelwal\\node_modules\\mysql\\lib\\Connection.js:130:18) 在对象。 (C:\\Users\\devashis khandelwal\\node files\\demo_db_connection.js:9:5) 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) at Function.Module._load (module.js:438:3) at Module.runMain (module.js:604:10)
(C:\\Users\\devashis khandelwal\\node files\\demo_db_connection.js:9:5) 在 Module._compile (module.js:570:32) 在 Object.Module._extensions..js (module.js:579:10) ) at Module.load (module.js:487:32) at tryModuleLoad (module.js:446:12) at Function.Module._load (module.js:438:3) at Module.runMain (module.js:604) :10)
var mysql = require('mysql'); var con = mysql.createConnection({ host : 'localhost', user : 'Root', password : 'my_pass', }); con.connect(function(err) { if (err) throw err; console.log("Connected!"); });
You are connecting with user Root
, I believe it is root
.您正在与用户
Root
连接,我相信它是root
。 Your error is clearly a credentials error.您的错误显然是凭据错误。
Error: ER_ACCESS_DENIED_ERROR: it's occurred because of permission.错误:ER_ACCESS_DENIED_ERROR:这是由于许可而发生的。
As far as I understand your above code you need to change Root to root据我了解您上面的代码,您需要将 Root 更改为 root
var mysql = require('mysql');
var con = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'my_pass',
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
Had a similar issue where I was able to login using the CLI with same creds, but didn't work with mysql2
in nodejs ( mysql-server 8.0
).有一个类似的问题,我可以使用具有相同凭据的 CLI 登录,但无法在 nodejs (
mysql-server 8.0
) 中使用mysql2
。 Solved it by using %
in place of localhost
:通过使用
%
代替localhost
解决了这个问题:
CREATE USER 'user'@'%' IDENTIFIED WITH mysql_native_password BY 'password';
GRANT ALL PRIVILEGES ON db.* TO 'user'@'%';
instead of而不是
CREATE USER 'user'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
GRANT ALL PRIVILEGES ON db.* TO 'user'@'localhost';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.