koa2 integration mysql

Mysql package introduced

npm install mysql

Mysql package

Create a file on mysql.js utils (Kit) in
Use pool connection pool
mysql.js
//封装mysql
const mysql = require('mysql')
let pools = {}//连接池
let query = (sql, callback, host = '127.0.0.1') => {
    if (!pools.hasOwnProperty(host)) {//是否存在连接池
        pools[host] = mysql.createPool({//不存在创建
            host: host,
            port: '3306',
            user: 'root',
            password: '',
            database: ''//数据库名
        })
    }
    pools[host].getConnection((err, connection) => {//初始化连接池
        connection.query(sql, (err, results) => {//去数据库查询数据
            callback(err, results)//结果回调
            connection.release()//释放连接资源 | 跟 connection.destroy() 不同,它是销毁
        })
    })
}

module.exports = query

Introduced mysql.js

In the app.js
/*
 通过一个中间件,把所有的工具关联起来
*/
app.use(async (ctx, next) => {
  //挂载到util中
  ctx.util = {
    mysql: require('./utils/mysql')
  }
  await next()
})

use

// 操作数据库
ctx.util.mysql('select * from test', function (err, results) {
    console.log(results);//返回的数据
    console.log(results[0].id);//获取返回数据中的id([0]代表取第一条中的id)
})

Do not use connection pooling

const mysql = require('mysql')

// 连接 mysql 服务器
const connection = mysql.createConnection({
    host: '127.0.0.1',
    port: '3306',
    user: 'root',
    password: '',
    database:''
})

// 执行SQL
connection.query(sql, function (err, result) {
  err // 错误信息
  result // 结果
})

// 销毁连接 | 由于 JS 是异步的,所以当前代码会在执行 SQL 之前就销毁了连接
connection.destroy()

end

Mysql create a connection createConnection, Each time a new connection is connection.query will cause a great waste of resources, reduce performance.
Connection pooling is another method of performing, it created a number of one-time connection, and then according to the client's queries, automatic distribution, reuse, manage these connections.
References to https://www.jmjc.tech/less/113

Mysql package introduced

npm install mysql

Mysql package

Create a file on mysql.js utils (Kit) in
Use pool connection pool
mysql.js
//封装mysql
const mysql = require('mysql')
let pools = {}//连接池
let query = (sql, callback, host = '127.0.0.1') => {
    if (!pools.hasOwnProperty(host)) {//是否存在连接池
        pools[host] = mysql.createPool({//不存在创建
            host: host,
            port: '3306',
            user: 'root',
            password: '',
            database: ''//数据库名
        })
    }
    pools[host].getConnection((err, connection) => {//初始化连接池
        connection.query(sql, (err, results) => {//去数据库查询数据
            callback(err, results)//结果回调
            connection.release()//释放连接资源 | 跟 connection.destroy() 不同,它是销毁
        })
    })
}

module.exports = query

Introduced mysql.js

In the app.js
/*
 通过一个中间件,把所有的工具关联起来
*/
app.use(async (ctx, next) => {
  //挂载到util中
  ctx.util = {
    mysql: require('./utils/mysql')
  }
  await next()
})

use

// 操作数据库
ctx.util.mysql('select * from test', function (err, results) {
    console.log(results);//返回的数据
    console.log(results[0].id);//获取返回数据中的id([0]代表取第一条中的id)
})

Do not use connection pooling

const mysql = require('mysql')

// 连接 mysql 服务器
const connection = mysql.createConnection({
    host: '127.0.0.1',
    port: '3306',
    user: 'root',
    password: '',
    database:''
})

// 执行SQL
connection.query(sql, function (err, result) {
  err // 错误信息
  result // 结果
})

// 销毁连接 | 由于 JS 是异步的,所以当前代码会在执行 SQL 之前就销毁了连接
connection.destroy()

end

Mysql create a connection createConnection, Each time a new connection is connection.query will cause a great waste of resources, reduce performance.
Connection pooling is another method of performing, it created a number of one-time connection, and then according to the client's queries, automatic distribution, reuse, manage these connections.
References to https://www.jmjc.tech/less/113

double eleven? It does not exist, only overtime makes me happy, smile .ing

Guess you like

Origin www.cnblogs.com/chendada/p/11837157.html