宿命论是那些缺乏意志力的弱者的借口。——罗曼·罗兰

今天试了试使用js连接数据库,发现报错Client does not support authentication protocol requested by server; consider upgrading MySQL client

乍一看,代码好像没问题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
var mysql = require("mysql");

let connection = mysql.createConnection({
host: "127.0.0.1",
port: 3306,
user: "root",
password: "789456",
database: "ruben"
});

connection.connect();

connection.query("select * from user", function (error, results, fields) {
if (error) throw error;
console.log(results);
console.log(fields);
});


connection.end();

但我的mysql版本是8.0.22

image-20210127194358584

所以需要执行以下sql

1
2
alter user 'root'@'localhost' identified with mysql_native_password by '789456';
flush privileges;

然后就可以正常执行了

image-20210127194735219