• node连接Mysql报错ER_NOT_SUPPORTED_AUTH_MODE


    报错信息

    本人系统安装的是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('------------------------------------------------------------
    
    ');  
    });
     
    connection.end();

    运行node test.js

    F:All Project	est
    ode>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

    报错原因

    mysql8.0以上加密方式,Node还不支持。

    解决方案

    第一步谷歌
    查到了 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	est
    ode>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' } ]
    ------------------------------------------------------------

    成功获得数据。

  • 相关阅读:
    javascript获得元素的尺寸和位置二 : clientWidth/Height、scrollWidth/Height、scrollTop/Left
    CSS中的Position属性
    JavaScript沙箱SandBox设计模式
    JavaScript中为事件句柄绑定监听函数
    MatrixTransform矩阵变换
    getBoundingClientRect计算页面元素的offsetLeft、offsetTop
    PHP Warning: date() [function.date]: It is not safe to rely on the system's timezone
    CollectGarbage函数JS清理垃圾,内存释放
    (转)python路径操作新标准:pathlib 模块
    (转)sql函数总结(更新中... ...)
  • 原文地址:https://www.cnblogs.com/jing-tian/p/11688073.html
Copyright © 2020-2023  润新知