将不同数据库的mysql池连接生成到同一api节点应用程序

将不同数据库的mysql池连接生成到同一api节点应用程序,mysql,node.js,Mysql,Node.js,我有一个带有mysql的NodeJSAPI应用程序 当用户从api请求数据时,我需要设置不同的数据库池,因为每个用户可以有不同的数据库 我使用请求中的主机头作为键来查找用户的数据库配置 const mysql = require("mysql2/promise"); const NodeCache = require("node-cache"); const mycache = new NodeCache(); const getDB = async (

我有一个带有mysql的NodeJSAPI应用程序

当用户从api请求数据时,我需要设置不同的数据库池,因为每个用户可以有不同的数据库

我使用请求中的主机头作为键来查找用户的数据库配置

const mysql = require("mysql2/promise");
const NodeCache = require("node-cache");
const mycache = new NodeCache();

const getDB = async (host) => {
    const config = await getConnectionConfig();
    return config;
};

const poolTenancy = async (host) => {
  let pool = mycache.get(host);
  if (pool == undefined) {
    let db;
    try {
      db = await getDB(host);
    } catch (error) {
      console.log(error);
      throw new Error("No DB found");
    }

    if (db) {
      const confPool = {
        host: db.host,
        user: db.user,
        password: db.password,
        database: db.database,
        waitForConnections: true,
        connectionLimit: 10,
        queueLimit: 0,
      };
      pool = mycache.get(host)
      if (pool == undefined) {
        pool = mysql.createPool(confPool);
        mycache.set(host, pool);
        pool.on('release', () => console.log('pool => relased connection')); 
        process.on('SIGINT', () => 
            pool.end(err => {
                if(err) return console.log(err);
                console.log('pool => closed');
                process.exit(0);
            })
        ); 
      }
      return pool;
    } else throw new Error("Host not defined:" + host);
  } else {
    console.log("Get old pool from cache with key:", host);
    return pool;
  }
};
module.exports={getPoolTenancy: poolTenancy}
我正在使用节点缓存来缓存池

我的问题是,我在mysql workbench上看到很多连接,更多的是同一个数据库的连接数限制为10。他们中的许多人正在睡觉。对同一数据库不遵守十(10)个连接限制

有没有更好的解决办法

在我的路线中,我执行下一步:

//路线样本

const express = require("express");
const router = express.Router();
const poolTenancy = require("../database/db").getPoolTenancy;

let pool;

router.use(async (req, res, next) => {
  try {
    pool = await poolTenancy(req.headers.host);
    return next();
  } catch (error) {
    res.status(400).send({ message: "pool não achado" });
  }
});

router.get("/list", async (req, res) => {
  const sql = `select  A.* from cl_conselho_profissional A  `;
  try {
    const resp = await pool.query(sql);
    return res.send({ data: resp[0] });
  } catch (e) {
    console.log(e);
    return res.status(400).send("Um erro ocorreu selecionando conselho");
  }
});
....
//这里是我生成池的地方 //db.js //将池设置为用户的数据库

const mysql = require("mysql2/promise");
const NodeCache = require("node-cache");
const mycache = new NodeCache();

const getDB = async (host) => {
    const config = await getConnectionConfig();
    return config;
};

const poolTenancy = async (host) => {
  let pool = mycache.get(host);
  if (pool == undefined) {
    let db;
    try {
      db = await getDB(host);
    } catch (error) {
      console.log(error);
      throw new Error("No DB found");
    }

    if (db) {
      const confPool = {
        host: db.host,
        user: db.user,
        password: db.password,
        database: db.database,
        waitForConnections: true,
        connectionLimit: 10,
        queueLimit: 0,
      };
      pool = mycache.get(host)
      if (pool == undefined) {
        pool = mysql.createPool(confPool);
        mycache.set(host, pool);
        pool.on('release', () => console.log('pool => relased connection')); 
        process.on('SIGINT', () => 
            pool.end(err => {
                if(err) return console.log(err);
                console.log('pool => closed');
                process.exit(0);
            })
        ); 
      }
      return pool;
    } else throw new Error("Host not defined:" + host);
  } else {
    console.log("Get old pool from cache with key:", host);
    return pool;
  }
};
module.exports={getPoolTenancy: poolTenancy}