Bootstrap

nodejs操作mysql数据库

首先要安装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();

在这里插入图片描述
在这里插入图片描述

;