sidorares / node-mysql2

:zap: fast mysqljs/mysql compatible mysql driver for node.js
https://sidorares.github.io/node-mysql2/
MIT License
4.07k stars 619 forks source link

too many connection pool #1278

Open craigcosmo opened 3 years ago

craigcosmo commented 3 years ago

pool.js

import mysql from 'mysql2'
import { dbusername, dbpassword, dbname, host } from './config'

const pool = mysql.createPool({
    host: host,
    user: dbusername,
    password: dbpassword,
    database: dbname,
    connectionLimit: 214000,
    waitForConnections: true,
})
export const promisePool = pool.promise()

home-route.js

import { promisePool } from '../pool'

router.post('/', async (request, res, next) => {
    const data = request.body
    const loop = await data.map(async (i, index) => {
        try {
            const row = await promisePool.execute(
                'SELECT * FROM sheet'
            )
        } catch (error) {
            console.log('mysql error', error)
        }
    })

    res.json({ message: 'ok' })
})

export { router as default }

When I do like this, I got error too many connection

EugeneZub commented 3 years ago

I had a such problem too. In my case there was too big connectionLimit value (1000) for the mysql.createPool method but my SQL server was able to proceed with 100 connections only (max_connections variable), so I reduced connectionLimit to 100 and lifted max_connections up to 1000 and it works for me. Maybe it makes sense to play around with the connection_timeout on your DB server.

hdev72 commented 2 years ago

i think you forget closing connection after finish query and connections will stack