首先要安装mysql模块。 ==>> npm install mysql
安装mysql模块
db.js
var mysql = require("mysql");
// 1:创建一个mysql的Connection对象
// 2.配置数据连接的信息
var connection = mysql.createConnection({
host:"127.0.0.1",
user:"root",
port:3306,
password:"111111",
database:"testdb"
});
// 3.开辟连接
connection.connect();
// 4.执行curd
connection.query("select * from kss_user",function(error,results,fields){
// 如果查询出错,直接抛出
if(error)throw error;
// 查询成功
console.log("results = ",results);
});
// 5.关闭连接
connection.end();