node.js解决 Error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol request

node链接mysql报错
error:如下:

Error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client
    at Handshake.Sequence._packetToError (D:\workspace\node\demo1\node_modules\mysql\lib\protocol\sequences\Sequence.js:47:14)
    at Handshake.ErrorPacket (D:\workspace\node\demo1\node_modules\mysql\lib\protocol\sequences\Handshake.js:123:18)
    at Protocol._parsePacket (D:\workspace\node\demo1\node_modules\mysql\lib\protocol\Protocol.js:291:23)
    at Parser._parsePacket (D:\workspace\node\demo1\node_modules\mysql\lib\protocol\Parser.js:433:10)
    at Parser.write (D:\workspace\node\demo1\node_modules\mysql\lib\protocol\Parser.js:43:10)
    at Protocol.write (D:\workspace\node\demo1\node_modules\mysql\lib\protocol\Protocol.js:38:16)
    at Socket.<anonymous> (D:\workspace\node\demo1\node_modules\mysql\lib\Connection.js:91:28)
    at Socket.<anonymous> (D:\workspace\node\demo1\node_modules\mysql\lib\Connection.js:525:10)
    at Socket.emit (events.js:198:13)
    at addChunk (_stream_readable.js:288:12)
    --------------------
    at Protocol._enqueue (D:\workspace\node\demo1\node_modules\mysql\lib\protocol\Protocol.js:144:48)
    at Protocol.handshake (D:\workspace\node\demo1\node_modules\mysql\lib\protocol\Protocol.js:51:23)
    at Connection.connect (D:\workspace\node\demo1\node_modules\mysql\lib\Connection.js:119:18)
    at Object.<anonymous> (D:\workspace\node\demo1\mysqlConnect.js:9:12)
    at Module._compile (internal/modules/cjs/loader.js:776:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)

原因:登录数据库的客户端跟mysql8.0不兼容了,mysql8.0密码认证采用了新的密码格式

解决办法:

在系统mysql终端输入下面命令

//password 是你的数据库账户密码,root和host也是
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
上一篇:PHP类基础(2)之生成对象的6种方法


下一篇:CentOS7.6-搭建SFTP服务