mysql的使用

mysql

  • 安装
1
npm i mysql --save
  • 配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
const mysql = require('mysql');

// 连接数据库配置
const config = {
host: 'localhost',
port: '3306',
user: 'root',
password: '123456',
database: 'test'
}

// 创建数据库连接池
module.exports = {
sqlConnection: (sql,sqlArr,callBack) => {
const pool = mysql.createPool(config);
pool.getConnection((err,conn) => {
if(err) {
console.log(err);
return;
}
// 事件驱动回调
conn.query(sql,sqlArr,callBack);
// 关闭连接池
conn.release();
})
}
}

Comments