node中mysql连接池的connectionLimit指什么,它和mysql的最小连接数和最大连接数的关系是什么?

问题1:node中mysql连接池的connectionLimit指什么,它和mysql的最小连接数和最大连接数的关系是什么
问题2:mysql max_connections是什么,max_used_connections是什么,两者的关系?为啥max_connecitons可以小于max_used_connections?
问题3:mysql pool.on('connection',callback)在什么情况下触发?例如我的这段代码,什么时候触发connection事件?

var mysql = require('mysql');
var http = require('http');
var pool  = mysql.createPool({
    host     : 'localhost',
    user     : 'guest',
    password : '****',
    database : 'test',
    connectionLimit: 2
});
pool.on('acquire', function(connection){
    console.log('connection %d accuired', connection.threadId);
});
pool.on('connection', function (connection) {
    connection.query('SET SESSION auto_increment_increment=1')
});
pool.on('enqueue', function () {
    console.log('Waiting for available connection slot');
});
// pool.end(function (err) {
//     console.log('end!')
// });
http.createServer(function(req,res){
    if(req.url==='/url1') {
        pool.getConnection(function(err, connection) {
            connection.query('select * from teacher', function(error,results,fields) {
                if (error) throw error;
                res.end(JSON.stringify(results));
                // connection.release();
            });
        })
    }
    if(req.url==='/url2') {
        pool.getConnection(function(err, connection) {
            connection.query('select * from student', function(error,results,fields) {
                if (error) throw error;
                res.end(JSON.stringify(results));
                connection.release();
            });
        })
    }
}).listen(8001);
阅读 9.1k
1 个回答

个人理解:
1、connectionLimit 指的就是连接池可创建的最大连接数,mysql 没有最小连接数,connectionLimit 由应用程序开发者自己设置,肯定是要不超过 mysql 的最大连接数

2、max_connections 就是 mysql 能同时提供有效服务的最大连接数,max_used_connections 是到 mysql 的峰值连接数,max_connecitons 可以小于 max_used_connections,比如说:你的 max_connections 为 1000 ,但是应用程序某个时刻建立了 1250 个连接,这多出来的连接数中就会出现无法提供有效服务的情况,mysql 也会报错 too many connections

3、连接池中建立新的连接就会触发 connection 事件

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题