安装
安装MySQL,MySQL Workbench
到bin目录下启动下列命令,能获得默认密码
mysqld --initialize --console
启动MySQL
mysqld --console
启动MySQL Workbench连接mysql数据库
show databases
node连接mysql
因版本问题,使用的是8.0最新的版本会报错
Client does not support authentication protocol requested by server; consider upgrading MySQL client
解决方案1
USE mysql; ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456'; FLUSH PRIVILEGES;
node连接代码
const mysql = require('mysql') const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: '123456', port: '3306', database: 'myblog' }) connection.connect() var sql = 'SELECT * FROM users' connection.query(sql, (err, res) => { if (err) { console.log(err) return } console.log(res) }) connection.end()
改造成Promise
const exec = sql => { return new Promise((resolve, reject) => { connection.query(sql, (err, res) => { if (err) reject(err) else { resolve(res) } }) }) }