node-mysql2
node-mysql2 copied to clipboard
too many connection pool
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
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.
i think you forget closing connection after finish query and connections will stack