npm i mysql2
const mysql = require('mysql2')
const mysqlMode = mysql.createPool({
host: '127.0.0.1', //服务端host
user: 'root', //用户名称,mysql一般默认root
password: '123456', //密码
database: 'sqlTest1', //数据库名字
waitForConnections: true, //超过最大连接数是否等待
connectionLimit: 10, //最大连接个数
queueLimit: 2 //等待队列最大限制
})
module.exports = mysqlMode
var express = require('express');
var router = express.Router();
const mysqlMode = require('../db/mysql')
/* GET home page. */
router.get('/', function (req, res, next) {
mysqlMode.query("SELECT * FROM `test01`", (error, results, fields) => {
if (error) {
console.error(error)
return
}
// console.log(results)
res.send(results)
})
});
module.exports = router;
连接成功!