鍍金池/ 問答/數(shù)據(jù)庫  HTML/ node中mysql連接池的connectionLimit指什么,它和mysql

node中mysql連接池的connectionLimit指什么,它和mysql的最小連接數(shù)和最大連接數(shù)的關(guān)系是什么?

問題1:node中mysql連接池的connectionLimit指什么,它和mysql的最小連接數(shù)和最大連接數(shù)的關(guān)系是什么
問題2:mysql max_connections是什么,max_used_connections是什么,兩者的關(guān)系?為啥max_connecitons可以小于max_used_connections?
問題3:mysql pool.on('connection',callback)在什么情況下觸發(fā)?例如我的這段代碼,什么時候觸發(fā)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);
回答
編輯回答
祈歡

個人理解:
1、connectionLimit 指的就是連接池可創(chuàng)建的最大連接數(shù),mysql 沒有最小連接數(shù),connectionLimit 由應(yīng)用程序開發(fā)者自己設(shè)置,肯定是要不超過 mysql 的最大連接數(shù)

2、max_connections 就是 mysql 能同時提供有效服務(wù)的最大連接數(shù),max_used_connections 是到 mysql 的峰值連接數(shù),max_connecitons 可以小于 max_used_connections,比如說:你的 max_connections 為 1000 ,但是應(yīng)用程序某個時刻建立了 1250 個連接,這多出來的連接數(shù)中就會出現(xiàn)無法提供有效服務(wù)的情況,mysql 也會報錯 too many connections

3、連接池中建立新的連接就會觸發(fā) connection 事件

2017年6月3日 03:43