报错信息

本人系统安装的是mysql-installer-community-8.0.18.0.msi这个版本,然后我本地使用node-mysql去连接数据库。

test.js文件

var mysql  = require('mysql');

var connection = mysql.createConnection({
  host     : 'localhost',     //本机地址
  user     : 'root',          //用户
  password : '123456',        //密码
  port: '3306',               //端口号
  database: 'test'            //要连接的数据库
});

connection.connect();

var  sql = 'SELECT * FROM table1';   //查询table1表的所有数据

connection.query(sql,function (err, result) {
        if(err){
          console.log('[SELECT ERROR] - ',err.message);
          return;
        }

       console.log('--------------------------SELECT----------------------------');
       console.log(result);
       console.log('------------------------------------------------------------\n\n');
});

connection.end();

运行node test.js

F:\All Project\test\node>node test.js
[SELECT ERROR] -  ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client

报错[SELECT ERROR] - ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client

解决方案

第一步谷歌
查到了 https://stackoverflow.com/questions/50093144/mysql-8-0-client-does-not-support-authentication-protocol-requested-by-server
这个答案,和我报错的步骤基本一样,按照这个进行操作,登录mysql使用这个

mysql> alter user 'root'@'localhost' identified with mysql_native_password by '123456';
Query OK, 0 rows affected (0.27 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.08 sec)

再次运行

F:\All Project\test\node>node test.js
--------------------------SELECT----------------------------
[ RowDataPacket {
    id: '1',
    name: 'Google',
    url: 'https://www.google.com',
    alexa: '1',
    country: 'USA' },
  RowDataPacket {
    id: '3',
    name: 'Facebook',
    url: 'https://www.facebook.com',
    alexa: '3',
    country: 'USA' },
  RowDataPacket {
    id: '4',
    name: '微博',
    url: 'https://www.weibo.com',
    alexa: '4',
    country: 'CN' } ]
------------------------------------------------------------

成功获得数据。

02-10 21:54