Node.js与VS Code SQLTools连接MySQL遇ER_NOT_SUPPORTED_AUTH_MODE错误
解决MySQL连接ER_NOT_SUPPORTED_AUTH_MODE错误
问题概述
使用Node.js代码和VS Code的SQLTools连接MySQL时,均触发ER_NOT_SUPPORTED_AUTH_MODE错误。已执行ALTER USER命令修改用户认证方式,但问题未解决。
错误详情
SQLTools报错
ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client
Node.js运行报错日志
Error connecting to the MySQL Database Error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client at Sequence._packetToError (D:\Backend\Server\node_modules\mysql\lib\protocol\sequences\Sequence.js:47:14) at Handshake.ErrorPacket (D:\Backend\Server\node_modules\mysql\lib\protocol\sequences\Handshake.js:123:18) at Protocol._parsePacket (D:\Backend\Server\node_modules\mysql\lib\protocol\Protocol.js:291:23) at Parser._parsePacket (D:\Backend\Server\node_modules\mysql\lib\protocol\Parser.js:433:10) at Parser.write (D:\Backend\Server\node_modules\mysql\lib\protocol\Parser.js:43:10) at Protocol.write (D:\Backend\Server\node_modules\mysql\lib\protocol\Protocol.js:38:16) at Socket.<anonymous> (D:\Backend\Server\node_modules\mysql\lib\Connection.js:88:28) at Socket.<anonymous> (D:\Backend\Server\node_modules\mysql\lib\Connection.js:526:10) at Socket.emit (node:events:537:28) at addChunk (node:internal/streams/readable:324:12) -------------------- at Protocol._enqueue (D:\Backend\Server\node_modules\mysql\lib\protocol\Protocol.js:144:48) at Protocol.handshake (D:\Backend\Server\node_modules\mysql\lib\protocol\Protocol.js:51:23) at Connection.connect (D:\Backend\Server\node_modules\mysql\lib\Connection.js:116:18) at Object.<anonymous> (D:\Backend\Server\serverTest.js:9:12) at Module._compile (node:internal/modules/cjs/loader:1112:14) at Module._extensions..js (node:internal/modules/cjs/loader:1166:10) at Module.load (node:internal/modules/cjs/loader:988:32) at Module._load (node:internal/modules/cjs/loader:834:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12) at node:internal/main/run_main_module:17:47 { code: 'ER_NOT_SUPPORTED_AUTH_MODE', errno: 1251, sqlMessage: 'Client does not support authentication protocol requested by server; consider upgrading MySQL client', sqlState: '08004', fatal: true }
已执行操作
- 将SQL Server路径添加至系统环境变量
- 执行SQL命令修改用户认证方式:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
关联代码(serverTest.js)
const mysql = require('mysql') const connection = mysql.createConnection({ host: 'localhost', user: 'root@localhost', password: '135794ee497531E', }) connection.connect(err => { if (err) { console.log('Error connecting to the MySQL Database') console.log(err) return } console.log('Connection established successfully!') }) connection.end()
解决方案
1. 修正Node.js连接配置的用户字段
代码中user字段错误填写为root@localhost,正确写法应为root(host已指定localhost,用户只需填用户名)。修改后配置:
const connection = mysql.createConnection({ host: 'localhost', user: 'root', // 修正此处 password: '135794ee497531E', })
2. 刷新MySQL权限并重启服务
执行ALTER USER后需刷新权限才能生效:
FLUSH PRIVILEGES;
完成后重启MySQL服务,确保配置更新。
3. 替换为适配新版MySQL的客户端包
你使用的mysql包版本较旧,对MySQL 8.0+的caching_sha2_password兼容性差。建议切换到mysql2包:
npm uninstall mysql npm install mysql2
修改代码导入语句:
const mysql = require('mysql2')
4. 修正SQLTools连接配置
在VS Code的SQLTools连接设置中,确保user字段仅填写root,同时确认认证方式选择mysql_native_password。
内容的提问来源于stack exchange,提问作者Lvasche
相关产品推荐
相关产品推荐

