node鏈接Mysql報錯ER_NOT_SUPPORTED_AUTH_MODE

報錯信息

本人系統安裝的是mysql-installer-community-8.0.18.0.msi這個版本,而後我本地使用node-mysql去鏈接數據庫。node

test.js文件mysql

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.jssql

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使用這個ide

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)

再次運行ui

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' } ]
------------------------------------------------------------

成功得到數據。google

相關文章
相關標籤/搜索