简体   繁体   English

node-mysql 连接池

[英]node-mysql connection pooling

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

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-pool or mysql-pool or 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
}

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

I believe same node-mysql package provides connection pooling.我相信相同的 node-mysql package 提供连接池。 Have a look看一看

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/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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