安装mysql
cnpm i mysql -S
创建一个js文件
const mysql = require('mysql');
// 建立数据库连接
const connection = mysql.createConnection({
host: 'localhost', //主机地址 (默认:localhost)
user: 'root', // 数据库用户名
password: '8888', // 数据库莫马
database: 'dom1', // 数据库名
port: 3306 // 端口号 默认(3306)
});
// 连接数据库
connection.connect();
//查询数据库
connection.query('select * from user', (err, res, fil) => {
if (err) throw err;
console.log(res);
});
// 查询完关闭数据库
connection.end();
以上便是node中 mysql 的简单操作了