我正在运行一个简单的Nodejs(v10.15.3)应用程序,试图访问MySQL服务器(8.0.15)。 npm mysql软件包版本为v2.17.1。
该脚本如下所示,
var mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
user: 'test',
password: '2222',
database: 'database',
debug: true
});
connection.connect(function(err) {
if (err) {
console.log(err.stack);
throw err;
}
console.log("Connected!");
});
奇怪的是,我能够使用相同的凭据从MySQL客户端登录。
我尝试了其他论坛中提到的几乎所有方法。我尝试过的一些事情
我已将用户测试的密码插件类型从
caching_sha2_password
更改为mysql_native_password
。更新了用户密码并执行了刷新特权。
附加整个堆栈跟踪。
<-- HandshakeInitializationPacket {
protocolVersion: 10,
serverVersion: '8.0.13',
threadId: 71,
scrambleBuff1: <Buffer 16 5c 10 12 68 49 73 68>,
filler1: <Buffer 00>,
serverCapabilities1: 65535,
serverLanguage: 33,
serverStatus: 2,
serverCapabilities2: 50175,
scrambleLength: 21,
filler2: <Buffer 00 00 00 00 00 00 00 00 00 00>,
scrambleBuff2: <Buffer 41 06 27 5a 0c 4c 4c 30 2f 72 0d 7e>,
filler3: <Buffer 00>,
pluginData: 'mysql_native_password',
protocol41: true }
--> (71) ClientAuthenticationPacket {
clientFlags: 455631,
maxPacketSize: 0,
charsetNumber: 33,
filler: undefined,
user: 'test',
scrambleBuff:
<Buffer fa ee eb f2 89 a4 f0 91 a9 d1 aa f2 77 08 76 bb ec a2 51 8b>,
database: 'simplify_meetup',
protocol41: true }
<-- (71) ErrorPacket {
fieldCount: 255,
errno: 1045,
sqlStateMarker: '#',
sqlState: '28000',
message:
'Access denied for user \'test\'@\'localhost\' (using password: YES)' }
Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'test'@'localhost' (using password: YES)
at Handshake.Sequence._packetToError (D:\Development\simplify-meetup\node_modules\mysql\lib\protocol\sequences\Sequence.js:47:14)
at Handshake.ErrorPacket (D:\Development\simplify-meetup\node_modules\mysql\lib\protocol\sequences\Handshake.js:123:18)
at Protocol._parsePacket (D:\Development\simplify-meetup\node_modules\mysql\lib\protocol\Protocol.js:291:23)
at Parser._parsePacket (D:\Development\simplify-meetup\node_modules\mysql\lib\protocol\Parser.js:433:10)
at Parser.write (D:\Development\simplify-meetup\node_modules\mysql\lib\protocol\Parser.js:43:10)
at Protocol.write (D:\Development\simplify-meetup\node_modules\mysql\lib\protocol\Protocol.js:38:16)
at Socket.<anonymous> (D:\Development\simplify-meetup\node_modules\mysql\lib\Connection.js:91:28)
at Socket.<anonymous> (D:\Development\simplify-meetup\node_modules\mysql\lib\Connection.js:525:10)
at Socket.emit (events.js:189:13)
at addChunk (_stream_readable.js:284:12)
--------------------
at Protocol._enqueue (D:\Development\simplify-meetup\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (D:\Development\simplify-meetup\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at Connection.connect (D:\Development\simplify-meetup\node_modules\mysql\lib\Connection.js:119:18)
at D:\Development\simplify-meetup\routes\EventReminderAPI.js:20:16
at Layer.handle [as handle_request] (D:\Development\simplify-meetup\node_modules\express\lib\router\layer.js:95:5)
at next (D:\Development\simplify-meetup\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (D:\Development\simplify-meetup\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (D:\Development\simplify-meetup\node_modules\express\lib\router\layer.js:95:5)
at D:\Development\simplify-meetup\node_modules\express\lib\router\index.js:281:22
at Function.process_params (D:\Development\simplify-meetup\node_modules\express\lib\router\index.js:335:12)
最佳答案
验证测试用户是否有权访问数据库;
在mysql shell中运行
SHOW GRANTS FOR 'test'@'localhost';
这将使您看到用户有权访问的所有数据库。