Koa 连接 mysql 发表于 2019-02-22 | 更新于: 2023-04-28 mysql.config.js 123456789101112131415161718192021222324const mysql = require('mysql');// 创建 mysql 连接池资源var pool = mysql.createPool({ host: 'localhost', user: 'root', password: 'mima', database: 'db'});module.exports = function (sql, value) { return new Promise((resolve, reject) => { pool.getConnection(function (err, connection) { if (err) reject(false); else connection.query(sql, value, (error, results, fields) => { //将链接返回到连接池中,准备由其他人重复使用 connection.release(); if (error) reject(false); else resolve(results) }); }); })} 使用 1234async function xxx () { const s = await query("sql", value) s ? '操作成功': '操作失败' }