node-mysql 连接池

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6731214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 20:31:11  来源:igfitidea点击:

node-mysql connection pooling

mysqldatabasenode.jsconnection-pooling

提问by Omer Aslam

I am using node-mysql module (https://github.com/felixge/node-mysql) OR (http://utahjs.com/2010/09/22/nodejs-and-mysql-introduction/) .

我正在使用 node-mysql 模块 ( https://github.com/felixge/node-mysql) 或 ( http://utahjs.com/2010/09/22/nodejs-and-mysql-introduction/)。

Is this API handling connection pooling as well?

这个 API 是否也处理连接池?

I mean with every user request I am calling Client.connect()to query the MySQL and to release the connection: Client.end().

我的意思是与每一个用户请求我打电话Client.connect()来查询MySQL和释放连接:Client.end()

Is this the right way, or should I connect/disconnect only once in a code.

这是正确的方法,还是我应该在代码中只连接/断开一次。

I am learning from this document: https://github.com/felixge/node-mysql/blob/master/Readme.md

我正在从这个文档中学习:https: //github.com/felixge/node-mysql/blob/master/Readme.md

回答by Andrey Sidorov

Update: Feb 2013 - pool support has been added to node-mysql, see docs

更新:2013 年 2 月 - 池支持已添加到 node-mysql,请参阅文档

Example using built-in pool:

使用内置池的示例:

var pool = require('mysql').createPool(opts);

pool.getConnection(function(err, conn) {
  conn.query('select 1+1', function(err, res) {
    conn.release();
  });
});

Pre 2013 solutions:

2013 年之前的解决方案:

You can use node-poolor mysql-poolor use your own simple round-robin pool

您可以使用node-poolmysql-pool或使用您自己的简单循环池

function Pool(num_conns)
{
    this.pool = [];
    for(var i=0; i < num_conns; ++i)
        this.pool.push(createConnection()); // your new Client + auth
    this.last = 0;
}

Pool.prototype.get = function()
{
    var cli = this.pool[this.last];
    this.last++;
    if (this.last == this.pool.length) // cyclic increment
       this.last = 0;
    return cli;
}

now you can hope to have all queries callbacks to execute in 1 second:

现在您可以希望在 1 秒内执行所有查询回调:

var p = new Pool(16);
for (var i=0; i < 10; ++i)
{
    p.get().query('select sleep(1)', function() { console.log('ready'); } ); // server blocks for 1 second
}

回答by VCD

The node-mysql module support connection pool now.

node-mysql 模块现在支持连接池。

Reference: https://github.com/felixge/node-mysql#pooling-connections

参考:https: //github.com/felixge/node-mysql#pooling-connections

回答by Shaikh Shahid

I believe same node-mysql package provides connection pooling. Have a look

我相信同一个 node-mysql 包提供了连接池。看一看

var express   =    require("express");
var mysql     =    require('mysql');
var app       =    express();

var pool      =    mysql.createPool({
    connectionLimit : 100, //important
    host     : 'localhost',
    user     : 'root',
    password : '',
    database : 'address_book',
    debug    :  false
});

function handle_database(req,res) {

    pool.getConnection(function(err,connection){
        if (err) {
          connection.release();
          res.json({"code" : 100, "status" : "Error in connection database"});
          return;
        }   

        console.log('connected as id ' + connection.threadId);

        connection.query("select * from user",function(err,rows){
            connection.release();
            if(!err) {
                res.json(rows);
            }           
        });

        connection.on('error', function(err) {      
              res.json({"code" : 100, "status" : "Error in connection database"});
              return;     
        });
  });
}

app.get("/",function(req,res){-
        handle_database(req,res);
});

app.listen(3000);

Read complete case study (with and without pool) : http://codeforgeek.com/2015/01/nodejs-mysql-tutorial/

阅读完整的案例研究(有和没有池):http: //codeforgeek.com/2015/01/nodejs-mysql-tutorial/