1报错:ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client具体代码如下:
import mysql from "mysql"
//连接数据库
const db=mysql.createPool({
host:'127.0.0.1',
user:"root", //一定要搞清楚这里的user之前设置的是哪个
port:3306,
password:"admin123",
database:'test'
})
//测试数据库是否连接成功
db.query('select1',(err,result)=>{
if(err){
console.log("err",err.message)
return
}
console.log("result:",result)
})
解决办法:
1进入mysql安装目录下的bin目录执行cmd,比如我的安装路径是C:\Windows\System32\cmd.exe
?1.启动数据库 net start mysql
??2.登录数据库 mysql -u root -p
此时要输入密码;
? ?3.用数据库
? ? ? ? eg:有 sys,name , age三个数据库你用哪个就需要执行命令,如下
? ? ? ? mysql> use name
? ? 4. alter user 'root'@'localhost' identified with mysql_native_password by '123456';?
? ? ? ? ? //123456为数据库密码(这个要记住)
? ? 5.flush privileges; ?
2 测试连接是否成功时,报错
ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'select1' at line 1
解决办法:
db.query('select 1',(err,result)=>{
if(err){
console.log("err",err.message)
return
}
console.log("result:",result)
})
//注意select 1