nodejs mysql关闭连接,nodejs连接mySQL数据库连接失败问题

正常写的nodejs代码,user和password都是对的,但是就是连接不上。

const mysql = require('mysql')

// 创建链接对象

const connection = mysql.createConnection({

host: '127.0.0.1',

user: 'root',

password: '123456',

port: '3306',

database: 'myblog',

insecureAuth: true

})

// 开始连接

connection.connect()

// 执行 sql 语句

const sql = 'select * from blogs;'

connection.query(sql, (err, result) => {

if(err) {

console.error('error connecting: ' + err.stack);

return

}

console.log(connection.threadId)

})

// 关闭连接

connection.end()

报错提示:

报错提示.jpg

客户端不支持服务器请求的身份验证协议;请考虑升级MySQL客户端。于是我就将MySQL升级为最新版本。还是提示这个报错。

后来google查询到一种解决方案,在 workbench输入:

alter user 'root'@'localhost' identified with mysql_native_password by '123456';

执行这句代码,再次连接数据库成功了。

你可能感兴趣的:(nodejs,mysql关闭连接)