简体   繁体   中英

How do I avoid max connections error in mysql?

This happens pretty frequently (once a week for about 30-40 minutes), where all of a sudden my database mentions max connections when I try to connect via heidisql, and any apis calls respond with the following error:

Cannot read property 'release' of undefined

I am calling .release() after every query in mysql. Is there something I am missing, am I suppose to call .end as well? I am using nodejs with mysql.

Here is the way I wrap every query and the pool code:

var mysql = require('mysql');

var mysql_pool = mysql.createPool({
    connectionLimit: config.mysql.limit,
    host: config.mysql.host,
    user: config.mysql.user,
    password: config.mysql.pw,
    database: config.mysql.db //,
    // debug: true
});
var qSelect = "SELECT id FROM Users";
    var qValues = [];
    var qCall = mysql.format(qSelect, qValues);
    mysql_pool.getConnection(function(err_pool, connection) {
        if (err_pool) {
            connection.release();
            console.log(' Error getting mysql_pool connection: ' + err_pool);
            throw err_pool;
        }
        connection.query(qCall, function(err, userFound, fields) {
            connection.release();
            if (err) {
                console.log("get user : " + err);
            } else {
               //some code here
            }

        });

Can someone please advise, appreciate it.

You should remove first connection.release() used in if loop

if (err_pool) {
            console.log(' Error getting mysql_pool connection: ' + err_pool);
            throw err_pool;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM