node-mysql2 icon indicating copy to clipboard operation
node-mysql2 copied to clipboard

too many connection pool

Open applemate opened this issue 4 years ago • 2 comments

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

applemate avatar Jan 14 '21 20:01 applemate

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.

EugeneZub avatar Feb 26 '21 08:02 EugeneZub

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

hdev72 avatar Feb 25 '22 17:02 hdev72